My custom Deserializer provide parent of the type.

37 views
Skip to first unread message

遗迹

unread,
May 31, 2019, 6:29:13 PM5/31/19
to jackson-user
Hello,

I create a deserializer for match corret subclass of abstract class.

I register the deserializer for `InputMessageContent`. 
But when I call `jp.getCodec().readTree<JsonNode>(jp)` in `deserializer(JsonParser jp, DeserializationContext ctxt)`.
It return the parent element which contains  `InputMessageContent`.

But If I call jp.nextToken() before, it will works.

It's says:

input: 
{"foo": {"a": "value", "b": "something"}}

type define:
class Foo{
 
InputMessageContent foo;
}

@JsonDeserialize(using = InputMessageContentDeserializer.class)
class InputMessageContent{
 
String a;
 
String b;
}

Output:
jp.getCodes().readTree(jp).toString()=="{"foo": {"a": "value", "b": "something"}}"
//I wish it should be "{"a": "value", "b": "something"}"   <--- If I call jp.nextToken() before.



In fact, I'm use Kotlin for this.
The source code is long:

Thank you.
iseki



Tatu Saloranta

unread,
May 31, 2019, 7:41:25 PM5/31/19
to jackson-user
I agree: it should not be necessary to call `nextToken()` at that point.

But instead of 

`jp.getCodec().readTree<JsonNode>(jp)`

I would just call

`jp.readValueAsTree()`

Not sure if that makes a difference. Also make sure to use a new version of `jackson-databind` and `jackson-core`.

I hope this helps,

-+ Tatu +-




 

遗迹

unread,
Jun 1, 2019, 9:29:31 AM6/1/19
to jackson-user
Thank you.
I test it. Use `jp.readValueAsTree()` instead of `jp.codecs.readTree`. But the problem still exists. 
I use version is 2.9.8 (I see the lastest version is 2.9.9. But the vert.x project has using 2.9.8......)

Maybe it's a temp workaround.

在 2019年6月1日星期六 UTC+8上午7:41:25,Tatu Saloranta写道:

遗迹

unread,
Jun 1, 2019, 11:26:41 AM6/1/19
to jackson-user
Ah, it not makes difference. I upgrade to 2.9.9 version, but the problem still exists. Maybe I should create a issue :)

Thank you.


在 2019年6月1日星期六 UTC+8上午7:41:25,Tatu Saloranta写道:
On Fri, May 31, 2019 at 3:29 PM 遗迹 <canpo...@gmail.com> wrote:

Tatu Saloranta

unread,
Jun 1, 2019, 7:02:23 PM6/1/19
to jackson-user
On Sat, Jun 1, 2019 at 8:26 AM 遗迹 <canpo...@gmail.com> wrote:
>
> Ah, it not makes difference. I upgrade to 2.9.9 version, but the problem still exists. Maybe I should create a issue :)

I think that makes sense, yes.

-+ 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/d3c6a9f4-4330-4ef0-9544-d119daf5e203%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

遗迹

unread,
Jun 7, 2019, 2:27:55 PM6/7/19
to jackson-user
Problem solved. Must register Kotlin module if using kotlin. Although I think this problem is interesting too.
Reply all
Reply to author
Forward
0 new messages