Tom
Tom

Reputation: 213

How to unescape string property in Web API

I'm trying to bend my head around the following. I have a WebAPI service that returns MediaInfo like so:

public List<MediaInfo> Get(string id)

Where MediaInfo is just a DTO as:

public class MediaInfo
{
    public string File { get; set; }
    public string Folder{ get; set; }
}

Now, in the Folder property there is a path stored, something like "\\10.x.x.x\share\foldername"

When the result comes back from WebAPI the backslashes are escaped like so:

[
  {
    "File": "GBHIST002242_RUS_xxx_HD_1.mp4",
    "Folder": "\\\\10.x.x.x\\share\\folder\\Archive"
  }
]

I can't seem to get WebAPI or JSON.Net to not escape the contents of Folder. How can I get the WebAPI / JSON.Net formatter to leave the Folder property alone?

[UPDATE]: See my answer below

Upvotes: 0

Views: 462

Answers (1)

Tom
Tom

Reputation: 213

Silly me, the extra backslashes are there for a reason: without them it's no longer valid json. see Serialising a string which contains backslashes with Json.Net

Also when you modify the json to the desired (incorrect) format and validate that in something like https://jsonformatter.curiousconcept.com/ you get validations errors.

So all in all this just lack of knowledge in JSON.

Upvotes: 1

Related Questions