json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.51k stars 518 forks source link

indentation !=0 & suppress line feed between array elements in JSON output #286

Open RalphSteinhagen opened 4 years ago

RalphSteinhagen commented 4 years ago

Thanks for the great library. This fits our use-case quite neatly and has a great IO performance. :+1:

As food-for-thought and/or possible feature request linked to the JsonStream.setIndentionStep(DEFAULT_INDENTATION) config.

Would it be possible to suppress line feeds (return) between individual array elements and add the line-feed only once the array is complete? Presently (e.g. JsonStream.setIndentionStep(DEFAULT_INDENTATION)) e.g.:

{
  "bool1": true,
  "byte1": 10,
  "char1": 97,
  "short1": 20,
  "int1": 30,
  "long1": 40,
  "float1": 50.5,
  "double1": 60.6,
  "string1": "\u0393\u03b5\u03b9\u03ac \u03c3\u03bf\u03c5 \u039a\u03cc\u03c3\u03bc\u03b5!",
  "boolArray": [
    true,
    false,
    true,
    false,
    true,
    false,
    true,
    false,
    true,
    false
  ],
  "doubleArray": [
    0.699999988079071,
    0.800000011920929,
    0.8999999761581421,
    1.0,
    1.100000023841858,
    1.2000000476837158,
    1.2999999523162842,
    1.399999976158142,
    1.5,
    1.600000023841858
  ]
}

to something like:

{
  "bool1": true,
  "byte1": 10,
  "char1": 97,
  "short1": 20,
  "int1": 30,
  "long1": 40,
  "float1": 50.5,
  "double1": 60.6,
  "string1": "\u0393\u03b5\u03b9\u03ac \u03c3\u03bf\u03c5 \u039a\u03cc\u03c3\u03bc\u03b5!",
  "boolArray": [true, false, true, false, true, false, true, false, true, false],
  "doubleArray": [0.699999988079071, 0.800000011920929, 0.8999999761581421, 1.0, 1.100000023841858, 1.2000000476837158, 1.2999999523162842, 1.399999976158142, 1.5, 1.600000023841858]
}

This has no impact on the validity or numeric parsing of the JSON output but (similarly to the indentation) would make it more human-readable, especially for large arrays, large maps, and if there are many fields.

N.B. as a minor side note/question: isn't the double format more precise/faster if expressed in an exponential form (am thinking since IEEE floating points are stored w.r.t. exponent and mantissa)

I see the place in the code where the line feed is issued to the stream but the corresponding function/array reflection handlers cannot be overwritten. Thanks in advance and keep up the good work!