jackson-core icon indicating copy to clipboard operation
jackson-core copied to clipboard

New JsonReadFeature: JSON5 compatible multi-line String values

Open literakl opened this issue 4 years ago • 4 comments

I read the JsonParser features and it is great that there are relaxed features available. I was reading JSON5 and HJSON and they have some interesting features.

Full support for JSON5 would be great. I especially miss:

Multiline strings:

lineBreaks: "Look, Mom! \
No \\n's!",

Trailing comma:

{ "field": 1, }

It would be great if you could consider an addition of these features. I have not found such request between issues. Thank you

literakl avatar Apr 16 '20 09:04 literakl

First a quick note: trailing commas are optionally available, but like anything that breaks strict JSON compliancy, must be explicitly enabled. Test file src/test/java/com/fasterxml/jackson/core/read/TrailingCommasTest.java has usage. Something like:

   JsonFactory f = JsonFactory.builder().
       // note: `JsonReadFeature` added in 2.10: there is also deprecated `JsonParser.Feature` equivalent
       .enable(JsonReadFeature.ALLOW_TRAILING_COMMA)
       .build();

Second part, possibly allowing multi-line text, would require more work.

Full JSON5 or HJSON support might require different backend (new format backend in jackson-dataformats-text?)

cowtowncoder avatar Apr 16 '20 16:04 cowtowncoder

Can you please add the feature ALLOW_TRAILING_COMMA to the documentation I referenced? It is missing there. Thank you. I understand that JSON5 is too big task. But multiline string would be very welcomed. We need to store code snippet and streamlining it to single line with \n is ugly for maintenance.

literakl avatar Apr 16 '20 16:04 literakl

@literakl yes, I'll try to get to adding that too. I wish project had more contributors to help with documentation, too, so it'd be more up to date.

On new feature: the likeliest route these days is for someone to implement it, send a PR.

cowtowncoder avatar Apr 16 '20 16:04 cowtowncoder

Can you please add the feature ALLOW_TRAILING_COMMA to the documentation I referenced? It is missing there. Thank you. I understand that JSON5 is too big task. But multiline string would be very welcomed. We need to store code snippet and streamlining it to single line with \n is ugly for maintenance.

If I understand correctly you want this JSON5 example to work.

{
  "demo" : "her\
e 2"
}

If you use JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS then this example works (note: no backslash) but the example mentioned above not.

{
  "demo" : "her
e 2"
}

However, it seems that if you use both JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS and JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER the JSON5 example works too (as does the second one).

    ObjectMapper mapper = JsonMapper.builder()
                                    .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS)
                                    .enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)
                                    .build();

    JsonNode jsonNode = mapper.readTree("{\n  \"demo\" : \"her\\\ne 2\"\n}");

Hope this helps @literakl !

natancox avatar May 08 '21 11:05 natancox