FasterXML / jackson-core

Core part of Jackson that defines Streaming API as well as basic shared abstractions
Apache License 2.0
2.25k stars 777 forks source link

New JsonReadFeature: JSON5 compatible multi-line String values #612

Open literakl opened 4 years ago

literakl commented 4 years ago

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

cowtowncoder commented 4 years ago

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?)

literakl commented 4 years ago

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.

cowtowncoder commented 4 years ago

@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.

natancox commented 3 years ago

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 !