-Added support for MS format JSON dates to ReadAsDateTime
-Fixed parsing certain JSON with comments into a JObject -Tests
This commit is contained in:
@@ -54,6 +54,136 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
});
|
||||
}
|
||||
|
||||
#if !NETFX_CORE
|
||||
[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,
|
||||
SpecialPropertyHandling = SpecialPropertyHandling.ReadAhead
|
||||
};
|
||||
string serializedString = JsonConvert.SerializeObject(inputContext, jsonSerializerSettings);
|
||||
|
||||
Console.WriteLine(serializedString);
|
||||
|
||||
var deserializedObject = (Dictionary<string, Guid>)JsonConvert.DeserializeObject(serializedString, jsonSerializerSettings);
|
||||
|
||||
Assert.AreEqual(someValue, deserializedObject[contextKey]);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DeserializeGuid()
|
||||
{
|
||||
Item expected = new Item()
|
||||
{
|
||||
SourceTypeID = new Guid("d8220a4b-75b1-4b7a-8112-b7bdae956a45"),
|
||||
BrokerID = new Guid("951663c4-924e-4c86-a57a-7ed737501dbd"),
|
||||
Latitude = 33.657145,
|
||||
Longitude = -117.766684,
|
||||
TimeStamp = new DateTime(2000, 3, 1, 23, 59, 59, DateTimeKind.Utc),
|
||||
Payload = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
|
||||
};
|
||||
|
||||
string jsonString = JsonConvert.SerializeObject(expected, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""SourceTypeID"": ""d8220a4b-75b1-4b7a-8112-b7bdae956a45"",
|
||||
""BrokerID"": ""951663c4-924e-4c86-a57a-7ed737501dbd"",
|
||||
""Latitude"": 33.657145,
|
||||
""Longitude"": -117.766684,
|
||||
""TimeStamp"": ""2000-03-01T23:59:59Z"",
|
||||
""Payload"": {
|
||||
""$type"": ""System.Byte[], mscorlib"",
|
||||
""$value"": ""AAECAwQFBgcICQ==""
|
||||
}
|
||||
}", jsonString);
|
||||
|
||||
Item actual = JsonConvert.DeserializeObject<Item>(jsonString, new JsonSerializerSettings
|
||||
{
|
||||
SpecialPropertyHandling = SpecialPropertyHandling.ReadAhead
|
||||
});
|
||||
|
||||
Assert.AreEqual(new Guid("d8220a4b-75b1-4b7a-8112-b7bdae956a45"), actual.SourceTypeID);
|
||||
Assert.AreEqual(new Guid("951663c4-924e-4c86-a57a-7ed737501dbd"), actual.BrokerID);
|
||||
byte[] bytes = (byte[])actual.Payload;
|
||||
CollectionAssert.AreEquivalent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, bytes);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeListsWithPreserveObjectReferences()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""$values"": [
|
||||
null,
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""$values"": [
|
||||
null
|
||||
]
|
||||
},
|
||||
{
|
||||
""$id"": ""3"",
|
||||
""$values"": [
|
||||
{
|
||||
""$id"": ""4"",
|
||||
""$values"": [
|
||||
{
|
||||
""$ref"": ""1""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
PreserveReferencesHandlingTests.CircularList circularList = JsonConvert.DeserializeObject<PreserveReferencesHandlingTests.CircularList>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
SpecialPropertyHandling = SpecialPropertyHandling.ReadAhead
|
||||
});
|
||||
|
||||
Assert.AreEqual(3, circularList.Count);
|
||||
Assert.AreEqual(null, circularList[0]);
|
||||
Assert.AreEqual(1, circularList[1].Count);
|
||||
Assert.AreEqual(1, circularList[2].Count);
|
||||
Assert.AreEqual(1, circularList[2][0].Count);
|
||||
Assert.IsTrue(ReferenceEquals(circularList, circularList[2][0][0]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeTypeNameOnly()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""$type"": ""Newtonsoft.Json.Tests.TestObjects.Employee"",
|
||||
""Name"": ""Name!"",
|
||||
""Manager"": null
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
SpecialPropertyHandling = SpecialPropertyHandling.ReadAhead
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRefNull()
|
||||
{
|
||||
@@ -67,7 +197,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
child.Add("Name", "Isabell");
|
||||
child.Add("Father", reference);
|
||||
|
||||
string json = JsonConvert.SerializeObject(child);
|
||||
string json = JsonConvert.SerializeObject(child, Formatting.Indented);
|
||||
|
||||
Console.WriteLine(json);
|
||||
|
||||
@@ -110,6 +240,40 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(employees[0], employees[1].Manager);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeFromJToken()
|
||||
{
|
||||
string json = @"[
|
||||
{
|
||||
""Name"": ""Mike Manager"",
|
||||
""$id"": ""1"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""Name"": ""Joe User"",
|
||||
""$id"": ""2"",
|
||||
""Manager"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
]";
|
||||
|
||||
JToken t1 = JToken.Parse(json);
|
||||
JToken t2 = t1.CloneToken();
|
||||
|
||||
List<EmployeeReference> employees = t1.ToObject<List<EmployeeReference>>(JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
SpecialPropertyHandling = SpecialPropertyHandling.ReadAhead
|
||||
}));
|
||||
|
||||
Assert.AreEqual(2, employees.Count);
|
||||
Assert.AreEqual("Mike Manager", employees[0].Name);
|
||||
Assert.AreEqual("Joe User", employees[1].Name);
|
||||
Assert.AreEqual(employees[0], employees[1].Manager);
|
||||
|
||||
Assert.IsTrue(JToken.DeepEquals(t1, t2));
|
||||
}
|
||||
|
||||
#if !(PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void DeserializeGenericObjectListWithTypeName()
|
||||
|
||||
Reference in New Issue
Block a user