Re: [jackson-user] Question about how ObjectMapper convertValue works internally

1,531 views
Skip to first unread message

Pascal Gelinas

unread,
Dec 20, 2013, 9:56:28 AM12/20/13
to us...@jackson.codehaus.org, jackso...@googlegroups.com
Cross-posting on google groups, it is the new communication channel.

I believe the methods you are looking for instead are
ObjectMapper#readValue(JsonParser, Class<T>) with ObjectNode#traverse()
which returns the JsonParser to use with the first method.
ObjectMapper#convertValue will work, but it will serialize the
ObjectNode structure in an intermediate TokenBuffer then parse/data-bind
into result value type, so definitely sub-optimal.

Hope this helps,
Pascal G�linas

On 12/19/2013 05:31 PM, Ransom Briggs wrote:
> I was reading the documentation for ObjectMapper.convertValue (in
> 1.9.4 api) and it says that it is a "Convenience method for doing
> two-step conversion from given value, into instance of given value
> type. This is functionality equivalent to first serializing given
> value into JSON, then binding JSON data into value of given type, but
> may be executed without fully serializing into JSON."
>
> If I have something that is already and ObjectNode that I want to
> convert to a given value type is there a better method to call? or
> will convertValue recognize that I already have converted it to JSON
> already and skip the first step?
>
> Thanks,
> Ransom

Tatu Saloranta

unread,
Dec 20, 2013, 1:37:03 PM12/20/13
to jackso...@googlegroups.com, us...@jackson.codehaus.org
Correct. And back to original question: convertValue() is to most efficient general-purpose way to bind data from JSON Tree Model into POJOs. It does avoid actual writing of JSON as text, and as Pascal explained, will use internal equivalent of token stream to use a source.

-+ Tatu +-


On Fri, Dec 20, 2013 at 6:56 AM, Pascal Gelinas <pascal....@gmail.com> wrote:
Cross-posting on google groups, it is the new communication channel.

I believe the methods you are looking for instead are ObjectMapper#readValue(JsonParser, Class<T>) with ObjectNode#traverse() which returns the JsonParser to use with the first method. ObjectMapper#convertValue will work, but it will serialize the ObjectNode structure in an intermediate TokenBuffer then parse/data-bind into result value type, so definitely sub-optimal.

Hope this helps,
Pascal Gélinas


On 12/19/2013 05:31 PM, Ransom Briggs wrote:
I was reading the documentation for ObjectMapper.convertValue (in 1.9.4 api) and it says that it is a "Convenience method for doing two-step conversion from given value, into instance of given value type. This is functionality equivalent to first serializing given value into JSON, then binding JSON data into value of given type, but may be executed without fully serializing into JSON."

If I have something that is already and ObjectNode that I want to convert to a given value type is there a better method to call?  or will convertValue recognize that I already have converted it to JSON already and skip the first step?

Thanks,
Ransom

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ransom Briggs

unread,
Dec 20, 2013, 5:03:02 PM12/20/13
to jackso...@googlegroups.com, us...@jackson.codehaus.org
Thank you Pascal and Tatu, this is exactly what I was looking for, I swapped my code out and it about 50% faster.

Thanks a bunch,
Ransom
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages