Example #1
0
def toString(val):
    if not convert:
        _late_import()

    if val == None:
        return ""
    elif isinstance(val, (Mapping, list, set)):
        from pyLibrary.jsons.encoder import json_encoder

        return json_encoder(val, pretty=True)
    elif hasattr(val, "__json__"):
        return val.__json__()
    elif isinstance(val, Duration):
        return unicode(round(val.seconds, places=4)) + " seconds"
    elif isinstance(val, timedelta):
        duration = val.total_seconds()
        return unicode(round(duration, 3)) + " seconds"

    try:
        return unicode(val)
    except Exception, e:
        if not Log:
            _late_import()

        Log.error(str(type(val)) + " type can not be converted to unicode", e)
Example #2
0
def value2json(obj, pretty=False):
    try:
        json = json_encoder(obj, pretty=pretty)
        if json == None:
            Log.note(str(type(obj)) + " is not valid{{type}}JSON",  type= " (pretty) " if pretty else " ")
            Log.error("Not valid JSON: " + str(obj) + " of type " + str(type(obj)))
        return json
    except Exception, e:
        Log.error("Can not encode into JSON: {{value}}",  value= repr(obj), cause=e)
Example #3
0
def value2json(obj, pretty=False, sort_keys=False):
    try:
        json = json_encoder(obj, pretty=pretty)
        if json == None:
            Log.note(str(type(obj)) + " is not valid{{type}}JSON",  type= " (pretty) " if pretty else " ")
            Log.error("Not valid JSON: " + str(obj) + " of type " + str(type(obj)))
        return json
    except Exception, e:
        e = Except.wrap(e)
        with suppress_exception:
            json = pypy_json_encode(obj)
            return json

        Log.error("Can not encode into JSON: {{value}}", value=repr(obj), cause=e)
Example #4
0
def toString(val):
    if val == None:
        return ""
    elif isinstance(val, (Mapping, list, set)):
        from pyLibrary.jsons.encoder import json_encoder

        return json_encoder(val, pretty=True)
    elif hasattr(val, "__json__"):
        return val.__json__()
    elif isinstance(val, timedelta):
        duration = val.total_seconds()
        return unicode(round(duration, 3)) + " seconds"

    try:
        return unicode(val)
    except Exception, e:
        if not Log:
            _late_import()

        Log.error(str(type(val)) + " type can not be converted to unicode", e)
Example #5
0
 def __json__(self):
     return json_encoder(self.as_dict())
Example #6
0
 def __json__(self):
     return json_encoder(self.as_dict())