Escaping in JsonObject


We use JsonObject on our API for a scenario where the type (of a single property) is dynamic and we just ran into an issue with escaped backslashes being doubled. It looks like not all code agrees if the internal values in the JsonObject dictionary are escaped or not.
I think the code below demonstrates the problem pretty clearly. Parsing some json with escapes into a JsonObject and then serializing it again results in double escaping:

using System.Diagnostics;
using ServiceStack;
using ServiceStack.Text;

var a = new Thing()
    Name = "With\\Backslash",
    Val = "With\"Escaped",
var json = a.ToJson();
Console.WriteLine(json);  // Correctly escaped.
JsonObject jsonObject = JsonObject.Parse(json);
Console.WriteLine(jsonObject.SerializeToString()); // Escaping doubled 
Console.WriteLine(jsonObject.ToJson()); // Escaping doubled

var b = jsonObject.ConvertTo<Thing>(); // Works as expected
Debug.Assert(a.Name == b.Name);

class Thing
    public string Name { get; set; }
    public string Val { get; set; }

Am I missing something here, or is this a bug in JsonObject?

That’s because JsonObject is just a wrapper around Dictionary<string,string> where you can get unescaped values with GetUnescaped() or an Unescaped dictionary with ToUnescapedDictionary().

For parsing adhoc json you should use JSON.parse() from ServiceStack.Common instead:

Thanks for the explanation, I’ll start using just plain object on the API then.