Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 51c6407

Browse files
author
Bruno Brito
committed
Update string serialize with special chars.
When deserializing this: "Hi I'm \r\n new line :)" produces: "Hi I'm\ \ new line :)" This occurs because a bug in JsonUtils.WriteString, on Escaped variables it's concating EscapedChar with the '\r', '\n', '\t'... Like this: { EscapeChar, CarriageReturnChar }, produces that bug.
1 parent 1afced6 commit 51c6407

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/ServiceStack.Text/Json/JsonUtils.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public static class JsonUtils
1111
public const long MinInteger = -9007199254740992;
1212

1313
public const char EscapeChar = '\\';
14+
15+
public const char EscapeTab = 't';
16+
public const char EscapeCarriageReturn = 'r';
17+
public const char EscapeLineFeedChar = 'n';
18+
public const char EscapeFormFeedChar = 'f';
19+
public const char EscapeBackspaceChar = 'b';
20+
1421
public const char QuoteChar = '"';
1522
public const string Null = "null";
1623
public const string True = "true";
@@ -26,11 +33,11 @@ public static class JsonUtils
2633
/// Micro-optimization keep pre-built char arrays saving a .ToCharArray() + function call (see .net implementation of .Write(string))
2734
/// </summary>
2835
private static readonly char[] EscapedBackslash = { EscapeChar, EscapeChar };
29-
private static readonly char[] EscapedTab = { EscapeChar, TabChar };
30-
private static readonly char[] EscapedCarriageReturn = { EscapeChar, CarriageReturnChar };
31-
private static readonly char[] EscapedLineFeed = { EscapeChar, LineFeedChar };
32-
private static readonly char[] EscapedFormFeed = { EscapeChar, FormFeedChar };
33-
private static readonly char[] EscapedBackspace = { EscapeChar, BackspaceChar };
36+
private static readonly char[] EscapedTab = { EscapeChar, EscapeTab };
37+
private static readonly char[] EscapedCarriageReturn = { EscapeChar, EscapeCarriageReturn };
38+
private static readonly char[] EscapedLineFeed = { EscapeChar, EscapeLineFeedChar };
39+
private static readonly char[] EscapedFormFeed = { EscapeChar, EscapeFormFeedChar };
40+
private static readonly char[] EscapedBackspace = { EscapeChar, EscapeBackspaceChar };
3441
private static readonly char[] EscapedQuote = { EscapeChar, QuoteChar };
3542

3643
public static readonly char[] WhiteSpaceChars = { ' ', TabChar, CarriageReturnChar, LineFeedChar };

tests/ServiceStack.Text.Tests/JsonTests/EscapedCharsTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using NUnit.Framework;
34
using ServiceStack.Common.Tests.Models;
45

@@ -259,6 +260,50 @@ public void Can_serialize_array_of_control_chars_and_unicode()
259260
Assert.That(json, Is.EqualTo(@"[""\u0018"",""Ω""]"));
260261
}
261262

263+
264+
[Test]
265+
public void Can_serialize_windows_new_line()
266+
{
267+
const string expected = "\"Hi I\'m\\r\\nnew line :)\"";
268+
var text = "Hi I\'m" + Environment.NewLine + "new line :)";
269+
270+
var result = JsonSerializer.SerializeToString(text);
271+
272+
Assert.AreEqual(expected, result);
273+
}
274+
275+
[Test]
276+
public void Can_serialize_object_with_escaped_chars()
277+
{
278+
const string expected = "{\"Id\":1,\"Name\":\"Hi I'm\\r\\nnew line :)\"}";
279+
var model = new ModelWithIdAndName
280+
{
281+
Id = 1,
282+
Name = "Hi I'm" + Environment.NewLine + "new line :)"
283+
};
284+
285+
var result = JsonSerializer.SerializeToString(model);
286+
287+
Assert.AreEqual(expected, result);
288+
}
289+
290+
[Test]
291+
public void Can_deserialize_with_new_line()
292+
{
293+
var model = new ModelWithIdAndName
294+
{
295+
Id = 1,
296+
Name = "Hi I'm" + Environment.NewLine + "new line :)"
297+
};
298+
299+
const string json = "{\"Id\":1,\"Name\":\"Hi I'm\\r\\nnew line :)\"}";
300+
301+
var fromJson = JsonSerializer.DeserializeFromString<ModelWithIdAndName>(json);
302+
303+
Assert.That(fromJson, Is.EqualTo(model));
304+
}
305+
306+
262307
public class MyModel
263308
{
264309
public string Name { get; set; }

0 commit comments

Comments
 (0)