Issue with ObjectNode.putPOJO

367 views
Skip to first unread message

ias...@imgeospatial.com

unread,
May 3, 2018, 12:29:47 PM5/3/18
to jackson-user

It seems that the ObjectNode.PutPOJO method does not put quotes around the members of a string array,


List<String> names = List.of("Zaphod""Arthur""Trillian""Ford");


ObjectMapper objectMapper = new ObjectMapper();


ObjectNode rootNode = objectMapper.createObjectNode();

rootNode.putPOJO("pojo", names);

rootNode.set("set", objectMapper.valueToTree(names));

System.out.println(rootNode);


The above code demonstrates the issue.

The string output cannot be parsed by ObjectMapper.readTree

Tatu Saloranta

unread,
May 3, 2018, 12:36:50 PM5/3/18
to jackson-user
This is not really along original intended usage for putPOJO (since
`List`s are not POJOs), but it would seem like
supporting it might be easy enough.

Could you do me a favor and file an issue against `jackson-databind`,
with above description?
That way you get credit for reporting the issue and I can link new
feature to reported issue for release notes.

-+ Tatu +-

Tatu Saloranta

unread,
May 3, 2018, 12:40:02 PM5/3/18
to jackson-user
Oh actually, no, never mind.

Your problem is here:

System.out.println(rootNode);

JsonNode.toString() is explicitly defined not to necessarily produce
valid JSON, and it should never be used
for anything other than diagnostics or debugging. In fact I am
planning to make it so that in 3.0 it will usually
not produce valid JSON to make this explicit.

To produce JSON you have to serialize node using `ObjectMapper` or
`ObjectWriter`: this will use all configured
settings to handle serialization details. Problem with `toString()` is
that it would have to replicate aspects of serialization
since there is no way to access any object capable of serialization
(there has to be a `JsonGenerator` to use, fundamentally).

I hope this helps,

-+ Tatu +-

ias...@imgeospatial.com

unread,
May 8, 2018, 4:59:13 AM5/8/18
to jackson-user
Tatu,

Tried it over the weekend and using the objectMapper to convert the JSONNode to a string works a treat. I agree that getting the JSONNode.toString not to output valid JSON would help considerably here. Even if it just prepended JSON:.

Thanks very much,
Ian

Tatu Saloranta

unread,
May 8, 2018, 12:42:55 PM5/8/18
to jackson-user
Right -- it is understandable that when `toString()` produces valid
JSON most of the time
(and esp. for simple cases) it would seem like useful. And some
libraries use that as the main
String serialization method.

>
> Thanks very much,
> Ian

You are welcome!

-+ Tatu +-

>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages