1

Is there any way to include spaces in JsonConvert.SerializeObject function?

My object gets serialized to:

{"domain":"domain.com","username":"xyz","videos":null}

Is there any way to get it like below?

{"domain": "domain.com", "username": "xyz", "videos": null}

REASON WHY I WANT IT: I receive a webpage with a lot of Json formatted like above. Some of that json (variable) needs to be replaced by custom one, in order to do this, I deserialize whole json to Dictionary, get some part, serialize and use as a first parameter in replace method.

Better explanation: Let's assume that I receive JSON like below:

{"name": "john", data:{"age": 21, "job": "programmer"}}

I want to replace "data" with my own one. In order to do so, I need to get current "data", so firstly I deserialize whole json, get "data", serialize it to string, because I want to replace it in the webpage html string, like webpage.Replace(oldData, newData), where oldData is serialized string and newData is my custom "data" string

Iks Ski
  • 370
  • 1
  • 5
  • 19
  • The closest you may be able to get is to set the formatting to `Formatting.Indented` then replace all the newlines with spaces. Otherwise you'll need to write a custom `JsonTextWriter`. Are you just wanting spaces between the key/value pairs? Your example isn't entirely consistent (space after the first : but not after subsequent ones). – Ron Beyer Sep 05 '18 at 17:30
  • 1
    Null values will still remain positioned directly after the colon. Wouldn't work for numeric and boolean values either. Regex will have a better result I think. – Bouke Sep 05 '18 at 17:36
  • 2
    Do you have a technical reason for wanting to do so or is it just personal preference? Spaces consume bytes, so for webapis. Removing the spaces in pre-render consumes CPU cycles. It's best to return the minimum of what you need to to save bandwidth and increase download speed. Might seem insignificant, but if you've got an API returning 2 MB of Data being called by thousands of things, it adds up pretty quick. Assuming it's a web api, which is why I'm curious why you want to do this. – Ryan Mann Sep 05 '18 at 17:44
  • @RonBeyer fixed – Iks Ski Sep 05 '18 at 17:52
  • How did you fix it? Can you show us your brand new custom JsonTextWriter? – Bouke Sep 05 '18 at 17:57
  • @Bourke I mean I fixed my example. "Your example isn't entirely consistent (space after the first : but not after subsequent ones)" – Iks Ski Sep 05 '18 at 17:58
  • Just to confirm, you want spaces between tokens, but you **do not** want newline indentations? Can you share a more complex example of what you want? – dbc Sep 05 '18 at 18:02
  • Can you explain *why* you want to do this? It may seem unimportant to you, but it may be vastly pertinent. – Reinstate Monica Cellio Sep 05 '18 at 18:02
  • @dbc I don't want newlines, I just want to add spaces, like in the example. E.g. between "domain": AND "domain.com" – Iks Ski Sep 05 '18 at 18:03
  • I second the comment by @RyanMann. _Why_ do you need this functionality OP? It does not make sense – maccettura Sep 05 '18 at 18:13
  • Reason provided – Iks Ski Sep 05 '18 at 18:14
  • 1
    @IksSki the reasoning you provided does not make sense. You dont need spaces to deserialize JSON... – maccettura Sep 05 '18 at 18:15
  • This is exactly why I asked *"why?"*. If it's to deserialize it then you don't need to do anything (you shouldn't do anything, really), or if it's to display in a web page then you also don't need to do anything - you use one of the many methods of prettifying that already exist. This is definitely an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) – Reinstate Monica Cellio Sep 05 '18 at 18:17
  • @maccettura you don't understand my reason. Read it once again, please – Iks Ski Sep 05 '18 at 18:17
  • 5
    @IksSki: Why do the replace as the string? If you are deserializing it why not just modify that object to how you want and if you need it as a json string just reserialize the whole thing... – Chris Sep 05 '18 at 18:21
  • 2
    Then you could load into a [`JToken`](https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm) hierarchy and replace selected token(s) as shown in, e.g., [Replace part of a JSON with other (using a string token)](https://stackoverflow.com/q/33045235), [How do you add a JToken to an JObject?](https://stackoverflow.com/q/15413825) or [How do you Add or Update a JProperty Value in a JObject](https://stackoverflow.com/q/30085926). – dbc Sep 05 '18 at 18:26
  • 1
    @Chris As I'm thinking about it now... You could be right :l My apologies – Iks Ski Sep 05 '18 at 18:29
  • @IksSki - you could create a custom `JsonTextWriter` like the one shown in https://dotnetfiddle.net/PUXVhO but it would be much better to edit the JSON using LINQ to JSON than with string replacement. – dbc Sep 05 '18 at 18:49
  • @dbc your part with tokens is also very helpful, thanks! – Iks Ski Sep 05 '18 at 19:31

0 Answers0