Question

I'm currently serializing arrays of float values and I'd like to serialize more than 1 array value per line.

For example, the following output is the standard Json.NET formatting which serializes a single value per line for a 4x4 float array:

  "Matrix1": [
    [
      1.37611,
      -0.37982,
      0.00381,
      -0.00173
    ],
    [
      -0.46092,
      0.77912,
      -0.02207,
      0.01123
    ],
    [
      0.00012,
      -4E-05,
      0.52735,
      -0.60753
    ],
    [
      -1E-05,
      -0.00093,
      -0.07794,
      1.28313
    ]
  ]

What I'd like to see as serialized output is more like this:

  "Matrix1": [
    [ 1.37611, 0.37982, 0.00381, -0.00173 ],
    [ -0.46092, 0.77912, -0.02207, 0.01123 ],
    [ 0.00012, -4E-05, 0.52735, -0.60753 ],
    [ -1E-05, -0.00093, -0.07794, 1.28313 ]
  ]

Is there a way to instruct the JsonSerializer to serialize arrays with more than a single value per line, and to do custom serializing like this?

I know that I can use the JsonTextWriter to customize the output in this manner, but I'd like to use the JsonSerializer to serialize all my single and multi-dimensional arrays using the above format and limit the number of values written per line to a maximum value, such as 10 values per line.

Thanks.

Was it helpful?

Solution

No.

Json.Net offers only two formatting options: Indented or None. Indented will format it as you have shown in your first example; None will remove all extra line breaks and whitespace. If you want a custom formatting scheme, you'll need to handle this yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top