Beispiel #1
0
def typed_encode(value):
    """
    pypy DOES NOT OPTIMIZE GENERATOR CODE WELL
    """
    try:
        _buffer = UnicodeBuilder(1024)
        _typed_encode(value, _buffer)
        output = _buffer.build()
        return output
    except Exception, e:
        # THE PRETTY JSON WILL PROVIDE MORE DETAIL ABOUT THE SERIALIZATION CONCERNS
        from pyLibrary.debugs.logs import Log

        Log.warning("Serialization of JSON problems", e)
        try:
            return pretty_json(value)
        except Exception, f:
            Log.error("problem serializing object", f)
def parse_string(i, json):
    j = i
    output = UnicodeBuilder()
    while True:
        c = json[j]
        if c == "\"":
            return j + 1, output.build()
        elif c == "\\":
            j += 1
            c = json[j]
            if c == "u":
                n = json[j:j + 4].decode('hex').decode('utf-8')
                output.append(n)
                j += 4
            else:
                try:
                    output.append(ESC[c])
                except Exception, e:
                    output.append("\\")
                    output.append(c)
        else:
            output.append(c)
        j += 1
Beispiel #3
0
def json2typed(json):
    """
    every ': {' gets converted to ': {"$object": ".", '
    every ': <value>' gets converted to '{"$value": <value>}'
    """
    # MODE VALUES
    #

    context = deque()
    output = UnicodeBuilder(1024)
    mode = VALUE
    for c in json:
        if c in "\t\r\n ":
            append(output, c)
        elif mode == VALUE:
            if c == "{":
                context.append(mode)
                mode = BEGIN_OBJECT
                append(output, '{"$object": "."')
                continue
            elif c == '[':
                context.append(mode)
                mode = VALUE
            elif c == ",":
                mode = context.pop()
                if mode != OBJECT:
                    context.append(mode)
                    mode = VALUE
            elif c in "]":
                mode = context.pop()
            elif c in "}":
                mode = context.pop()
                mode = context.pop()
            elif c == '"':
                context.append(mode)
                mode = STRING
                append(output, '{"$value": ')
            else:
                mode = PRIMITIVE
                append(output, '{"$value": ')
            append(output, c)
        elif mode == PRIMITIVE:
            if c == ",":
                append(output, '}')
                mode = context.pop()
                if mode == 0:
                    context.append(mode)
            elif c == "]":
                append(output, '}')
                mode = context.pop()
            elif c == "}":
                append(output, '}')
                mode = context.pop()
                mode = context.pop()
            append(output, c)
        elif mode == BEGIN_OBJECT:
            if c == '"':
                context.append(OBJECT)
                context.append(KEYWORD)
                mode = STRING
                append(output, ', ')
            elif c == "}":
                mode = context.pop()
            else:
                Log.error("not expected")
            append(output, c)
        elif mode == KEYWORD:
            append(output, c)
            if c == ':':
                mode = VALUE
            else:
                Log.error("Not expected")
        elif mode == STRING:
            append(output, c)
            if c == '"':
                mode = context.pop()
                if mode != KEYWORD:
                    append(output, '}')
            elif c == '\\':
                context.append(mode)
                mode = ESCAPE
        elif mode == ESCAPE:
            mode = context.pop()
            append(output, c)
        elif mode == OBJECT:
            if c == '"':
                context.append(mode)
                context.append(KEYWORD)
                mode = STRING
            elif c == ",":
                pass
            elif c == '}':
                mode = context.pop()
            else:
                Log.error("not expected")

            append(output, c)

    if mode == PRIMITIVE:
        append(output, "}")
    return output.build()
Beispiel #4
0
def parse_string(i, json):
    j = i
    output = UnicodeBuilder()
    while True:
        c = json[j]
        if c == "\"":
            return j + 1, output.build()
        elif c == "\\":
            j += 1
            c = json[j]
            if c == "u":
                n = json[j:j + 4].decode('hex').decode('utf-8')
                output.append(n)
                j += 4
            else:
                try:
                    output.append(ESC[c])
                except Exception, e:
                    output.append("\\")
                    output.append(c)
        else:
            output.append(c)
        j += 1