Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #244 from kevin-montrose/master

Fixing a bug where unicode sequences would be omitted.
  • Loading branch information...
commit ba114aa90f47daf2b3c936abf69b3f643a3e0282 2 parents 1011da2 + 28d8285
@mythz mythz authored
View
5 src/ServiceStack.Text/Json/JsonTypeSerializer.cs
@@ -468,7 +468,8 @@ private static string UnEscapeJsonString(string json, ref int index)
}
/// <summary>
- /// Since Silverlight doesn't have char.ConvertFromUtf32() so putting Mono's implemenation inline.
+ /// Given a character as utf32, returns the equivalent string provided that the character
+ /// is legal json.
/// </summary>
/// <param name="utf32"></param>
/// <returns></returns>
@@ -476,8 +477,6 @@ private static string ConvertFromUtf32(int utf32)
{
if (utf32 < 0 || utf32 > 0x10FFFF)
throw new ArgumentOutOfRangeException("utf32", "The argument must be from 0 to 0x10FFFF.");
- if (0xD800 <= utf32 && utf32 <= 0xDFFF)
- throw new ArgumentOutOfRangeException("utf32", "The argument must not be in surrogate pair range.");
if (utf32 < 0x10000)
return new string((char)utf32, 1);
utf32 -= 0x10000;
View
94 src/ServiceStack.Text/Json/JsonUtils.cs
@@ -46,53 +46,49 @@ public static void WriteString(TextWriter writer, string value)
writer.Write(QuoteChar);
var len = value.Length;
- for (var i = 0; i < len; i++)
- {
- switch (value[i])
- {
- case '\n':
- writer.Write("\\n");
- continue;
-
- case '\r':
- writer.Write("\\r");
- continue;
-
- case '\t':
- writer.Write("\\t");
- continue;
-
- case '"':
- case '\\':
- writer.Write('\\');
- writer.Write(value[i]);
- continue;
-
- case '\f':
- writer.Write("\\f");
- continue;
-
- case '\b':
- writer.Write("\\b");
- continue;
- }
-
- //Is printable char?
- if (value[i] >= 32 && value[i] <= 126)
- {
- writer.Write(value[i]);
- continue;
- }
-
- var isValidSequence = value[i] < 0xD800 || value[i] > 0xDFFF;
- if (isValidSequence)
- {
- // Default, turn into a \uXXXX sequence
- IntToHex(value[i], hexSeqBuffer);
- writer.Write("\\u");
- writer.Write(hexSeqBuffer);
- }
- }
+ for (var i = 0; i < len; i++)
+ {
+ switch (value[i])
+ {
+ case '\n':
+ writer.Write("\\n");
+ continue;
+
+ case '\r':
+ writer.Write("\\r");
+ continue;
+
+ case '\t':
+ writer.Write("\\t");
+ continue;
+
+ case '"':
+ case '\\':
+ writer.Write('\\');
+ writer.Write(value[i]);
+ continue;
+
+ case '\f':
+ writer.Write("\\f");
+ continue;
+
+ case '\b':
+ writer.Write("\\b");
+ continue;
+ }
+
+ //Is printable char?
+ if (value[i] >= 32 && value[i] <= 126)
+ {
+ writer.Write(value[i]);
+ continue;
+ }
+
+ // Default, turn into a \uXXXX sequence
+ IntToHex(value[i], hexSeqBuffer);
+ writer.Write("\\u");
+ writer.Write(hexSeqBuffer);
+ }
writer.Write(QuoteChar);
}
@@ -108,6 +104,10 @@ private static bool HasAnyEscapeChars(string value)
for (var i = 0; i < len; i++)
{
var c = value[i];
+
+ // non-printable
+ if (!(value[i] >= 32 && value[i] <= 126)) return true;
+
if (c >= LengthFromLargestChar || !EscapeCharFlags[c]) continue;
return true;
}
View
19 tests/ServiceStack.Text.Tests/JsonObjectTests.cs
@@ -31,5 +31,24 @@ public void Does_escape_string_access()
Assert.That(jsonObject.GetUnescaped("a"), Is.EqualTo(test.Replace("\"","\\\"")));
}
+
+ [Test]
+ public void Does_encode_unicode()
+ {
+ string test = "<\"I get this : 􏰁􏰂􏰃􏰄􏰂􏰅􏰆􏰇􏰈􏰀􏰉􏰊􏰇􏰋􏰆􏰌􏰀􏰆􏰊􏰀􏰍􏰄􏰎􏰆􏰏􏰐􏰑􏰑􏰆􏰒􏰆􏰂􏰊􏰀";
+ var obj = new { test };
+ using (var mem = new System.IO.MemoryStream())
+ {
+ ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), mem);
+
+ var encoded = System.Text.Encoding.UTF8.GetString(mem.ToArray());
+
+ var copy1 = JsonObject.Parse(encoded);
+
+ Assert.That(test, Is.EqualTo(copy1["test"]));
+
+ System.Diagnostics.Debug.WriteLine(copy1["test"]);
+ }
+ }
}
}

0 comments on commit ba114aa

Please sign in to comment.
Something went wrong with that request. Please try again.