diff --git a/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs b/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs new file mode 100644 index 000000000..63bf85b10 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Newtonsoft.Json.Utilities; +using NUnit.Framework; + +namespace Newtonsoft.Json.Tests +{ + [TestFixture] + public class JavaScriptConvertTest + { + [Test] + public void EscapeJavaScriptString() + { + string result; + + result = JavaScriptUtils.ToEscapedJavaScriptString("How now brown cow?", '"', true); + Assert.AreEqual(@"""How now brown cow?""", result); + + result = JavaScriptUtils.ToEscapedJavaScriptString("How now 'brown' cow?", '"', true); + Assert.AreEqual(@"""How now 'brown' cow?""", result); + + result = JavaScriptUtils.ToEscapedJavaScriptString("How now cow?", '"', true); + Assert.AreEqual(@"""How now cow?""", result); + + result = JavaScriptUtils.ToEscapedJavaScriptString(@"How +now brown cow?", '"', true); + Assert.AreEqual(@"""How \r\nnow brown cow?""", result); + + result = JavaScriptUtils.ToEscapedJavaScriptString("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", '"', true); + Assert.AreEqual(@"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007""", result); + + result = + JavaScriptUtils.ToEscapedJavaScriptString("\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013", '"', true); + Assert.AreEqual(@"""\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013""", result); + + result = + JavaScriptUtils.ToEscapedJavaScriptString( + "\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f ", '"', true); + Assert.AreEqual(@"""\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f """, result); + + result = + JavaScriptUtils.ToEscapedJavaScriptString( + "!\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]", '"', true); + Assert.AreEqual(@"""!\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]""", result); + + result = JavaScriptUtils.ToEscapedJavaScriptString("^_`abcdefghijklmnopqrstuvwxyz{|}~", '"', true); + Assert.AreEqual(@"""^_`abcdefghijklmnopqrstuvwxyz{|}~""", result); + + string data = + "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + string expected = + @"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""; + + result = JavaScriptUtils.ToEscapedJavaScriptString(data, '"', true); + Assert.AreEqual(expected, result); + + result = JavaScriptUtils.ToEscapedJavaScriptString("Fred's cat.", '\'', true); + Assert.AreEqual(result, @"'Fred\'s cat.'"); + + result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are you gentlemen?"" said Cats.", '"', true); + Assert.AreEqual(result, @"""\""How are you gentlemen?\"" said Cats."""); + + result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are' you gentlemen?"" said Cats.", '"', true); + Assert.AreEqual(result, @"""\""How are' you gentlemen?\"" said Cats."""); + + result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true); + Assert.AreEqual(result, @"'Fred\'s ""cat"".'"); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index fbbd1ce82..264755644 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -498,6 +498,26 @@ public void ReadOnlyCollectionSerialize() CollectionAssert.AreEqual(r1, r2); } + public class Person + { + private Guid _internalId; + private string _firstName; + + [JsonIgnore] + public Guid InternalId + { + get { return _internalId; } + set { _internalId = value; } + } + + [JsonProperty("first_name")] + public string FirstName + { + get { return _firstName; } + set { _firstName = value; } + } + } + //public abstract class Foo //{ //} diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index a1bb2c487..e0da73c44 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -46,6 +46,7 @@ + diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index e17ba8fac..daff630ce 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -34,6 +34,7 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests")] [assembly: AllowPartiallyTrustedCallers] // Setting ComVisible to false makes the types in this assembly not visible diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 7bedfbb95..fde0bb92d 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -84,9 +84,13 @@ public static void WriteEscapedJavaScriptChar(TextWriter writer, char c, char de //case '\'': // StringUtils.WriteCharAsUnicode(writer, c); // break; + case '\'': + // only escape if this charater is being used as the delimiter + writer.Write((delimiter == '\'') ? @"\'" : @"'"); + break; case '"': // only escape if this charater is being used as the delimiter - writer.Write((delimiter == '"') ? "\\\"" : null); + writer.Write((delimiter == '"') ? "\\\"" : @""""); break; default: if (c > '\u001f') @@ -130,8 +134,4 @@ public static string ToEscapedJavaScriptString(string value, char delimiter, boo } } } -} - - - - +} \ No newline at end of file