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();
}
}
}