Using Json.NET, is there a way to control the number of array values serialized per line?

StackOverflow https://stackoverflow.com/questions/23147166

  •  05-07-2023
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top