Exemplo n.º 1
0
def obj2json(obj, path, **metadata):
    """serialise object to json file"""
    from cing.Libs.disk import Path

    p = Path(str(path))  # assure path instance
    root, f, ext = p.split3()
    root.makedirs()
    with open(p,'w') as fp:
        fp.write(encode(obj, path=str(path), timestamp=str(io.now()), **metadata))
Exemplo n.º 2
0
def json2obj(path, referenceObject=None):
    """return object from serialised representation in json file
    or None on error
    """
    from cing.Libs.disk import Path

    p = Path(str(path))  # assure path instance
    if not p.exists():
        io.error('json2obj: path "{0}" does not exist\n', path)
        return None, None

    with open(p,'r') as fp:
        try:
            obj, keyedMetadata = decode(fp.read(), referenceObject=referenceObject)
        except ValueError:
            io.error('json2obj: trying to decode object from "{0}" failed\n', path)
            return None, None
    return obj, keyedMetadata