public void DataContractSerializerTest()
        {
            ValidateSerialization(new JsonPrimitive(DateTime.Now));
            ValidateSerialization(new JsonObject {
                { "a", 1 }, { "b", 2 }, { "c", 3 }
            });
            ValidateSerialization(new JsonArray {
                "a", "b", "c", 1, 2, 3
            });

            JsonObject beforeObject = new JsonObject {
                { "a", 1 }, { "b", 2 }, { "c", 3 }
            };
            JsonObject afterObject1 = (JsonObject)ValidateSerialization(beforeObject);

            beforeObject.Add("d", 4);
            afterObject1.Add("d", 4);
            Assert.Equal(beforeObject.ToString(), afterObject1.ToString());

            JsonObject afterObject2 = (JsonObject)ValidateSerialization(beforeObject);

            beforeObject.Add("e", 5);
            afterObject2.Add("e", 5);
            Assert.Equal(beforeObject.ToString(), afterObject2.ToString());

            JsonArray beforeArray = new JsonArray {
                "a", "b", "c"
            };
            JsonArray afterArray1 = (JsonArray)ValidateSerialization(beforeArray);

            beforeArray.Add("d");
            afterArray1.Add("d");
            Assert.Equal(beforeArray.ToString(), afterArray1.ToString());

            JsonArray afterArray2 = (JsonArray)ValidateSerialization(beforeArray);

            beforeArray.Add("e");
            afterArray2.Add("e");
            Assert.Equal(beforeArray.ToString(), afterArray2.ToString());
        }
        public void TestDeeplyNestedArrayGraph()
        {
            JsonArray     ja              = new JsonArray();
            JsonArray     current         = ja;
            StringBuilder builderExpected = new StringBuilder();

            builderExpected.Append('[');
            int depth = 10000;

            for (int i = 0; i < depth; i++)
            {
                JsonArray next = new JsonArray();
                builderExpected.Append('[');
                current.Add(next);
                current = next;
            }

            for (int i = 0; i < depth + 1; i++)
            {
                builderExpected.Append(']');
            }

            Assert.Equal(builderExpected.ToString(), ja.ToString());
        }
Ejemplo n.º 3
0
        JsonValue ReadCore()
        {
            SkipSpaces();
            int c = PeekChar();

            if (c < 0)
            {
                throw JsonError("Incomplete JSON input");
            }
            switch (c)
            {
            case '[':
                ReadChar();
                var list = new JsonArray();
                SkipSpaces();
                if (PeekChar() == ']')
                {
                    ReadChar();
                    return(list);
                }
                while (true)
                {
                    list.Add(ReadCore());
                    SkipSpaces();
                    c = PeekChar();
                    if (c != ',')
                    {
                        break;
                    }
                    ReadChar();
                    continue;
                }
                if (ReadChar() != ']')
                {
                    throw JsonError("JSON array must end with ']'");
                }
                return(list);

            case '{':
                ReadChar();
                var obj = new JsonObject();
                SkipSpaces();
                if (PeekChar() == '}')
                {
                    ReadChar();
                    return(obj);
                }
                while (true)
                {
                    SkipSpaces();
                    string name = ReadStringLiteral();
                    SkipSpaces();
                    Expect(':');
                    SkipSpaces();
                    obj.Add(name, ReadCore());
                    SkipSpaces();
                    c = ReadChar();
                    if (c == ',')
                    {
                        continue;
                    }
                    if (c == '}')
                    {
                        break;
                    }
                }
                return(obj);

            case 't':
                Expect("true");
                return(new JsonPrimitive(true));

            case 'f':
                Expect("false");
                return(new JsonPrimitive(false));

            case 'n':
                Expect("null");
                // FIXME: what should we return?
                return(new JsonPrimitive((string)null));

            case '"':
                return(new JsonPrimitive(ReadStringLiteral()));

            default:
                if ('0' <= c && c <= '9' || c == '-')
                {
                    return(ReadNumericLiteral());
                }
                else
                {
                    throw JsonError(String.Format("Unexpected character '{0}'", (char)c));
                }
            }
        }