-Added FloatFormatHandling to JsonWriter/JsonSerializer/JsonSerializerSettings
-Added DateFormatString to JsonWriter/JsonSerializer/JsonSerializerSettings -Changed StringEnumConverter to convert an empty string to null for nullable enums -Changed NaN and Infinity floating point values to be serialized as strings by default
This commit is contained in:
@@ -321,5 +321,18 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
EnumContainer<FlagsTestEnum> c = JsonConvert.DeserializeObject<EnumContainer<FlagsTestEnum>>(json, new StringEnumConverter { CamelCaseText = true });
|
||||
Assert.AreEqual(FlagsTestEnum.First | FlagsTestEnum.Second, c.Enum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEmptyStringIntoNullable()
|
||||
{
|
||||
string json = @"{
|
||||
""StoreColor"": ""Red"",
|
||||
""NullableStoreColor1"": ""White"",
|
||||
""NullableStoreColor2"": """"
|
||||
}";
|
||||
|
||||
EnumClass c = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
|
||||
Assert.IsNull(c.NullableStoreColor2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,7 +389,7 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers()
|
||||
public void FloatingPointNonFiniteNumbers_Symbol()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
@@ -397,6 +397,7 @@ namespace Newtonsoft.Json.Tests
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
@@ -423,6 +424,115 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers_Zero()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Zero;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
jsonWriter.WriteValue(double.PositiveInfinity);
|
||||
jsonWriter.WriteValue(double.NegativeInfinity);
|
||||
jsonWriter.WriteValue(float.NaN);
|
||||
jsonWriter.WriteValue(float.PositiveInfinity);
|
||||
jsonWriter.WriteValue(float.NegativeInfinity);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
jsonWriter.Flush();
|
||||
}
|
||||
|
||||
string expected = @"[
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers_String()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.String;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
jsonWriter.WriteValue(double.PositiveInfinity);
|
||||
jsonWriter.WriteValue(double.NegativeInfinity);
|
||||
jsonWriter.WriteValue(float.NaN);
|
||||
jsonWriter.WriteValue(float.PositiveInfinity);
|
||||
jsonWriter.WriteValue(float.NegativeInfinity);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
jsonWriter.Flush();
|
||||
}
|
||||
|
||||
string expected = @"[
|
||||
""NaN"",
|
||||
""Infinity"",
|
||||
""-Infinity"",
|
||||
""NaN"",
|
||||
""Infinity"",
|
||||
""-Infinity""
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers_QuoteChar()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.String;
|
||||
jsonWriter.QuoteChar = '\'';
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
jsonWriter.WriteValue(double.PositiveInfinity);
|
||||
jsonWriter.WriteValue(double.NegativeInfinity);
|
||||
jsonWriter.WriteValue(float.NaN);
|
||||
jsonWriter.WriteValue(float.PositiveInfinity);
|
||||
jsonWriter.WriteValue(float.NegativeInfinity);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
jsonWriter.Flush();
|
||||
}
|
||||
|
||||
string expected = @"[
|
||||
'NaN',
|
||||
'Infinity',
|
||||
'-Infinity',
|
||||
'NaN',
|
||||
'Infinity',
|
||||
'-Infinity'
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteRawInStart()
|
||||
{
|
||||
@@ -432,6 +542,7 @@ namespace Newtonsoft.Json.Tests
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;
|
||||
|
||||
jsonWriter.WriteRaw("[1,2,3,4,5]");
|
||||
jsonWriter.WriteWhitespace(" ");
|
||||
@@ -457,6 +568,7 @@ namespace Newtonsoft.Json.Tests
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
@@ -569,6 +681,8 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
jsonWriter.WriteValue(0.0);
|
||||
@@ -642,6 +756,8 @@ namespace Newtonsoft.Json.Tests
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;
|
||||
|
||||
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
|
||||
|
||||
jsonWriter.Indentation = 5;
|
||||
@@ -936,6 +1052,10 @@ _____'propertyName': NaN
|
||||
writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
|
||||
writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));
|
||||
|
||||
writer.DateFormatString = "yyyy gg";
|
||||
writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
|
||||
writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));
|
||||
|
||||
writer.WriteValue(new byte[] { 1, 2, 3 });
|
||||
writer.WriteValue(TimeSpan.Zero);
|
||||
writer.WriteValue(new Uri("http://www.google.com/"));
|
||||
@@ -948,10 +1068,35 @@ _____'propertyName': NaN
|
||||
'2000-01-01T01:01:01+00:00',
|
||||
'\/Date(946688461000)\/',
|
||||
'\/Date(946688461000+0000)\/',
|
||||
'2000 A.D.',
|
||||
'2000 A.D.',
|
||||
'AQID',
|
||||
'00:00:00',
|
||||
'http://www.google.com/',
|
||||
'00000000-0000-0000-0000-000000000000'
|
||||
]", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Culture()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
writer.Formatting = Formatting.Indented;
|
||||
writer.DateFormatString = "yyyy tt";
|
||||
writer.Culture = new CultureInfo("en-NZ");
|
||||
writer.QuoteChar = '\'';
|
||||
|
||||
writer.WriteStartArray();
|
||||
|
||||
writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
|
||||
writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));
|
||||
|
||||
writer.WriteEnd();
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
'2000 a.m.',
|
||||
'2000 a.m.'
|
||||
]", sw.ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -76,5 +76,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.11.15729")]
|
||||
[assembly: AssemblyFileVersion("4.5.11.15802")]
|
||||
#endif
|
||||
|
||||
@@ -7123,6 +7123,128 @@ Parameter name: value",
|
||||
Assert.AreEqual(TimeSpan.FromHours(9.5), dateTimeOffset.Offset);
|
||||
Assert.AreEqual("07/19/2012 14:30:00 +09:30", dateTimeOffset.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DateFormatString()
|
||||
{
|
||||
IList<object> dates = new List<object>
|
||||
{
|
||||
new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc),
|
||||
new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(1))
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dates, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "yyyy tt",
|
||||
Culture = new CultureInfo("en-NZ")
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
""2000 p.m."",
|
||||
""2000 p.m.""
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonSerializerDateFormatString()
|
||||
{
|
||||
IList<object> dates = new List<object>
|
||||
{
|
||||
new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc),
|
||||
new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(1))
|
||||
};
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(sw);
|
||||
|
||||
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "yyyy tt",
|
||||
Culture = new CultureInfo("en-NZ"),
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
serializer.Serialize(jsonWriter, dates);
|
||||
|
||||
Assert.IsNull(jsonWriter.DateFormatString);
|
||||
Assert.AreEqual(CultureInfo.InvariantCulture, jsonWriter.Culture);
|
||||
Assert.AreEqual(Formatting.None, jsonWriter.Formatting);
|
||||
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
""2000 p.m."",
|
||||
""2000 p.m.""
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonSerializerStringEscapeHandling()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(sw);
|
||||
|
||||
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
serializer.Serialize(jsonWriter, new { html = "<html></html>" });
|
||||
|
||||
Assert.AreEqual(StringEscapeHandling.Default, jsonWriter.StringEscapeHandling);
|
||||
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""html"": ""\u003chtml\u003e\u003c/html\u003e""
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadStringFloatingPointSymbols()
|
||||
{
|
||||
string json = @"[
|
||||
'NaN',
|
||||
'Infinity',
|
||||
'-Infinity'
|
||||
]";
|
||||
|
||||
IList<float> floats = JsonConvert.DeserializeObject<IList<float>>(json);
|
||||
Assert.AreEqual(float.NaN, floats[0]);
|
||||
Assert.AreEqual(float.PositiveInfinity, floats[1]);
|
||||
Assert.AreEqual(float.NegativeInfinity, floats[2]);
|
||||
|
||||
IList<double> doubles = JsonConvert.DeserializeObject<IList<double>>(json);
|
||||
Assert.AreEqual(float.NaN, doubles[0]);
|
||||
Assert.AreEqual(float.PositiveInfinity, doubles[1]);
|
||||
Assert.AreEqual(float.NegativeInfinity, doubles[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultDateStringFormatVsUnsetDateStringFormat()
|
||||
{
|
||||
IDictionary<string, object> dates = new Dictionary<string, object>
|
||||
{
|
||||
{"DateTime-Unspecified", new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Unspecified)},
|
||||
{"DateTime-Utc", new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)},
|
||||
{"DateTime-Local", new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Local)},
|
||||
{"DateTimeOffset-Zero", new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)},
|
||||
{"DateTimeOffset-Plus1", new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(1))},
|
||||
{"DateTimeOffset-Plus15", new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(1.5))}
|
||||
};
|
||||
|
||||
string expected = JsonConvert.SerializeObject(dates, Formatting.Indented);
|
||||
|
||||
Console.WriteLine(expected);
|
||||
|
||||
string actual = JsonConvert.SerializeObject(dates, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = JsonSerializerSettings.DefaultDateFormatString
|
||||
});
|
||||
|
||||
Console.WriteLine(expected);
|
||||
|
||||
Assert.AreEqual(expected, actual);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !NET20
|
||||
|
||||
@@ -1455,6 +1455,27 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(2, (int)o["MyProperty"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void SerializeDeserialize_DictionaryContextContainsGuid_DeserializesItemAsGuid()
|
||||
{
|
||||
const string contextKey = "k1";
|
||||
var someValue = Guid.NewGuid();
|
||||
|
||||
Dictionary<string, Guid> inputContext = new Dictionary<string, Guid>();
|
||||
inputContext.Add(contextKey, someValue);
|
||||
|
||||
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
};
|
||||
string serializedString = JsonConvert.SerializeObject(inputContext, jsonSerializerSettings);
|
||||
|
||||
var deserializedObject = (Dictionary<string, Guid>)JsonConvert.DeserializeObject(serializedString, jsonSerializerSettings);
|
||||
|
||||
Assert.AreEqual(someValue, deserializedObject[contextKey]);
|
||||
}
|
||||
}
|
||||
|
||||
public class Message
|
||||
|
||||
Reference in New Issue
Block a user