Beispiel #1
0
    def __parse_object(self):
        """Parse a JSON object. The scanner must be at the first '{'."""

        object_start = self.__scanner.position
        self.__scanner.read_ubyte()

        result_object = JsonObject()

        while True:
            c = self.__peek_next_non_whitespace()

            if c is None:
                return self.__error("Need '}' for end of object", object_start)
            elif c == '"':
                key = self.__parse_string()
            elif c == '_' or 'a' <= c <= 'z' or 'A' <= c <= 'Z':
                key = self.__parse_identifier()

                next_char = self.__scanner.peek_next_ubyte(
                    none_if_bad_index=True)

                if next_char is None:
                    return self.__error("Need '}' for end of object",
                                        object_start)
                if ord(next_char) > 32 and next_char != ':':
                    self.__error("To use character '%s' in an attribute name, "
                                 "you must place the attribute name in "
                                 "double-quotes" % next_char)
            elif c == '}':
                # End-of-object.
                self.__scanner.read_ubyte()
                return result_object
            else:
                return self.__error(
                    "Expected string literal for object attribute name")

            self.__peek_next_non_whitespace()
            c = self.__scanner.read_ubyte()

            if c != ':':
                self.__error("Expected ':' delimiting object attribute value")

            # skip any whitespace after the colon
            self.__peek_next_non_whitespace()
            if self.check_duplicate_keys and result_object.__contains__(key):
                self.__error("Duplicate key [" + key + "]", object_start)

            result_object.put(key, self.parse_value())

            c = self.__peek_next_non_whitespace()

            if c is None:
                self.__error("Need '}' for end of object", object_start)
            elif c == '}':
                # do nothing we'll process the '}' back around at the top of
                # the loop.
                continue
            elif c == ',':
                self.__scanner.read_ubyte()
            else:
                if self.__preceding_line_break() and self.allow_missing_commas:
                    # proceed, inferring a comma
                    continue
                else:
                    self.__error("After object field, expected ',' or '}' but "
                                 "found '%s'... are you missing a comma?" % c)
Beispiel #2
0
    def __parse_object(self):
        """Parse a JSON object. The scanner must be at the first '{'."""

        object_start = self.__scanner.position
        self.__scanner.read_ubyte()

        result_object = JsonObject()

        while True:
            c = self.__peek_next_non_whitespace()
      
            if c is None:
                return self.__error("Need '}' for end of object", object_start)
            elif c == '"':
                key = self.__parse_string()
            elif c == '_' or 'a' <= c <= 'z' or 'A' <= c <= 'Z':
                key = self.__parse_identifier()
    
                next_char = self.__scanner.peek_next_ubyte(
                    none_if_bad_index=True)

                if next_char is None:
                    return self.__error("Need '}' for end of object",
                                        object_start)
                if ord(next_char) > 32 and next_char != ':':
                    self.__error("To use character '%s' in an attribute name, "
                                 "you must place the attribute name in "
                                 "double-quotes" % next_char)
            elif c == '}':
                # End-of-object.
                self.__scanner.read_ubyte()
                return result_object
            else:
                return self.__error(
                    "Expected string literal for object attribute name")

            self.__peek_next_non_whitespace()
            c = self.__scanner.read_ubyte()

            if c != ':':
                self.__error("Expected ':' delimiting object attribute value")

            # skip any whitespace after the colon
            self.__peek_next_non_whitespace()
            if self.check_duplicate_keys and result_object.__contains__(key):
                self.__error("Duplicate key [" + key + "]", object_start)

            result_object.put(key, self.parse_value())
      
            c = self.__peek_next_non_whitespace()
      
            if c is None:
                self.__error("Need '}' for end of object", object_start)
            elif c == '}':
                # do nothing we'll process the '}' back around at the top of 
                # the loop.
                continue
            elif c == ',':
                self.__scanner.read_ubyte()
            else:
                if self.__preceding_line_break() and self.allow_missing_commas:
                    # proceed, inferring a comma
                    continue
                else:
                    self.__error("After object field, expected ',' or '}' but "
                                 "found '%s'... are you missing a comma?" % c)