コード例 #1
0
        public static JsonValue Deserialize(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            JsonBuffer buffer = new JsonBuffer(reader);

            JsonValue result = DeserializeInternal(buffer.Read(), buffer);

            // There are still unprocessed char. The parsing is not finished. Error happened.
            JsonToken nextToken = buffer.Read();

            if (nextToken.Type != JsonTokenType.EOF)
            {
                throw new JsonDeserializerException(
                          JsonDeserializerResource.Format_UnfinishedJSON(nextToken.Value),
                          nextToken);
            }

            return(result);
        }
コード例 #2
0
        private static JsonObject DeserializeObject(JsonToken head, JsonBuffer buffer)
        {
            Dictionary <string, JsonValue> dictionary = new Dictionary <string, JsonValue>();

            // Loop through each JSON entry in the input object
            while (true)
            {
                JsonToken next = buffer.Read();
                if (next.Type == JsonTokenType.EOF)
                {
                    throw new JsonDeserializerException(
                              JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", '}'),
                              next);
                }

                if (next.Type == JsonTokenType.Colon)
                {
                    throw new JsonDeserializerException(
                              JsonDeserializerResource.Format_InvalidSyntaxNotExpected("JSON object", ':'),
                              next);
                }
                else if (next.Type == JsonTokenType.RightCurlyBracket)
                {
                    break;
                }
                else
                {
                    if (next.Type != JsonTokenType.String)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object member name", "JSON string"),
                                  next);
                    }

                    string memberName = next.Value;
                    if (dictionary.ContainsKey(memberName))
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_DuplicateObjectMemberName(memberName),
                                  next);
                    }

                    next = buffer.Read();
                    if (next.Type != JsonTokenType.Colon)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ':'),
                                  next);
                    }

                    dictionary[memberName] = DeserializeInternal(buffer.Read(), buffer);

                    next = buffer.Read();
                    if (next.Type == JsonTokenType.RightCurlyBracket)
                    {
                        break;
                    }
                    else if (next.Type != JsonTokenType.Comma)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ',', '}'),
                                  next);
                    }
                }
            }

            return(new JsonObject(dictionary, head.Line, head.Column));
        }