0

I have an issue with the JObject.ToString() method. I am trying to save the object data in a JSON file. The problem here is, I kind of want to use a mix between Formatting.None and Formatting.Indented. I want to print every item in a new row, but if there is a list, I want the list values in one row only and not for each item a new line.

The format should look like this:

{
  "Vector": [1,2,3,4,5]
  "Name": null
}

Currently, if I use Formatting.None I get this:

{"Vector":[1,2,3,4,5],"Name":null}

And if I use Formatting.Indented, I get this:

{
  "Vector": [
    1,
    2,
    3,
    4,
    5
  ],
  "Name": null
}

Is there a way to use Formatting.None only for lists and Formatting.Indented for every other data type?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • 2
    Does this answer your question? [How to apply indenting serialization only to some properties?](https://stackoverflow.com/questions/28655996/how-to-apply-indenting-serialization-only-to-some-properties) – xdtTransform Dec 15 '20 at 14:56
  • [How to apply indenting serialization only to some properties?](https://stackoverflow.com/q/28655996/3744182) and [Newtonsoft inline formatting for subelement while serializing](https://stackoverflow.com/q/30831895/3744182) would appear to be duplicates for this question (though not your [other question](https://stackoverflow.com/q/65304693/3744182)) since the problem here involves changing the formatting during serialization rather than during `JToken` writing. – dbc Dec 16 '20 at 03:24

0 Answers0