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

Convert YAML string issue #129

Closed
785172550 opened this issue Apr 25, 2019 · 14 comments
Closed

Convert YAML string issue #129

785172550 opened this issue Apr 25, 2019 · 14 comments
Milestone

Comments

@785172550
Copy link

jackson-dataformat-yaml 2.8.11

read file config like this:

 gemfireSwitchForRegions:
    maxConnections: '20'
    user: 'OFF'
    legalEntity: 'ON'

----
java

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory().enable(Feature.MINIMIZE_QUOTES));
JsonNode jsonNode = objectMapper.readTree(inputStream);
SequenceWriter sw = objectMapper.writer().writeValues(System.out);
sw.write(jsonNode);

----
output:

  gemfireSwitchForRegions:
    maxConnections: 20
    user: OFF
    legalEntity: ON


read file config like this:

 gemfireSwitchForRegions:
    maxConnections: '20'
    user: OFF
    legalEntity: ON

----
java

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory().enable(Feature.MINIMIZE_QUOTES));
JsonNode jsonNode = objectMapper.readTree(inputStream);
SequenceWriter sw = objectMapper.writer().writeValues(System.out);
sw.write(jsonNode);

----
output:

  gemfireSwitchForRegions:
    maxConnections: 20
    user: false
    legalEntity: true


why in the first case it will convert it will output OFF/ON, but when it reads OFF/ON, it will change to false/true?

@cowtowncoder
Copy link
Member

This is because YAML has weird rules on accepting many strange Strings as booleans, unless explicitly quoted to avoid such handling.
See:

https://yaml.org/type/bool.html

@785172550
Copy link
Author

OK, thanks for your information. It's really weird, so if I use Feature.MINIMIZE_QUOTES config, it can not keep the idempotent operation, right? (e.g. read "OFF" from a file and overwrite to OFF, and at the second time, it will read OFF and overwrite to false). Any suggestions for this case? can I prevent to transform OFF to false by adding configuration?

@cowtowncoder
Copy link
Member

Ok, maybe #90 (to be included in 2.9.9) helps: it will force quoting of "truthy" values?

@785172550
Copy link
Author

Hi cowtowncoder, I think I have found the reason.
In Yaml version 1.2, 'ON' should not convert to true, but it can be in Yaml version 1.1 as the link your provided.

and also, in https://yaml.org, I find information like this:

  • SnakeYAML # Java 5 / YAML 1.1

because the jackson-dataformats-yaml depends on SnakeYAML, so it can not support the new version of yaml standard.

does jackson-dataformats-yaml have any idea about upgrade this? : )

@785172550
Copy link
Author

@GuillaumeSmaha
Copy link
Contributor

GuillaumeSmaha commented Jun 28, 2019

I think the values which can be implicitly converted, should be protected when the option MINIMIZE_QUOTES is enabled (See the list for in https://yaml.org/type/bool.html).
It looks like the same issue with the boolean-like string fixed in FasterXML/jackson-dataformat-yaml#78

GuillaumeSmaha added a commit to GuillaumeSmaha/jackson-dataformats-text that referenced this issue Jun 28, 2019
GuillaumeSmaha added a commit to GuillaumeSmaha/jackson-dataformats-text that referenced this issue Jun 28, 2019
GuillaumeSmaha added a commit to GuillaumeSmaha/jackson-dataformats-text that referenced this issue Jun 28, 2019
GuillaumeSmaha added a commit to GuillaumeSmaha/jackson-dataformats-text that referenced this issue Jun 28, 2019
GuillaumeSmaha added a commit to GuillaumeSmaha/jackson-dataformats-text that referenced this issue Jun 28, 2019
@cowtowncoder
Copy link
Member

Thank you for contributing this! I'll be off until next week, and there's bit of backlog, but I added this on my todo list hoping to get that in before 2.0.0.pr1.

@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Jul 18, 2019
@cowtowncoder cowtowncoder changed the title convert YAML string issue Convert YAML string issue Jul 18, 2019
cowtowncoder added a commit that referenced this issue Jul 18, 2019
@cowtowncoder
Copy link
Member

@GuillaumeSmaha Thank you again for the fix -- will be in 2.10.0(.pr1)

@dukenguyen
Copy link

would it be possible to PLEASE add this fix to the jackson 2.9.x versions? I'm using the latest version of GWT (Google Web Toolkit) which is relegated to using a somewhat older version of Jetty 9.2. That version of Jetty doesn't play well with the new Java 9 module-info Annotation parsing (you alluded to something similar in your 2.10 release notes). I'm also willing to open a PR onto the 2.9 branch if that would help. Thanks a lot.

@cowtowncoder
Copy link
Member

Unfortunately at this point 2.9 branch is basically closed, except for critical security fixes. Further, this particular fix has non-trivial chance of breaking someone's usage somewhere. So unfortunately I don't think I'd want to backport the fix.

@zurvan2
Copy link

zurvan2 commented Apr 23, 2020

I've run across this bug testing exactly the same problem, and the fix does not seem to help. The problem seems to now be on the parsing side not on the output side. The parsed object ends up parsing on and off as booleans, so they end up in the resulting structure as a boolean (true or false).

If the text comes in as "on" the text is (correctly) written out as: on
But if it comes in as on it is still incorrectly written out as: true

@cowtowncoder
Copy link
Member

@zurvan2 If there are remaining problems, please file a new issue; comments on closed issues are easy to miss (but I noticed this one :) ). And if so, include a simple reproduction (ideally unit test, but code snippet works) -- that eliminates much of ambiguity of textual description as details on exactly what calls are made sometimes make big difference.

@zurvan2
Copy link

zurvan2 commented Apr 23, 2020

@cowtowncoder After doing more research, I believe the end result is that jackson 2.x is using SnakeYAML, which only supports YAML 1.1. In YAML 1.1, on and off are valid booleans. So Jackson is currently doing the right thing for parsing.

I look forward to jackson 3.x with SnakeYAML Engine that supports YAML 1.2, where on and off are not valid booleans, and will be treated as strings when parsing.

@cowtowncoder
Copy link
Member

Ok. That does sound plausible. Jackson YAML module does have a fair bit of logic trying to deal with tricky definitions of "alternate" booleans but it is difficult to get that to line up as expected in all places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants