-Added StringEscapeHandling with options to escape HTML and non-ASCII characters
-Added feature to deserialize ISet<T> properties as HashSet<T> -Fixed ReferenceLoopHandling not being used when serializing ISerializable and dynamic values
This commit is contained in:
@@ -841,6 +841,69 @@ _____'propertyName': NaN
|
||||
Assert.AreEqual(@"""2000-01-01T01:01:01Z""", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HtmlStringEscapeHandling()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw)
|
||||
{
|
||||
StringEscapeHandling = StringEscapeHandling.EscapeHtml
|
||||
};
|
||||
|
||||
string script = @"<script type=""text/javascript"">alert('hi');</script>";
|
||||
|
||||
writer.WriteValue(script);
|
||||
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(@"""\u003cscript type=\u0022text/javascript\u0022\u003ealert(\u0027hi\u0027);\u003c/script\u003e""", json);
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
Assert.AreEqual(script, reader.ReadAsString());
|
||||
|
||||
//Console.WriteLine(HttpUtility.HtmlEncode(script));
|
||||
|
||||
//System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();
|
||||
//Console.WriteLine(s.Serialize(new { html = script }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NonAsciiStringEscapeHandling()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw)
|
||||
{
|
||||
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
|
||||
};
|
||||
|
||||
string unicode = "\u5f20";
|
||||
|
||||
writer.WriteValue(unicode);
|
||||
|
||||
string json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(8, json.Length);
|
||||
Assert.AreEqual(@"""\u5f20""", json);
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
Assert.AreEqual(unicode, reader.ReadAsString());
|
||||
|
||||
sw = new StringWriter();
|
||||
writer = new JsonTextWriter(sw)
|
||||
{
|
||||
StringEscapeHandling = StringEscapeHandling.Default
|
||||
};
|
||||
|
||||
writer.WriteValue(unicode);
|
||||
|
||||
json = sw.ToString();
|
||||
|
||||
Assert.AreEqual(3, json.Length);
|
||||
Assert.AreEqual("\"\u5f20\"", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty()
|
||||
{
|
||||
|
||||
@@ -291,6 +291,48 @@ namespace Newtonsoft.Json.Tests
|
||||
Console.WriteLine("XML: " + watch.Elapsed.TotalSeconds);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeString()
|
||||
{
|
||||
string text = @"The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
|
||||
Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
|
||||
Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
|
||||
The name of an HTML element is the name used in the tags.
|
||||
Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
|
||||
If attributes are not mentioned, default values are used in each case.
|
||||
|
||||
The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
|
||||
Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
|
||||
Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
|
||||
The name of an HTML element is the name used in the tags.
|
||||
Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
|
||||
If attributes are not mentioned, default values are used in each case.
|
||||
|
||||
The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
|
||||
Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
|
||||
Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
|
||||
The name of an HTML element is the name used in the tags.
|
||||
Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
|
||||
If attributes are not mentioned, default values are used in each case.
|
||||
";
|
||||
|
||||
int interations = 1000;
|
||||
|
||||
TimeOperation(() =>
|
||||
{
|
||||
string json = null;
|
||||
for (int i = 0; i < interations; i++)
|
||||
{
|
||||
using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(text) ?? 16))
|
||||
{
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default);
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}, "New");
|
||||
}
|
||||
|
||||
private void SerializeSize(object value)
|
||||
{
|
||||
// this is extremely slow with 5000 interations
|
||||
|
||||
@@ -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.10.15512")]
|
||||
[assembly: AssemblyFileVersion("4.5.10.15518")]
|
||||
#endif
|
||||
|
||||
@@ -7154,6 +7154,68 @@ Parameter name: value",
|
||||
Assert.IsNull(c2.MyNullableDecimal);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
|
||||
[Test]
|
||||
public void HashSetInterface()
|
||||
{
|
||||
ISet<string> s1 = new HashSet<string>(new[] {"1", "two", "III"});
|
||||
|
||||
string json = JsonConvert.SerializeObject(s1);
|
||||
|
||||
ISet<string> s2 = JsonConvert.DeserializeObject<ISet<string>>(json);
|
||||
|
||||
Assert.AreEqual(s1.Count, s2.Count);
|
||||
foreach (string s in s1)
|
||||
{
|
||||
Assert.IsTrue(s2.Contains(s));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public class NewEmployee : Employee
|
||||
{
|
||||
public int Age { get; set; }
|
||||
|
||||
public bool ShouldSerializeName()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldSerializeInheritedClassTest()
|
||||
{
|
||||
NewEmployee joe = new NewEmployee();
|
||||
joe.Name = "Joe Employee";
|
||||
joe.Age = 100;
|
||||
|
||||
Employee mike = new Employee();
|
||||
mike.Name = "Mike Manager";
|
||||
mike.Manager = mike;
|
||||
|
||||
joe.Manager = mike;
|
||||
|
||||
//StringWriter sw = new StringWriter();
|
||||
|
||||
//XmlSerializer x = new XmlSerializer(typeof(NewEmployee));
|
||||
//x.Serialize(sw, joe);
|
||||
|
||||
//Console.WriteLine(sw);
|
||||
|
||||
//JavaScriptSerializer s = new JavaScriptSerializer();
|
||||
//Console.WriteLine(s.Serialize(new {html = @"<script>hi</script>; & ! ^ * ( ) ! @ # $ % ^ ' "" - , . / ; : [ { } ] ; ' - _ = + ? ` ~ \ |"}));
|
||||
|
||||
string json = JsonConvert.SerializeObject(joe, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Age"": 100,
|
||||
""Name"": ""Joe Employee"",
|
||||
""Manager"": {
|
||||
""Name"": ""Mike Manager""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
@@ -31,6 +32,11 @@ using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
|
||||
#endif
|
||||
#if !(NET20 || NET35 || WINDOWS_PHONE)
|
||||
using System.Dynamic;
|
||||
#endif
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Tests.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
@@ -142,6 +148,126 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
|
||||
#if !(PORTABLE || SILVERLIGHT)
|
||||
public class MainClass : ISerializable
|
||||
{
|
||||
public ChildClass Child { get; set; }
|
||||
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Child", Child);
|
||||
}
|
||||
}
|
||||
|
||||
public class ChildClass : ISerializable
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public MainClass Parent { get; set; }
|
||||
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Parent", Parent);
|
||||
info.AddValue("Name", Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
|
||||
main.Child = child;
|
||||
|
||||
var settings =
|
||||
new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+MainClass'. Path 'Child'.",
|
||||
() => JsonConvert.SerializeObject(main, settings));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
|
||||
main.Child = child;
|
||||
|
||||
var settings =
|
||||
new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
|
||||
var c = JsonConvert.SerializeObject(main, settings);
|
||||
Assert.AreEqual(@"{""Child"":{""Name"":""Child1""}}", c);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
|
||||
public class DictionaryDynamicObject : DynamicObject
|
||||
{
|
||||
public IDictionary<string, object> Values { get; private set; }
|
||||
|
||||
public DictionaryDynamicObject()
|
||||
{
|
||||
Values = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public override bool TrySetMember(SetMemberBinder binder, object value)
|
||||
{
|
||||
Values[binder.Name] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
{
|
||||
return Values.TryGetValue(binder.Name, out result);
|
||||
}
|
||||
|
||||
public override IEnumerable<string> GetDynamicMemberNames()
|
||||
{
|
||||
return Values.Keys;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
child.parent = parent;
|
||||
|
||||
var settings = new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+DictionaryDynamicObject'. Path 'child'.",
|
||||
() => JsonConvert.SerializeObject(parent, settings));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
parent.name = "parent";
|
||||
child.parent = parent;
|
||||
child.name = "child";
|
||||
|
||||
var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
|
||||
var c = JsonConvert.SerializeObject(parent, settings);
|
||||
Assert.AreEqual(@"{""child"":{""name"":""child""},""name"":""parent""}", c);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public class PropertyItemReferenceLoopHandling
|
||||
|
||||
Reference in New Issue
Block a user