public void op_Read_whenObjectArrayEmpty(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.True(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); } } } }
public void ctor_Stream() { using (var stream = new MemoryStream()) { using (var reader = new JsonReader(stream)) { Assert.NotNull(reader); } } }
public void op_Read_whenEndOfStream() { using (var stream = new MemoryStream()) { using (var reader = new JsonReader(stream)) { Assert.Equal(JsonNodeType.None, reader.NodeType); Assert.False(reader.Read()); } } }
public void op_ReadJson_JsonReader() { using (var stream = new MemoryStream()) { using (var reader = new JsonReader(stream)) { // ReSharper disable AccessToDisposedClosure var mock = new Mock<IJsonSerializable>(); mock .Setup(x => x.ReadJson(reader)) .Verifiable(); // ReSharper restore AccessToDisposedClosure mock.Object.ReadJson(reader); mock.VerifyAll(); } } }
public void op_ReadJson_JsonReader_whenNestedArray(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { reader.Read(); var obj = new JsonObject(); obj.ReadJson(reader); Assert.True(obj.Array("list").Boolean(0)); Assert.Equal("1", obj.Array("list").Array(1).Number(0).Value); Assert.Equal("2", obj.Array("list").Array(1).Number(1).Value); Assert.Equal("3", obj.Array("list").Array(1).Number(2).Value); Assert.False(obj.Array("list").Boolean(2)); Assert.True(obj.Array("list").IsNull(3)); Assert.Equal("abc", obj.Array("list").String(4).Value); Assert.Equal("123", obj.Array("list").Object(5).Number("count").Value); Assert.Equal("456", obj.Array("list").Object(6).Number("count").Value); Assert.True(obj.Array("list").Boolean(7)); Assert.Equal("789", obj.Array("list").Object(8).Number("count").Value); } } } }
public void op_ReadJson_JsonReader_whenEmpty(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { reader.Read(); var obj = new JsonObject(); obj.ReadJson(reader); Assert.Empty(obj); } } } }
public void op_ReadJson_JsonReader(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { reader.Read(); var obj = new JsonObject(); obj.ReadJson(reader); Assert.Equal("value", obj.String("name").Value); Assert.Equal("1", obj.Array("range").Number(0).Value); Assert.Equal("2", obj.Array("range").Number(1).Value); Assert.Equal("3", obj.Array("range").Number(2).Value); Assert.Equal("123", obj.Number("id").Value); Assert.True(obj.Boolean("visible")); Assert.False(obj.Boolean("enabled")); Assert.Null(obj.String("check")); Assert.Equal("1.23", obj.Object("child").Number("value").Value); } } } }
public void op_ReadJson_JsonReader_whenEmptyObject(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { var document = new JsonDocument(); document.ReadJson(reader); Assert.Equal(1, document.Count); } } } }
public void op_ReadJson_JsonReader_whenArray(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { var document = new JsonDocument(); document.ReadJson(reader); Assert.Equal("1", document.First().Number("one").Value); Assert.Equal("2", document.Last().Number("two").Value); } } } }
public void op_ReadJson_JsonReader(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { var document = new JsonDocument(); document.ReadJson(reader); Assert.Equal("value", document.First().String("name").Value); } } } }
public void op_Read_whenRfc4627example2() { var file = new FileInfo("rfc4627 example 2.json"); using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("precision", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("precision", reader.Name); Assert.Equal("zip", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Latitude", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Latitude", reader.Name); Assert.Equal("37.7668", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Longitude", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Longitude", reader.Name); Assert.Equal("-122.3959", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Address", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Address", reader.Name); Assert.Equal(string.Empty, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("City", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("City", reader.Name); Assert.Equal("SAN FRANCISCO", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("State", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("State", reader.Name); Assert.Equal("CA", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Zip", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Zip", reader.Name); Assert.Equal("94107", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Country", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Country", reader.Name); Assert.Equal("US", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("precision", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("precision", reader.Name); Assert.Equal("zip", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Latitude", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Latitude", reader.Name); Assert.Equal("37.371991", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Longitude", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Longitude", reader.Name); Assert.Equal("-122.026020", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Address", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Address", reader.Name); Assert.Equal(string.Empty, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("City", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("City", reader.Name); Assert.Equal("SUNNYVALE", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("State", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("State", reader.Name); Assert.Equal("CA", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Zip", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Zip", reader.Name); Assert.Equal("94085", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Country", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Country", reader.Name); Assert.Equal("US", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); } } }
public void op_Read_whenPropertyValueObject(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Name", reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.Equal("Name", reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Number", reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Number", reader.Name); Assert.Equal("123", reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); } } } }
public void op_Read_whenPropertyArrayString(string values, string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Letters", reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.Equal("Letters", reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); foreach (var value in values.Split(',')) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Null(reader.Name); Assert.Equal(value, reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); } Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.Null(reader.Name); Assert.Null(reader.Value); Assert.False(reader.IsEmptyArray); Assert.False(reader.IsEmptyObject); } } } }
public void read_nested_arrays(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("list", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.Equal("list", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.TrueValue, reader.NodeType); Assert.Null(reader.Name); Assert.Equal("true", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("1", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("2", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("3", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.FalseValue, reader.NodeType); Assert.Null(reader.Name); Assert.Equal("false", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); } } } }
public void op_ReadJson_JsonReader_whenNestedArrayBug(string json) { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(json); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { reader.Read(); var obj = new JsonObject(); obj.ReadJson(reader); Assert.Equal("123", obj.Array("list").Object(0).Number("count").Value); Assert.True(obj.Array("list").Boolean(1)); } } } }
public void op_ReadJson_JsonReader_whenNotObjectNode() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write("{}"); writer.Flush(); stream.Position = 0; using (var reader = new JsonReader(stream)) { // ReSharper disable AccessToDisposedClosure Assert.Throws<InvalidOperationException>(() => new JsonObject().ReadJson(reader)); // ReSharper restore AccessToDisposedClosure } } } }
public void op_Read_whenRfc4627example1() { var file = new FileInfo("rfc4627 example 1.json"); using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new JsonReader(stream)) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Image", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.Equal("Image", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Width", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Width", reader.Name); Assert.Equal("800", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Height", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Height", reader.Name); Assert.Equal("600", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Title", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Title", reader.Name); Assert.Equal("View from 15th Floor", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Thumbnail", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Object, reader.NodeType); Assert.Equal("Thumbnail", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Url", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Url", reader.Name); Assert.Equal("http://www.example.com/image/481989943", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Height", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Equal("Height", reader.Name); Assert.Equal("125", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("Width", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.StringValue, reader.NodeType); Assert.Equal("Width", reader.Name); Assert.Equal("100", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Name, reader.NodeType); Assert.Equal("IDs", reader.Name); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.Array, reader.NodeType); Assert.Equal("IDs", reader.Name); foreach (var value in "116,943,234,38793".Split(',')) { Assert.True(reader.Read()); Assert.Equal(JsonNodeType.NumberValue, reader.NodeType); Assert.Null(reader.Name); Assert.Equal(value, reader.Value); } Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndArray, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); Assert.True(reader.Read()); Assert.Equal(JsonNodeType.EndObject, reader.NodeType); } } }