WriteString function when the string contains a '\' (backslash)

Oct 15, 2015 at 1:21 PM
Just wanted to inform that I used this dll in my project and one of my tests revealed a bug where strings containing backslashes would be deserialized incorrectly, for example:
[Test]
public void TestBackslash(){
    StringBuilder builder = new StringBuilder();
    JsonWriter jwriter = new JsonWriter(builder);

    JsonSerializer.Serialize(jwriter, new TestType() { ThisPropertyIsAPath = @"C:\Program Files\" });

    JsonReader reader = new JsonReader(builder.ToString());

    var parsedData = JsonDeserializer.Deserialize<TestType>(reader);

    Assert.AreEqual(@"C:\Program Files\", parsedData.ThisPropertyIsAPath);
}
Would fail with an exception when deserializing: "Unreconized escape character 'P'"

I fixed this in my project by changing the "WriteString" function from JsonWriter into:
        public virtual void WriteString(string value)
        {
            _writer.Write(string.Concat(JsonTokens.StringDelimiter, value.Replace("\\", "\\\\").Replace("\"", "\\\""), JsonTokens.StringDelimiter));
        }