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

Infer non-string types from property values. #20

Open
skwirking opened this issue Apr 13, 2017 · 7 comments
Open

Infer non-string types from property values. #20

skwirking opened this issue Apr 13, 2017 · 7 comments
Labels
Properties Issue related to (Java) Properties format backend

Comments

@skwirking
Copy link

It would be great if there was a way to specify a non-string property value when mapping to the generic ObjectNode type. For example:

Properties props = new Properties();

props.setProperty("booleanExample", "true");
props.setProperty("integerExample", "4");
props.setProperty("stringExample", "some string");
    
ObjectNode root = new JavaPropsMapper().readValue(props, ObjectNode.class);

System.out.println(new ObjectMapper().writeValueAsString(root));

This code outputs

{"integerExample":"4","booleanExample":"true","stringExample":"some string"}

But it would be great if there was some configuration or way to escape property values such that the output inferred integer or boolean types from the values themselves:

{"integerExample":4,"booleanExample":true,"stringExample":"some string"}

@cowtowncoder cowtowncoder added the Properties Issue related to (Java) Properties format backend label Apr 13, 2017
@cowtowncoder
Copy link
Member

Yes, that would make sense. Not sure where and how this would work: I suspect that simple setting would not suffice since users tend to have their own preferences and nuances. So most likely need a handler of some kind.
Which leads to something like maybe:

FasterXML/jackson-core#355

which would allow adding hooks for such conversion at low level: and could then tackle both properties and CSV format -- and perhaps even YAML, and maybe if we are lucky XML.

@skwirking
Copy link
Author

Thanks, @cowtowncoder. I hadn't seen that issue. Perhaps this could be useful as a real-world use case for that feature once development starts.

@cowtowncoder
Copy link
Member

@skwirking yes, absolutely.

@mschielmann
Copy link

Hi, a lot of time has passed - was just wondering - has anything possibly changed in regards to that issue, or is there any other way that JavaPropsMapper would read to JsonNode having integers / booleans instead of TextNodes?

@cowtowncoder
Copy link
Member

@mschielmann No, nothing has changed. If anyone had time & interest in adding configurable coercion/detection functionality, I'd be happy to review.
I will not have time to work on this in near future but I make it my priority to help others.

@mschielmann
Copy link

Thank you for your answer @cowtowncoder, I of course understand.
Seems like an interesting item to pick up, but quite honestly, I feel a bit lost trying to understand how this would fit into the whole ecosystem.

@cowtowncoder
Copy link
Member

@mschielmann Yes, it's not necessarily the simplest thing ever. But changes would be quite self-contained; in JavaPropertiesParser (or however it was named), before indicating something as JsonToken.VALUE_STRING check against a regexp for numbers (etc) to see if coercion would be needed. But yeah then state keeping will become more challenging... and in case of numbers, magnitude (to expose as int, long or BigInteger for integrals; double or BigDecimal for floating-point) will need non-trivial handling.

Similar questions woudl be relevant for CSV backend as well, fwtw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Properties Issue related to (Java) Properties format backend
Projects
None yet
Development

No branches or pull requests

3 participants