Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
literakl opened this issue Apr 16, 2020 · 4 comments
Open

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

literakl opened this issue Apr 16, 2020 · 4 comments
Labels
pr-needed Feature request for which PR likely needed (no active development but idea is workable)

Comments

@literakl
Copy link

literakl commented Apr 16, 2020

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
Copy link
Member

cowtowncoder commented Apr 16, 2020

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 cowtowncoder changed the title New feature: JSON5 New JsonReadFeature: JSON5 compatible multi-line String values Apr 16, 2020
@literakl
Copy link
Author

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
Copy link
Member

cowtowncoder commented Apr 16, 2020

@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
Copy link

natancox commented May 8, 2021

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 !

@cowtowncoder cowtowncoder added the pr-needed Feature request for which PR likely needed (no active development but idea is workable) label Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-needed Feature request for which PR likely needed (no active development but idea is workable)
Projects
None yet
Development

No branches or pull requests

3 participants