What is the correct way to force JSON.Net to hide slash characters (solidus)?

Therefore, for business reasons, I need to get JSON.NET to avoid a drop of JSON as follows:

{ url: 'http://some.uri/endpoint' }

As

{ "url": "http:\/\/some.uri\/endpoint" }

In other words, he needs to avoid malt characters with a forward slash. I know that the JSON specification does not require this, and technically they are equal, but in this particular situation I need to create the same JSON.NET string as I, from somewhere else.

What is the best way to get JSON.NET to do this?

Would it be wise to create a new subclass of JSONConverter (e.g. MyPedanticStringConverter) and use it like this?

 string json = JSONConvert.SerializeObject( myObject, Formatting.None, new MyPedanticStringConverter()); 
+6
source share
1 answer

If you are looking for a one-stop solution, you may need to write a converter.

Another solution would be to add the property to the class as follows:

 [JsonObject(MemberSerialization = MemberSerialization.OptIn)] public class MyObject { public string Url { get; set; } [JsonProperty("url")] private string UrlJson { get { return this.Url.Replace("/", "\\/"); } } } 

(Obviously, you can change the Replace method to something more complex and more complete).

Hope this helps.

Miki

+2
source

Source: https://habr.com/ru/post/886750/


All Articles