3

When I tried to serialize an object with a string property containing a slash /, the JSON generated escapes twice each slash.


Example:

A random class:

public class Foo
{ 
    [DataMember(Name = "bar"]
    public string Bar { get; set; }
}

And

Foo foo = new Foo() { Bar = " Foo / Bar" };
string json = RandomStaticClass.Serialize(foo);

The JSON will be:

{ 
    \"bar\":\"Foo \\/ Bar\"
}

Which results in:

{ 
    "bar":"Foo \/ Bar"
}

While, I just want:

{ 
    "bar":"Foo / Bar"
}

Any ideas ? Thanks :)


Here is my function to serialize an object :

public static string Serialize(object instance)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(instance.GetType());
            serializer.WriteObject(stream, instance);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
2
  • The serializer does this and there isn't an option to turn it off. It's valid JSON, so is there a problem with it being escaped? Commented Jan 21, 2015 at 10:53
  • The problem is that the slash is escaped twice. Commented Jan 21, 2015 at 10:55

1 Answer 1

2

Are you seeing the JSON like this in Visual Studio? Because I believe it is OK like that, while the actual value will not have the escape character.

LE: Tested, you are correct.

I think a workaround might be just to have a Replace call after the string, like this:

        using (MemoryStream stream = new MemoryStream())
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(instance.GetType());
            serializer.WriteObject(stream, instance);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd().Replace("\\", "");
            }
        }

If there is a better option, I would like to know how it's been handled.

I hope this answers your question.

LLE (only if you plan to use this in your client-side with JavaScript):

I have to add the fact that it is automatically done like this (forward slashes are not escaped) to treat closing tags - /> (I quote - "HTML does not allow a string inside a tag to contain </"), because in Client-Side '\/' === '/' in JavaScript. So you do not have to worry about it.

Sign up to request clarification or add additional context in comments.

4 Comments

I have to add the fact that it is automatically done like this (forward slashes are not escaped) to treat closing tags - "/>" (I quote - "HTML does not allow a string inside a <script> tag to contain </"), because in Client-Side '\/' === '/' in JavaScript. So you do not have to worry about it.
The main problem when you remove all backslashes is that you may erase backslashes that are actually in the original string.
But, I can restrict the problem replacing \/ into /.
That is a solution, too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.