예제 #1
0
 def load(self, source):
     """
     读取json格式数据,source为json字符串。若遇到json格式错误,抛出异常。
     """
     start = Tool.escape_whitespace(source, 0)
     obj, end = self._decode(source, start)
     end = Tool.escape_whitespace(source, end)
     if end != len(source):
         raise ValueError(Tool.errmsg("Extra data", source, end, len(source)))
     return obj
예제 #2
0
    def _decode_list(self, string, index):
        values = []
        nextchar = string[index:index + 1]
        if nextchar in Tool.WhiteSpace:
            index = Tool.escape_whitespace(string, index + 1)
            nextchar = string[index:index + 1]

        if nextchar == ']':
            return values, index + 1

        while True:
            try:
                value, index = self._decode(string, index)
            except StopIteration:
                raise ValueError(Tool.errmsg("Expecting object", string, index))

            values.append(value)
            nextchar = string[index:index + 1]
            if nextchar in Tool.WhiteSpace:
                index = Tool.escape_whitespace(string, index + 1)
                nextchar = string[index:index + 1]
            index += 1
            if nextchar == ']':
                break
            elif nextchar != ',':
                raise ValueError(Tool.errmsg("Expecting , delimiter", string, index))

            try:
                if string[index] in Tool.WhiteSpace:
                    index += 1
                    if string[index] in Tool.WhiteSpace:
                        index = Tool.escape_whitespace(string, index + 1)
            except IndexError:
                pass

        return values, index
예제 #3
0
    def _decode_dict(self, string, index):
        values = []
        nextchar = string[index:index + 1]

        if nextchar != '"':
            if nextchar in Tool.WhiteSpace:
                index = Tool.escape_whitespace(string, index)
                nextchar = string[index:index + 1]

            if nextchar == '}':
                values = {}
                return values, index + 1
            elif nextchar != '"':
                raise ValueError(Tool.errmsg("Expecting property name", string, index))

        index += 1

        while True:
            key, index = self._decode_string(string, index)

            if string[index: index + 1] != ':':
                index = Tool.escape_whitespace(string, index)
                if string[index:index + 1] != ':':
                    raise ValueError(Tool.errmsg("Expecting : delimiter", string, index))

            index += 1

            try:
                if string[index] in Tool.WhiteSpace:
                    index += 1
                    if string[index] in Tool.WhiteSpace:
                        index = Tool.escape_whitespace(string, index + 1)
            except IndexError:
                pass

            try:
                value, index = self._decode(string, index)
            except StopIteration:
                raise ValueError(Tool.errmsg("Expecting object", string, index))

            values.append((key, value))

            try:
                nextchar = string[index]
                if nextchar in Tool.WhiteSpace:
                    index = Tool.escape_whitespace(string, index)
                    nextchar = string[index]
            except IndexError:
                nextchar = ''
            index += 1

            if nextchar == '}':
                break
            elif nextchar != ',':
                raise ValueError(Tool.errmsg("Expecting , delimiter", string, index))

            try:
                nextchar = string[index]
                if nextchar in Tool.WhiteSpace:
                    index += 1
                    nextchar = string[index]
                    if nextchar in Tool.WhiteSpace:
                        index = Tool.escape_whitespace(string, index + 1)
                        nextchar = string[index]
            except IndexError:
                nextchar = ''

            index += 1
            if nextchar != '"':
                raise ValueError(Tool.errmsg("Expecting property name", string, index))

        values = dict(values)

        return values, index