-Added JsonSerializer argument to JsonConverter.ReadJson and JsonConverter.WriteJson. Needed because serializer now has state
-Added CustomCreationConverter -Added IsReference to JsonContainerAttribute -Added PreserveReferencesHandling to JsonSerializer -Added IReferenceResolver to JsonSerializer -Split internals of JsonSerializer into JsonSerializerWriter (serializes) and JsonSerializerReader (deserializes) -Moved serialize stack from JsonWriter (yuck) to JsonSerializerWriter (yay) -Added FromObject overload with JsonSerializer argument for cases when state is needed
This commit is contained in:
@@ -762,11 +762,12 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void SerializeArrayAsArrayList()
|
||||
{
|
||||
string jsonText = @"[3, ""somestring"",[1,2,3]]";
|
||||
string jsonText = @"[3, ""somestring"",[1,2,3],{}]";
|
||||
ArrayList o = JsonConvert.DeserializeObject<ArrayList>(jsonText);
|
||||
|
||||
Assert.AreEqual(3, o.Count);
|
||||
Assert.AreEqual(4, o.Count);
|
||||
Assert.AreEqual(3, ((JArray)o[2]).Count);
|
||||
Assert.AreEqual(0, ((JObject)o[3]).Count);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1617,5 +1618,738 @@ keyword such as type of business.""
|
||||
Assert.AreEqual("_name: Jeff, _age: 26, Rank: 10, JsonTitle: Dr", cc.ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void PopulatePerson()
|
||||
{
|
||||
Person p = new Person();
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Populate(new StringReader(@"{""Name"":""James""}"), p);
|
||||
|
||||
Assert.AreEqual("James", p.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PopulateListOfPeople()
|
||||
{
|
||||
List<Person> p = new List<Person>();
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Populate(new StringReader(@"[{""Name"":""James""},{""Name"":""Jim""}]"), p);
|
||||
|
||||
Assert.AreEqual(2, p.Count);
|
||||
Assert.AreEqual("James", p[0].Name);
|
||||
Assert.AreEqual("Jim", p[1].Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PopulateDictionary()
|
||||
{
|
||||
Dictionary<string, string> p = new Dictionary<string, string>();
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Populate(new StringReader(@"{""Name"":""James""}"), p);
|
||||
|
||||
Assert.AreEqual(1, p.Count);
|
||||
Assert.AreEqual("James", p["Name"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeEmployeeReference()
|
||||
{
|
||||
Employee mikeManager = new Employee
|
||||
{
|
||||
Name = "Mike Manager"
|
||||
};
|
||||
Employee joeUser = new Employee
|
||||
{
|
||||
Name = "Joe User",
|
||||
Manager = mikeManager
|
||||
};
|
||||
|
||||
List<Employee> employees = new List<Employee>
|
||||
{
|
||||
mikeManager,
|
||||
joeUser
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(employees, Formatting.Indented);
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""Mike Manager"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""Name"": ""Joe User"",
|
||||
""Manager"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEmployeeReference()
|
||||
{
|
||||
string json = @"[
|
||||
{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""Mike Manager"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""Name"": ""Joe User"",
|
||||
""Manager"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
]";
|
||||
|
||||
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(json);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeCircularReference()
|
||||
{
|
||||
CircularReferenceClass c1 = new CircularReferenceClass {Name = "c1"};
|
||||
CircularReferenceClass c2 = new CircularReferenceClass {Name = "c2"};
|
||||
CircularReferenceClass c3 = new CircularReferenceClass {Name = "c3"};
|
||||
|
||||
c1.Child = c2;
|
||||
c2.Child = c3;
|
||||
c3.Child = c1;
|
||||
|
||||
string json = JsonConvert.SerializeObject(c1, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""c1"",
|
||||
""Child"": {
|
||||
""$id"": ""2"",
|
||||
""Name"": ""c2"",
|
||||
""Child"": {
|
||||
""$id"": ""3"",
|
||||
""Name"": ""c3"",
|
||||
""Child"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeCircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""c1"",
|
||||
""Child"": {
|
||||
""$id"": ""2"",
|
||||
""Name"": ""c2"",
|
||||
""Child"": {
|
||||
""$id"": ""3"",
|
||||
""Name"": ""c3"",
|
||||
""Child"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
CircularReferenceClass c1 =
|
||||
JsonConvert.DeserializeObject<CircularReferenceClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects
|
||||
});
|
||||
|
||||
Assert.AreEqual("c1", c1.Name);
|
||||
Assert.AreEqual("c2", c1.Child.Name);
|
||||
Assert.AreEqual("c3", c1.Child.Child.Name);
|
||||
Assert.AreEqual("c1", c1.Child.Child.Child.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeReferenceInList()
|
||||
{
|
||||
Employee e1 = new Employee { Name = "e1" };
|
||||
Employee e2 = new Employee { Name = "e2" };
|
||||
|
||||
List<Employee> employees = new List<Employee> { e1, e2, e1, e2 };
|
||||
|
||||
string json = JsonConvert.SerializeObject(employees, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""e1"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""Name"": ""e2"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$ref"": ""1""
|
||||
},
|
||||
{
|
||||
""$ref"": ""2""
|
||||
}
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeReferenceInList()
|
||||
{
|
||||
string json = @"[
|
||||
{
|
||||
""$id"": ""1"",
|
||||
""Name"": ""e1"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""Name"": ""e2"",
|
||||
""Manager"": null
|
||||
},
|
||||
{
|
||||
""$ref"": ""1""
|
||||
},
|
||||
{
|
||||
""$ref"": ""2""
|
||||
}
|
||||
]";
|
||||
|
||||
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(json);
|
||||
Assert.AreEqual(4, employees.Count);
|
||||
|
||||
Assert.AreEqual("e1", employees[0].Name);
|
||||
Assert.AreEqual("e2", employees[1].Name);
|
||||
Assert.AreEqual("e1", employees[2].Name);
|
||||
Assert.AreEqual("e2", employees[3].Name);
|
||||
|
||||
Assert.AreEqual(employees[0], employees[2]);
|
||||
Assert.AreEqual(employees[1], employees[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeReferenceInDictionary()
|
||||
{
|
||||
Employee e1 = new Employee {Name = "e1"};
|
||||
Employee e2 = new Employee {Name = "e2"};
|
||||
|
||||
Dictionary<string, Employee> employees = new Dictionary<string, Employee>
|
||||
{
|
||||
{"One", e1},
|
||||
{"Two", e2},
|
||||
{"Three", e1},
|
||||
{"Four", e2}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(employees, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""One"": {
|
||||
""$id"": ""1"",
|
||||
""Name"": ""e1"",
|
||||
""Manager"": null
|
||||
},
|
||||
""Two"": {
|
||||
""$id"": ""2"",
|
||||
""Name"": ""e2"",
|
||||
""Manager"": null
|
||||
},
|
||||
""Three"": {
|
||||
""$ref"": ""1""
|
||||
},
|
||||
""Four"": {
|
||||
""$ref"": ""2""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeReferenceInDictionary()
|
||||
{
|
||||
string json = @"{
|
||||
""One"": {
|
||||
""$id"": ""1"",
|
||||
""Name"": ""e1"",
|
||||
""Manager"": null
|
||||
},
|
||||
""Two"": {
|
||||
""$id"": ""2"",
|
||||
""Name"": ""e2"",
|
||||
""Manager"": null
|
||||
},
|
||||
""Three"": {
|
||||
""$ref"": ""1""
|
||||
},
|
||||
""Four"": {
|
||||
""$ref"": ""2""
|
||||
}
|
||||
}";
|
||||
|
||||
Dictionary<string, Employee> employees = JsonConvert.DeserializeObject<Dictionary<string, Employee>>(json);
|
||||
Assert.AreEqual(4, employees.Count);
|
||||
|
||||
Employee e1 = employees["One"];
|
||||
Employee e2 = employees["Two"];
|
||||
|
||||
Assert.AreEqual("e1", e1.Name);
|
||||
Assert.AreEqual("e2", e2.Name);
|
||||
|
||||
Assert.AreEqual(e1, employees["Three"]);
|
||||
Assert.AreEqual(e2, employees["Four"]);
|
||||
}
|
||||
|
||||
public class CircularDictionary : Dictionary<string, CircularDictionary>
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
|
||||
public void SerializeCircularDictionarysError()
|
||||
{
|
||||
CircularDictionary circularDictionary = new CircularDictionary();
|
||||
circularDictionary.Add("other", new CircularDictionary { { "blah", null } });
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
JsonConvert.SerializeObject(circularDictionary, Formatting.Indented);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeCircularDictionarysIgnore()
|
||||
{
|
||||
CircularDictionary circularDictionary = new CircularDictionary();
|
||||
circularDictionary.Add("other", new CircularDictionary { { "blah", null } });
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
string json = JsonConvert.SerializeObject(circularDictionary, Formatting.Indented,
|
||||
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""other"": {
|
||||
""blah"": null
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDictionarysWithPreserveObjectReferences()
|
||||
{
|
||||
CircularDictionary circularDictionary = new CircularDictionary();
|
||||
circularDictionary.Add("other", new CircularDictionary { {"blah", null }});
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
string json = JsonConvert.SerializeObject(circularDictionary, Formatting.Indented,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""$id"": ""1"",
|
||||
""other"": {
|
||||
""$id"": ""2"",
|
||||
""blah"": null
|
||||
},
|
||||
""self"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDictionarysWithPreserveObjectReferences()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""other"": {
|
||||
""$id"": ""2"",
|
||||
""blah"": null
|
||||
},
|
||||
""self"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}";
|
||||
|
||||
CircularDictionary circularDictionary = JsonConvert.DeserializeObject<CircularDictionary>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All
|
||||
});
|
||||
|
||||
Assert.AreEqual(2, circularDictionary.Count);
|
||||
Assert.AreEqual(1, circularDictionary["other"].Count);
|
||||
Assert.AreEqual(circularDictionary, circularDictionary["self"]);
|
||||
}
|
||||
|
||||
public class CircularList : List<CircularList>
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
|
||||
public void SerializeCircularListsError()
|
||||
{
|
||||
CircularList circularList = new CircularList();
|
||||
circularList.Add(null);
|
||||
circularList.Add(new CircularList { null });
|
||||
circularList.Add(new CircularList { new CircularList { circularList } });
|
||||
|
||||
JsonConvert.SerializeObject(circularList, Formatting.Indented);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeCircularListsIgnore()
|
||||
{
|
||||
CircularList circularList = new CircularList();
|
||||
circularList.Add(null);
|
||||
circularList.Add(new CircularList { null });
|
||||
circularList.Add(new CircularList { new CircularList { circularList } });
|
||||
|
||||
string json = JsonConvert.SerializeObject(circularList,
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
null,
|
||||
[
|
||||
null
|
||||
],
|
||||
[
|
||||
[]
|
||||
]
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeListsWithPreserveObjectReferences()
|
||||
{
|
||||
CircularList circularList = new CircularList();
|
||||
circularList.Add(null);
|
||||
circularList.Add(new CircularList { null });
|
||||
circularList.Add(new CircularList { new CircularList { circularList }});
|
||||
|
||||
string json = JsonConvert.SerializeObject(circularList, Formatting.Indented,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
|
||||
WriteEscapedJson(json);
|
||||
Assert.AreEqual(@"{
|
||||
""$id"": ""1"",
|
||||
""value"": [
|
||||
null,
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""value"": [
|
||||
null
|
||||
]
|
||||
},
|
||||
{
|
||||
""$id"": ""3"",
|
||||
""value"": [
|
||||
{
|
||||
""$id"": ""4"",
|
||||
""value"": [
|
||||
{
|
||||
""$ref"": ""1""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeListsWithPreserveObjectReferences()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""value"": [
|
||||
null,
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""value"": [
|
||||
null
|
||||
]
|
||||
},
|
||||
{
|
||||
""$id"": ""3"",
|
||||
""value"": [
|
||||
{
|
||||
""$id"": ""4"",
|
||||
""value"": [
|
||||
{
|
||||
""$ref"": ""1""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
CircularList circularList = JsonConvert.DeserializeObject<CircularList>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
|
||||
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]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Cannot preserve reference to array or readonly list: System.String[][]")]
|
||||
public void DeserializeArraysWithPreserveObjectReferences()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""value"": [
|
||||
null,
|
||||
{
|
||||
""$id"": ""2"",
|
||||
""value"": [
|
||||
null
|
||||
]
|
||||
},
|
||||
{
|
||||
""$id"": ""3"",
|
||||
""value"": [
|
||||
{
|
||||
""$id"": ""4"",
|
||||
""value"": [
|
||||
{
|
||||
""$ref"": ""1""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unexpected end when deserializing object.")]
|
||||
public void UnexpectedEnd()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"":";
|
||||
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
}
|
||||
|
||||
public class DictionaryInterfaceClass
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public IDictionary<string, int> Dictionary { get; set; }
|
||||
public ICollection<int> Collection { get; set; }
|
||||
public Employee Employee { get; set; }
|
||||
public object Random { get; set; }
|
||||
|
||||
public DictionaryInterfaceClass()
|
||||
{
|
||||
Dictionary = new Dictionary<string, int>
|
||||
{
|
||||
{ "existing", 1 }
|
||||
};
|
||||
Collection = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3
|
||||
};
|
||||
Employee = new Employee
|
||||
{
|
||||
Name = "EmployeeName!"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDictionaryInterface()
|
||||
{
|
||||
string json = @"{
|
||||
""Name"": ""Name!"",
|
||||
""Dictionary"": {
|
||||
""Item"": 11
|
||||
}
|
||||
}";
|
||||
|
||||
DictionaryInterfaceClass c = JsonConvert.DeserializeObject<DictionaryInterfaceClass>(json,
|
||||
new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace });
|
||||
Assert.AreEqual("Name!", c.Name);
|
||||
Assert.AreEqual(1, c.Dictionary.Count);
|
||||
Assert.AreEqual(11, c.Dictionary["Item"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDictionaryInterfaceWithExistingValues()
|
||||
{
|
||||
string json = @"{
|
||||
""Random"": {
|
||||
""blah"": 1
|
||||
},
|
||||
""Name"": ""Name!"",
|
||||
""Dictionary"": {
|
||||
""Item"": 11,
|
||||
""Item1"": 12
|
||||
},
|
||||
""Collection"": [
|
||||
999
|
||||
],
|
||||
""Employee"": {
|
||||
""Manager"": {
|
||||
""Name"": ""ManagerName!""
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
DictionaryInterfaceClass c = JsonConvert.DeserializeObject<DictionaryInterfaceClass>(json,
|
||||
new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Reuse });
|
||||
|
||||
Assert.AreEqual("Name!", c.Name);
|
||||
Assert.AreEqual(3, c.Dictionary.Count);
|
||||
Assert.AreEqual(11, c.Dictionary["Item"]);
|
||||
Assert.AreEqual(1, c.Dictionary["existing"]);
|
||||
Assert.AreEqual(4, c.Collection.Count);
|
||||
Assert.AreEqual(1, c.Collection.ElementAt(0));
|
||||
Assert.AreEqual(999, c.Collection.ElementAt(3));
|
||||
Assert.AreEqual("EmployeeName!", c.Employee.Name);
|
||||
Assert.AreEqual("ManagerName!", c.Employee.Manager.Name);
|
||||
Assert.IsNotNull(c.Random);
|
||||
}
|
||||
|
||||
public class CircularReferenceClassConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
CircularReferenceClass circularReferenceClass = (CircularReferenceClass) value;
|
||||
|
||||
string reference = serializer.ReferenceResolver.ResolveReference(circularReferenceClass);
|
||||
|
||||
JObject me = new JObject();
|
||||
me["$id"] = new JValue(reference);
|
||||
me["$type"] = new JValue(value.GetType().Name);
|
||||
me["Name"] = new JValue(circularReferenceClass.Name);
|
||||
|
||||
JObject o = JObject.FromObject(circularReferenceClass.Child, serializer);
|
||||
me["Child"] = o;
|
||||
|
||||
me.WriteTo(writer);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
{
|
||||
JObject o = JObject.Load(reader);
|
||||
string id = (string)o["$id"];
|
||||
if (id != null)
|
||||
{
|
||||
CircularReferenceClass circularReferenceClass = new CircularReferenceClass();
|
||||
serializer.Populate(o.CreateReader(), circularReferenceClass);
|
||||
return circularReferenceClass;
|
||||
}
|
||||
else
|
||||
{
|
||||
string reference = (string)o["$ref"];
|
||||
return serializer.ReferenceResolver.ResolveObject(reference);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(CircularReferenceClass));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeCircularReferencesWithConverter()
|
||||
{
|
||||
CircularReferenceClass c1 = new CircularReferenceClass { Name = "c1" };
|
||||
CircularReferenceClass c2 = new CircularReferenceClass { Name = "c2" };
|
||||
CircularReferenceClass c3 = new CircularReferenceClass { Name = "c3" };
|
||||
|
||||
c1.Child = c2;
|
||||
c2.Child = c3;
|
||||
c3.Child = c1;
|
||||
|
||||
string json = JsonConvert.SerializeObject(c1, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
||||
Converters = new List<JsonConverter> { new CircularReferenceClassConverter() }
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""$id"": ""1"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c1"",
|
||||
""Child"": {
|
||||
""$id"": ""2"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c2"",
|
||||
""Child"": {
|
||||
""$id"": ""3"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c3"",
|
||||
""Child"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeCircularReferencesWithConverter()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c1"",
|
||||
""Child"": {
|
||||
""$id"": ""2"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c2"",
|
||||
""Child"": {
|
||||
""$id"": ""3"",
|
||||
""$type"": ""CircularReferenceClass"",
|
||||
""Name"": ""c3"",
|
||||
""Child"": {
|
||||
""$ref"": ""1""
|
||||
}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
CircularReferenceClass c1 = JsonConvert.DeserializeObject<CircularReferenceClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
||||
Converters = new List<JsonConverter> { new CircularReferenceClassConverter() }
|
||||
});
|
||||
|
||||
Assert.AreEqual("c1", c1.Name);
|
||||
Assert.AreEqual("c2", c1.Child.Name);
|
||||
Assert.AreEqual("c3", c1.Child.Child.Name);
|
||||
Assert.AreEqual("c1", c1.Child.Child.Child.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user