示例#1
0
 public void ctor_Stream_JsonWriterSettingsNull()
 {
     using (var stream = new MemoryStream())
     {
         using (var writer = new JsonWriter(stream, null))
         {
             Assert.NotNull(writer);
         }
     }
 }
示例#2
0
        public void op_ArrayNull_whenParentNotArray()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();

                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<InvalidOperationException>(() => writer.ArrayNull());

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
        public void op_WriteJson_JsonWriter()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    // ReSharper disable AccessToDisposedClosure
                    var mock = new Mock<IJsonSerializable>();
                    mock
                        .Setup(x => x.WriteJson(writer))
                        .Verifiable();

                    // ReSharper restore AccessToDisposedClosure
                    mock.Object.WriteJson(writer);

                    mock.VerifyAll();
                }
            }
        }
        public static string JsonSerialize(this object value,
                                           JsonWriterSettings settings)
        {
            if (null == value)
            {
                return null;
            }

            if (null == settings)
            {
                throw new ArgumentNullException("settings");
            }

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream, settings))
                {
                    var list = value as IEnumerable;
                    if (value.GetType().IsArray)
                    {
                        writer.Array();
                        writer.JsonSerializeList(list);
                    }
                    else
                    {
                        writer.Object();
                        writer.JsonSerializeObject(value);
                    }
                }

                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
示例#5
0
        public void op_Array_string_whenRoot()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<InvalidOperationException>(() => writer.Array("example"));

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
示例#6
0
        public void op_WriteJson_JsonWriter(string expected)
        {
            var obj = new JsonObject
                          {
                              new JsonPair("id", new JsonNumber("123")),
                              new JsonPair("title", new JsonString(string.Empty)),
                              new JsonPair("value", new JsonNull()),
                              new JsonPair("list",
                                           new JsonArray
                                               {
                                                   Values =
                                                       {
                                                           new JsonNumber("1"),
                                                           new JsonString(string.Empty),
                                                           new JsonNull(),
                                                           new JsonTrue(),
                                                           new JsonFalse()
                                                       }
                                               }),
                              new JsonPair("visible", new JsonTrue()),
                              new JsonPair("enabled", new JsonFalse())
                          };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    obj.WriteJson(writer);
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#7
0
        public void op_Pair_string_bool_whenArrayParent()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Array("example");

                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<InvalidOperationException>(() => writer.Pair("name", true));

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
示例#8
0
        public void op_Pair_string_DateTime(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Pair("example", new DateTime(2011, 7, 14, 19, 43, 37, DateTimeKind.Utc));
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#9
0
        public void op_Object(string expected,
                              int count)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    for (var i = 0; i < count; i++)
                    {
                        writer.Object();
                        writer.EndObject();
                    }
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#10
0
        public void write_object_with_array(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Array("example");
                    writer.ArrayNull();
                    writer.ArrayNumber("1");
                    writer.ArrayValue(2L);
                    writer.ArrayValue(3m);
                    writer.ArrayValue(4d);
                    writer.ArrayValue(" ");
                    writer.ArrayValue(true);
                    writer.Object();
                    writer.Pair("one", 1);
                    writer.EndObject();
                    writer.EndArray();
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#11
0
        public void write_empty_object(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#12
0
        public void roundtrip_terse(string json)
        {
            var expected = new FileInfo(json).ReadToEnd();

            var document = JsonDocument.Load(expected);

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#13
0
        public void roundtrip_pretty(string source,
                                     string destination)
        {
            var expected = new FileInfo(destination).ReadToEnd();

            var document = JsonDocument.Load(new FileInfo(source).ReadToEnd());

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream, JsonWriterSettings.Pretty))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    ////new FileInfo("{0}.txt".FormatWith(source)).Append(actual);
                    Assert.Equal(expected, actual);
                }
            }
        }
示例#14
0
        public void op_WriteJson_JsonWriter_whenEmptyObject(string expected)
        {
            var document = new JsonDocument
                               {
                                   new JsonObject()
                               };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#15
0
        public void op_WriteJson_JsonWriter_whenArray(string expected)
        {
            var document = new JsonDocument
                               {
                                   new JsonObject
                                       {
                                           new JsonPair("one", new JsonNumber("1"))
                                       },
                                   new JsonObject
                                       {
                                           new JsonPair("two", new JsonNumber("2"))
                                       }
                               };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#16
0
        public void op_NumberPair_string_stringEmpty(string value)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<ArgumentOutOfRangeException>(() => writer.NumberPair("example", value));

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
示例#17
0
        public void write_object_with_array_of_objects(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Array("example");
                    writer.Object();
                    writer.Pair("one", 1);
                    writer.EndObject();
                    writer.Object();
                    writer.Pair("two", 2);
                    writer.EndObject();
                    writer.EndArray();
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#18
0
        public void op_Object_whenAfterPair()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Pair("one", 1);

                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<InvalidOperationException>(() => writer.Object());

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
示例#19
0
        public void write_object_with_nested_arrays(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Array("list");
                    writer.ArrayValue(true);
                    writer.Array();
                    writer.ArrayValue(1);
                    writer.ArrayValue(2);
                    writer.ArrayValue(3);
                    writer.EndArray();
                    writer.ArrayValue(false);
                    writer.EndArray();
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#20
0
        public void op_Pair_stringNull_string()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<ArgumentNullException>(() => writer.Pair(null, "value"));

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }
示例#21
0
        public void write_object_with_pairs(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Pair("id", 123);
                    writer.Pair("title", string.Empty);
                    writer.NullPair("value");
                    writer.Array("list");
                    writer.ArrayValue(1);
                    writer.ArrayValue(string.Empty);
                    writer.ArrayNull();
                    writer.ArrayValue(true);
                    writer.ArrayValue(false);
                    writer.EndArray();
                    writer.Pair("visible", true);
                    writer.Pair("enabled", false);
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#22
0
        public void op_Pair_string_TimeSpan(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Pair("example", new TimeSpan(0, 17, 9, 43, 89));
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#23
0
        public void write_pretty_object(string json)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream, JsonWriterSettings.Debug))
                {
                    writer.Object();
                    writer.Pair("id", 123);
                    writer.Pair("title", string.Empty);
                    writer.NullPair("value");
                    writer.Array("list");
                    writer.ArrayValue(1);
                    writer.ArrayValue(string.Empty);
                    writer.ArrayNull();
                    writer.ArrayValue(true);
                    writer.ArrayValue(false);
                    writer.EndArray();
                    writer.Pair("visible", true);
                    writer.Pair("enabled", false);
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var expected = new FileInfo(json).ReadToEnd();
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#24
0
        public void op_StringPair_string_string(string value,
                                                string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Pair("example", value);
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#25
0
        public void op_ArrayValue_long(string expected)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    writer.Array("example");
                    writer.ArrayValue(12345L);
                    writer.EndArray();
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#26
0
        public void op_WriteJson_JsonWriter_whenNestedArrays(string expected)
        {
            var obj = new JsonObject
                          {
                              new JsonPair("list",
                                           new JsonArray
                                               {
                                                   Values =
                                                       {
                                                           new JsonTrue(),
                                                           new JsonArray
                                                               {
                                                                   Values =
                                                                       {
                                                                           new JsonNumber("1"),
                                                                           new JsonNumber("2"),
                                                                           new JsonNumber("3")
                                                                       }
                                                               },
                                                           new JsonFalse()
                                                       }
                                               })
                          };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();
                    obj.WriteJson(writer);
                    writer.EndObject();
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#27
0
        public void op_Array_stringNull()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    writer.Object();

                    // ReSharper disable AccessToDisposedClosure
                    Assert.Throws<ArgumentNullException>(() => writer.Array(null));

                    // ReSharper restore AccessToDisposedClosure
                }
            }
        }