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); } } }
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); } } }
public void op_NullPair_stringNull() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { // ReSharper disable AccessToDisposedClosure Assert.Throws <ArgumentNullException>(() => writer.NullPair(null)); // ReSharper restore AccessToDisposedClosure } } }
public virtual void WriteJson(JsonWriter writer) { if (null == writer) { throw new ArgumentNullException("writer"); } foreach (var pair in this) { var obj = pair.Value as IJsonSerializable; if (null != obj) { writer.Pair(pair.Name, obj); continue; } var number = pair.Value as JsonNumber; if (null != number) { writer.NumberPair(pair.Name, number.Value); continue; } var str = pair.Value as JsonString; if (null != str) { writer.Pair(pair.Name, str.Value); continue; } var array = pair.Value as JsonArray; if (null != array) { WriteJsonArray(writer, pair.Name, array); continue; } if (pair.Value is JsonNull) { writer.NullPair(pair.Name); } else if (pair.Value is JsonTrue) { writer.Pair(pair.Name, true); } else if (pair.Value is JsonFalse) { writer.Pair(pair.Name, false); } } }
public void op_NullPair_string_whenArrayParent() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); writer.Array("example"); // ReSharper disable AccessToDisposedClosure Assert.Throws <InvalidOperationException>(() => writer.NullPair("name")); // ReSharper restore AccessToDisposedClosure } } }
public void op_NullPair_string(string expected) { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); writer.NullPair("example"); writer.EndObject(); } using (var reader = new StreamReader(stream)) { var actual = reader.ReadToEnd(); Assert.Equal(expected, actual); } } }
public void op_NullPair_string_whenArrayParent() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); writer.Array("example"); // ReSharper disable AccessToDisposedClosure Assert.Throws<InvalidOperationException>(() => writer.NullPair("name")); // ReSharper restore AccessToDisposedClosure } } }
public void op_NullPair_stringNull() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { // ReSharper disable AccessToDisposedClosure Assert.Throws<ArgumentNullException>(() => writer.NullPair(null)); // ReSharper restore AccessToDisposedClosure } } }
private static bool JsonSerializeBaseClassLibraryType(this JsonWriter writer, string name, object value) { if (null == value) { if (null == name) { writer.ArrayNull(); } else { writer.NullPair(name); } return(true); } if (writer.JsonSerializeBuiltInType(name, value)) { return(true); } if (value is DateTime) { if (null == name) { writer.ArrayValue((DateTime)value); } else { writer.Pair(name, (DateTime)value); } return(true); } if (value is DateTimeOffset) { if (null == name) { writer.ArrayValue((DateTimeOffset)value); } else { writer.Pair(name, (DateTimeOffset)value); } return(true); } if (value is Guid) { if (null == name) { writer.ArrayValue((Guid)value); } else { writer.Pair(name, (Guid)value); } return(true); } if (value is TimeSpan) { if (null == name) { writer.ArrayValue((TimeSpan)value); } else { writer.Pair(name, (TimeSpan)value); } return(true); } if (value is Enum) { if (null == name) { writer.ArrayValue(value.ToString()); } else { writer.Pair(name, value.ToString()); } return(true); } var uri = value as Uri; if (null != uri) { if (null == name) { writer.ArrayValue(uri); } else { writer.Pair(name, uri); } return(true); } if (0 == value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Length) { if (null == name) { writer.ArrayValue(value.ToString()); } else { writer.Pair(name, value.ToString()); } return(true); } return(false); }