Exemplo n.º 1
0
def _typed_encode(value, _buffer):
    try:
        if value is None:
            append(_buffer, u'{"$value": null}')
            return
        elif value is True:
            append(_buffer, u'{"$value": true}')
            return
        elif value is False:
            append(_buffer, u'{"$value": false}')
            return

        _type = value.__class__
        if _type in (dict, Dict):
            if value:
                _dict2json(value, _buffer)
            else:
                append(_buffer, u'{"$object": "."}')
        elif _type is str:
            append(_buffer, u'{"$value": "')
            try:
                v = utf82unicode(value)
            except Exception, e:
                problem_serializing(value, e)

            for c in v:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, u'"}')
        elif _type is unicode:
            append(_buffer, u'{"$value": "')
            for c in value:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, u'"}')
Exemplo n.º 2
0
def _value2json(value, _buffer):
    try:
        if value is None:
            append(_buffer, "null")
            return
        elif value is True:
            append(_buffer, "true")
            return
        elif value is False:
            append(_buffer, "false")
            return
        elif isinstance(value, Mapping):
            if value:
                _dict2json(value, _buffer)
            else:
                append(_buffer, "{}")
            return

        type = value.__class__
        if type is str:
            append(_buffer, '"')
            try:
                v = utf82unicode(value)
            except Exception, e:
                problem_serializing(value, e)

            for c in v:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, '"')
        elif type is unicode:
            append(_buffer, '"')
            for c in value:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, '"')
Exemplo n.º 3
0
def _value2json(value, _buffer):
    try:
        _class = value.__class__
        if value is None:
            append(_buffer, u"null")
            return
        elif value is True:
            append(_buffer, u"true")
            return
        elif value is False:
            append(_buffer, u"false")
            return

        type = value.__class__
        if type is str:
            append(_buffer, u"\"")
            try:
                v = utf82unicode(value)
            except Exception, e:
                problem_serializing(value, e)

            for c in v:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, u"\"")
        elif type is unicode:
            append(_buffer, u"\"")
            for c in value:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, u"\"")
Exemplo n.º 4
0
def _dict2json(value, _buffer):
    prefix = u"{\""
    for k, v in value.iteritems():
        append(_buffer, prefix)
        prefix = u", \""
        if isinstance(k, str):
            k = utf82unicode(k)
        for c in k:
            append(_buffer, ESCAPE_DCT.get(c, c))
        append(_buffer, u"\": ")
        _value2json(v, _buffer)
    append(_buffer, u"}")
Exemplo n.º 5
0
def _dict2json(value, _buffer):
    prefix = u"{\""
    for k, v in value.iteritems():
        append(_buffer, prefix)
        prefix = u", \""
        if isinstance(k, str):
            k = utf82unicode(k)
        for c in k:
            append(_buffer, ESCAPE_DCT.get(c, c))
        append(_buffer, u"\": ")
        _value2json(v, _buffer)
    append(_buffer, u"}")
Exemplo n.º 6
0
def _dict2json(value, _buffer):
    prefix = u'{"$object": ".", "'
    for k, v in value.iteritems():
        append(_buffer, prefix)
        prefix = u", \""
        if isinstance(k, str):
            k = utf82unicode(k)
        if not isinstance(k, unicode):
            Log.error("Expecting property name to be a string")
        for c in k:
            append(_buffer, ESCAPE_DCT.get(c, c))
        append(_buffer, u"\": ")
        _typed_encode(v, _buffer)
    append(_buffer, u"}")
Exemplo n.º 7
0
def _dict2json(value, _buffer):
    try:
        prefix = '{"'
        for k, v in value.iteritems():
            append(_buffer, prefix)
            prefix = ', "'
            if isinstance(k, str):
                k = utf82unicode(k)
            for c in k:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, '": ')
            _value2json(v, _buffer)
        append(_buffer, "}")
    except Exception, e:
        from pyLibrary.debugs.logs import Log

        Log.error(_repr(value) + " is not JSON serializable", cause=e)
Exemplo n.º 8
0
def _dict2json(value, _buffer):
    try:
        prefix = u"{\""
        for k, v in value.iteritems():
            append(_buffer, prefix)
            prefix = u", \""
            if isinstance(k, str):
                k = utf82unicode(k)
            for c in k:
                append(_buffer, ESCAPE_DCT.get(c, c))
            append(_buffer, u"\": ")
            _value2json(v, _buffer)
        append(_buffer, u"}")
    except Exception, e:
        from pyLibrary.debugs.logs import Log

        Log.error(_repr(value) + " is not JSON serializable", cause=e)