Esempio n. 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
Esempio n. 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
Esempio n. 3
0
    def _decode_float_int(self, string, index):
        integer = ''
        frac = ''
        exp = ''
        isInt = True
        isFrac = False
        isExp = False

        while True:
            if len(string) == index:
                break
            nextchar = string[index]

            if nextchar in Tool.WhiteSpace:
                index = Tool.escape_whitespace(string, index)
            elif nextchar == '-' or nextchar == '+' or ('0' <= nextchar <= '9'):
                if isInt:
                    integer += nextchar
                    index += 1
                elif isFrac and not isExp:
                    frac += nextchar
                    index += 1
                elif isExp:
                    exp += nextchar
                    index += 1
            elif nextchar == '.':
                isInt = False
                isFrac = True
                frac += nextchar
                index += 1
            elif nextchar == 'e' or nextchar == 'E':
                isExp = True
                exp += nextchar
                index += 1
            else:
                break

        if isFrac:
            res = float(integer + frac + exp)
        else:
            res = int(integer)

        return res, index
Esempio n. 4
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