コード例 #1
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void ReadLiteralFalse(string content)
        {
            using (var stream = CreateTextReader(content))
            {
                var buffer = new JsonBuffer(stream);
                Assert.NotNull(buffer);

                var token = buffer.Read();
                Assert.Equal(JsonTokenType.False, token.Type);
            }
        }
コード例 #2
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void ReadTokenThroughLockFileSample()
        {
            using (var stream = File.OpenRead(Path.Combine("TestSample", "project.lock.sample")))
            {
                var reader = new StreamReader(stream);
                var buffer = new JsonBuffer(reader);
                Assert.NotNull(buffer);

                while (buffer.Read().Type != JsonTokenType.EOF)
                    ;
            }
        }
コード例 #3
0
        public static JsonValue Deserialize(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var buffer = new JsonBuffer(reader);

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

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

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

            return(result);
        }
コード例 #4
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void BasicReadToken()
        {
            var content = @"
            {
                ""key1"": ""value1"",
                ""key2"": 99,
                ""key3"": true,
                ""key4"": [""str1"", ""str2"", ""str3""],
                ""key5"": {
                    ""subkey1"": ""subvalue1"",
                    ""subkey2"": [1, 2]
                },
                ""key6"": null
            }";

            using (var stream = CreateTextReader(content))
            {
                var buffer = new JsonBuffer(stream);
                Assert.NotNull(buffer);

                while (buffer.Read().Type != JsonTokenType.EOF)
                    ;
            }
        }
コード例 #5
0
ファイル: Json.cs プロジェクト: modai888/xunit
        static JsonObject DeserializeObject(JsonToken head, JsonBuffer buffer)
        {
            var dictionary = new Dictionary<string, JsonValue>();

            // Loop through each JSON entry in the input object
            while (true)
            {
                var 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);
                    }

                    var 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);
        }
コード例 #6
0
ファイル: Json.cs プロジェクト: modai888/xunit
        static JsonArray DeserializeArray(JsonToken head, JsonBuffer buffer)
        {
            var list = new List<JsonValue>();
            while (true)
            {
                var next = buffer.Read();
                if (next.Type == JsonTokenType.RightSquareBracket)
                {
                    break;
                }

                list.Add(DeserializeInternal(next, buffer));

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

            return new JsonArray(list.ToArray(), head.Line, head.Column);
        }
コード例 #7
0
ファイル: Json.cs プロジェクト: modai888/xunit
        public static JsonValue Deserialize(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var buffer = new JsonBuffer(reader);

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

            // There are still unprocessed char. The parsing is not finished. Error happened.
            var nextToken = buffer.Read();
            if (nextToken.Type != JsonTokenType.EOF)
            {
                throw new JsonDeserializerException(
                    JsonDeserializerResource.Format_UnfinishedJSON(nextToken.Value),
                    nextToken);
            }

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

            // Loop through each JSON entry in the input object
            while (true)
            {
                var 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);
                    }

                    var 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));
        }
コード例 #9
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void ReadUnicode()
        {
            var content = "\"Unicode string \\u00A9\"";
            using (var stream = CreateTextReader(content))
            {
                var buffer = new JsonBuffer(stream);
                Assert.NotNull(buffer);

                var token = buffer.Read();
                Assert.Equal(JsonTokenType.String, token.Type);
                Assert.Equal(16, token.Value.Length);
            }
        }
コード例 #10
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void ReadThroughArray(string content)
        {
            using (var stream = CreateTextReader(content))
            {
                var buffer = new JsonBuffer(stream);
                Assert.NotNull(buffer);

                var tokens = new List<JsonToken>();
                do
                {
                    tokens.Add(buffer.Read());
                }
                while (tokens[tokens.Count - 1].Type != JsonTokenType.EOF);

                Assert.Equal(JsonTokenType.LeftSquareBracket, tokens[0].Type);
                Assert.Equal(JsonTokenType.RightSquareBracket, tokens[tokens.Count - 2].Type);
            }
        }
コード例 #11
0
ファイル: JsonBufferFacts.cs プロジェクト: leloulight/dnx
        public void ReadLiteralThrowException(string content)
        {
            using (var stream = CreateTextReader(content))
            {
                var buffer = new JsonBuffer(stream);
                Assert.NotNull(buffer);

                Assert.Throws<JsonDeserializerException>(() =>
                {
                    buffer.Read();
                });
            }
        }