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); } } }
private static bool JsonSerializeFloatingPointType(this JsonWriter writer, string name, object value) { if (value is double) { if (null == name) { writer.ArrayValue((double)value); } else { writer.Pair(name, (double)value); } return(true); } if (value is float) { if (null == name) { writer.ArrayValue((float)value); } else { writer.Pair(name, (float)value); } return(true); } return(false); }
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); } } }
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 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); } } }
private static bool JsonSerializeBuiltInType(this JsonWriter writer, string name, object value) { if (writer.JsonSerializeIntegralType(name, value)) { return(true); } if (writer.JsonSerializeFloatingPointType(name, value)) { return(true); } if (value is bool) { if (null == name) { writer.ArrayValue((bool)value); } else { writer.Pair(name, (bool)value); } return(true); } if (value is decimal) { if (null == name) { writer.ArrayValue((decimal)value); } else { writer.Pair(name, (decimal)value); } return(true); } var str = value as string; if (null != str) { if (null == name) { writer.ArrayValue(str); } else { writer.Pair(name, str); } return(true); } return(false); }
public void op_ArrayValue_string_whenParentNotArray() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); // ReSharper disable AccessToDisposedClosure Assert.Throws <InvalidOperationException>(() => writer.ArrayValue(string.Empty)); // ReSharper restore AccessToDisposedClosure } } }
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); } } }
public void op_ArrayValue_DateTime(string expected) { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); writer.Array("example"); writer.ArrayValue(new DateTime(2011, 7, 14, 19, 43, 37, DateTimeKind.Utc)); writer.EndArray(); writer.EndObject(); } using (var reader = new StreamReader(stream)) { var actual = reader.ReadToEnd(); Assert.Equal(expected, actual); } } }
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); }
private static bool JsonSerializeIntegralType(this JsonWriter writer, string name, object value) { if (value is byte) { if (null == name) { writer.ArrayValue((byte)value); } else { writer.Pair(name, (byte)value); } return(true); } if (value is char) { if (null == name) { writer.ArrayValue((char)value); } else { writer.Pair(name, (char)value); } return(true); } if (value is short) { if (null == name) { writer.ArrayValue((short)value); } else { writer.Pair(name, (short)value); } return(true); } if (value is int) { if (null == name) { writer.ArrayValue((int)value); } else { writer.Pair(name, (int)value); } return(true); } if (value is long) { if (null == name) { writer.ArrayValue((long)value); } else { writer.Pair(name, (long)value); } return(true); } return(false); }
public void op_ArrayValue_string_whenParentNotArray() { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); // ReSharper disable AccessToDisposedClosure Assert.Throws<InvalidOperationException>(() => writer.ArrayValue(string.Empty)); // ReSharper restore AccessToDisposedClosure } } }
private static void WriteJsonArray(JsonWriter writer, string name, JsonArray value) { if (null == name) { writer.Array(); } else { writer.Array(name); } foreach (var item in value.Values) { var obj = item as IJsonSerializable; if (null != obj) { writer.Object(); obj.WriteJson(writer); writer.EndObject(); continue; } var number = item as JsonNumber; if (null != number) { writer.ArrayNumber(number.Value); continue; } var str = item as JsonString; if (null != str) { writer.ArrayValue(str.Value); continue; } var array = item as JsonArray; if (null != array) { WriteJsonArray(writer, null, array); continue; } if (item is JsonNull) { writer.ArrayNull(); } else if (item is JsonTrue) { writer.ArrayValue(true); } else if (item is JsonFalse) { writer.ArrayValue(false); } } writer.EndArray(); }
public void op_ArrayValue_string(string value, string expected) { using (var stream = new MemoryStream()) { using (var writer = new JsonWriter(stream)) { writer.Object(); writer.Array("example"); writer.ArrayValue(value); writer.EndArray(); writer.EndObject(); } using (var reader = new StreamReader(stream)) { var actual = reader.ReadToEnd(); Assert.Equal(expected, actual); } } }