Пример #1
0
 public void create_empty_array()
 {
     var array = new JsonArray();
     array.ToString().ShouldEqual("[]");
 }
Пример #2
0
 public void create_jagged_array()
 {
     var array = new JsonArray(new[] { new object[0], new object[] { 1 }, new object[] { 1, 2 } });
     array.ToString().ShouldEqual("[[],[1],[1,2]]");
 }
Пример #3
0
 public void create_array()
 {
     var array = new JsonArray(new[] { 1, 2, 3 });
     array.ToString().ShouldEqual("[1,2,3]");
 }
Пример #4
0
 public void add_new_item_to_array()
 {
     var array = new JsonArray { new JsonValue(1), new JsonValue(2), new JsonValue(3) };
     array.ToString().ShouldEqual("[1,2,3]");
 }
        public void ValidJsonArrayRoundTrip()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;
            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;
                Log.Info("Seed: {0}", seed);
                Random rndGen = new Random(seed);

                JsonArray sourceJson = new JsonArray(new JsonValue[]
                {
                    PrimitiveCreator.CreateInstanceOfBoolean(rndGen),
                    PrimitiveCreator.CreateInstanceOfByte(rndGen),
                    PrimitiveCreator.CreateInstanceOfDateTime(rndGen),
                    PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen),
                    PrimitiveCreator.CreateInstanceOfDecimal(rndGen),
                    PrimitiveCreator.CreateInstanceOfDouble(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt16(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt32(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt64(rndGen),
                    PrimitiveCreator.CreateInstanceOfSByte(rndGen),
                    PrimitiveCreator.CreateInstanceOfSingle(rndGen),
                    PrimitiveCreator.CreateInstanceOfString(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt16(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt32(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt64(rndGen)
                });

                JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString());

                Log.Info("Original JsonArray object is: {0}", sourceJson);
                Log.Info("Round-tripped JsonArray object is: {0}", newJson);

                Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
        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 ToStringTest()
 {
     JsonObject jo = new JsonObject
     {
         { "first", 1 },
         { "second", 2 },
         { "third", new JsonObject { { "inner_one", 4 }, { "", null }, { "inner_3", "" } } },
         { "fourth", new JsonArray { "Item1", 2, false } },
         { "fifth", null }
     };
     JsonValue jv = new JsonArray(123, null, jo);
     string expectedJson = "[\r\n  123,\r\n  null,\r\n  {\r\n    \"first\": 1,\r\n    \"second\": 2,\r\n    \"third\": {\r\n      \"inner_one\": 4,\r\n      \"\": null,\r\n      \"inner_3\": \"\"\r\n    },\r\n    \"fourth\": [\r\n      \"Item1\",\r\n      2,\r\n      false\r\n    ],\r\n    \"fifth\": null\r\n  }\r\n]";
     Assert.Equal<string>(expectedJson.Replace("\r\n", "").Replace(" ", ""), jv.ToString());
 }