Add JsonCloneSettings to disable copy annotations (#2757)
Co-authored-by: Sneha Shivakumar <snshivakumar@microsoft.com>
This commit is contained in:
co-authored by
Sneha Shivakumar
parent
d0a328e8a4
commit
b13717a1c1
@@ -303,11 +303,140 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(0, o2.Annotations<Version>().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedAnnotationsAreCopied()
|
||||
{
|
||||
Version version = new Version(1, 2, 3, 4);
|
||||
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("string!");
|
||||
o.AddAnnotation(version);
|
||||
|
||||
JValue v = new JValue(true);
|
||||
v.AddAnnotation("string!");
|
||||
v.AddAnnotation(version);
|
||||
|
||||
o["Item1"] = v;
|
||||
|
||||
JObject o2 = (JObject)o.DeepClone();
|
||||
Assert.AreEqual("string!", o2.Annotation<string>());
|
||||
Assert.AreEqual(version, o2.Annotation<Version>());
|
||||
|
||||
JValue v2 = (JValue)o2["Item1"];
|
||||
Assert.AreEqual("string!", v2.Annotation<string>());
|
||||
Assert.AreEqual(version, v2.Annotation<Version>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedAnnotationsAreCopiedWithDefault()
|
||||
{
|
||||
Version version = new Version(1, 2, 3, 4);
|
||||
JsonCloneSettings settings = new JsonCloneSettings();
|
||||
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("string!");
|
||||
o.AddAnnotation(version);
|
||||
|
||||
JValue v = new JValue(true);
|
||||
v.AddAnnotation("string!");
|
||||
v.AddAnnotation(version);
|
||||
|
||||
o["Item1"] = v;
|
||||
|
||||
JObject o2 = (JObject)o.DeepClone(settings);
|
||||
Assert.AreEqual("string!", o2.Annotation<string>());
|
||||
Assert.AreEqual(version, o2.Annotation<Version>());
|
||||
|
||||
JValue v2 = (JValue)o2["Item1"];
|
||||
Assert.AreEqual("string!", v2.Annotation<string>());
|
||||
Assert.AreEqual(version, v2.Annotation<Version>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedAnnotationsAreNotCopiedWithSettingsCopyAnnotationsFalse()
|
||||
{
|
||||
Version version = new Version(1, 2, 3, 4);
|
||||
JsonCloneSettings settings = new JsonCloneSettings() { CopyAnnotations = false };
|
||||
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("string!");
|
||||
o.AddAnnotation(version);
|
||||
|
||||
JValue v = new JValue(true);
|
||||
v.AddAnnotation("string!");
|
||||
v.AddAnnotation(version);
|
||||
|
||||
o["Item1"] = v;
|
||||
|
||||
JObject o2 = (JObject)o.DeepClone(settings);
|
||||
Assert.IsNull(o2.Annotation<string>());
|
||||
Assert.AreEqual(0, o2.Annotations<Version>().Count());
|
||||
|
||||
JValue v2 = (JValue)o2["Item1"];
|
||||
Assert.IsNull(v2.Annotation<string>());
|
||||
Assert.AreEqual(0, v2.Annotations<Version>().Count());
|
||||
}
|
||||
|
||||
private void AssertCloneCopy<T>(JToken t, T annotation) where T : class
|
||||
{
|
||||
Assert.AreEqual(annotation, t.DeepClone().Annotation<T>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleAnnotationsAreNotCopiedWithSetting()
|
||||
{
|
||||
Version version = new Version(1, 2, 3, 4);
|
||||
JsonCloneSettings settings = new JsonCloneSettings() { CopyAnnotations = false };
|
||||
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("string!");
|
||||
o.AddAnnotation(version);
|
||||
|
||||
JObject o2 = (JObject)o.DeepClone(settings);
|
||||
Assert.IsNull(o2.Annotation<string>());
|
||||
Assert.AreEqual(0, o2.Annotations<Version>().Count());
|
||||
|
||||
JArray a = new JArray();
|
||||
a.AddAnnotation("string!");
|
||||
a.AddAnnotation(version);
|
||||
|
||||
JArray a2 = (JArray)a.DeepClone(settings);
|
||||
Assert.IsNull(a2.Annotation<string>());
|
||||
Assert.AreEqual(0, a2.Annotations<Version>().Count());
|
||||
|
||||
JProperty p = new JProperty("test");
|
||||
p.AddAnnotation("string!");
|
||||
p.AddAnnotation(version);
|
||||
|
||||
JProperty p2 = (JProperty)p.DeepClone(settings);
|
||||
Assert.IsNull(p2.Annotation<string>());
|
||||
Assert.AreEqual(0, p2.Annotations<Version>().Count());
|
||||
|
||||
JRaw r = new JRaw("test");
|
||||
r.AddAnnotation("string!");
|
||||
r.AddAnnotation(version);
|
||||
|
||||
JRaw r2 = (JRaw)r.DeepClone(settings);
|
||||
Assert.IsNull(r2.Annotation<string>());
|
||||
Assert.AreEqual(0, r2.Annotations<Version>().Count());
|
||||
|
||||
JConstructor c = new JConstructor("test");
|
||||
c.AddAnnotation("string!");
|
||||
c.AddAnnotation(version);
|
||||
|
||||
JConstructor c2 = (JConstructor)c.DeepClone(settings);
|
||||
Assert.IsNull(c2.Annotation<string>());
|
||||
Assert.AreEqual(0, c2.Annotations<Version>().Count());
|
||||
|
||||
JValue v = new JValue("test");
|
||||
v.AddAnnotation("string!");
|
||||
v.AddAnnotation(version);
|
||||
|
||||
JValue v2 = (JValue)v.DeepClone(settings);
|
||||
Assert.IsNull(v2.Annotation<string>());
|
||||
Assert.AreEqual(0, v2.Annotations<Version>().Count());
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void Example()
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
public void RawClone()
|
||||
{
|
||||
JRaw r1 = new JRaw("raw1");
|
||||
JToken r2 = r1.CloneToken();
|
||||
JToken r2 = r1.DeepClone();
|
||||
|
||||
CustomAssert.IsInstanceOfType(typeof(JRaw), r2);
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
]";
|
||||
|
||||
JToken t1 = JToken.Parse(json);
|
||||
JToken t2 = t1.CloneToken();
|
||||
JToken t2 = t1.DeepClone();
|
||||
|
||||
List<EmployeeReference> employees = t1.ToObject<List<EmployeeReference>>(JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user