Is there a runtime equivalent of @JsonSubTypes with name parameter?

1,342 views
Skip to first unread message

da...@bakins-bits.com

unread,
Oct 3, 2015, 2:33:35 PM10/3/15
to jackson-user
(one more question for the weekend ...)

Is there a runtime equivalent to @JsonSubTypes with the name parameter?  E.g., some data structure I can build and hang on to an ObjectMapper so that I can use an arbitrary name (instead of the type names) to do polymorphic deserialization without having to list all the subtypes at compile time in the @JsonSubTypes attribute?

Or is the lack of this capability what the documentation is referring to when it says: "In future we may want to add additional methods for linking types with sub-types: current method is not optimal for use cases where subtypes may be added dynamically; and it does add unnecessary back-links between types (even if as annotation metadata)."?  (If so, if I wanted to build this thing myself and add it to ObjectMapper what should I be looking at doing?)

(I would really like to use my own names instead of type names for the indirection (so I can substitute implementation classes) but also want both to do this at runtime and decouple the parent class from the knowledge of all its subclasses at compile time.)

Thanks! -- David

Tatu Saloranta

unread,
Oct 3, 2015, 4:40:01 PM10/3/15
to jackso...@googlegroups.com
I think this is what SimpleModule.registerSubtypes(NamedType...) lets you do.

-+ 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.

da...@bakins-bits.com

unread,
Oct 3, 2015, 7:40:04 PM10/3/15
to jackson-user
Yes, thank you!  That is what it lets you do (see example below, for anyone intersested).

I see now that there is much more stuff on ObjectMapper in the javadocs than in is described in the wiki.  I see also that the idea of a type "name" assigned by the programmer independent of the actual class name is a more general concept in Jackson than just JsonSubTypes.  Thanks!  -- David


On Saturday, October 3, 2015 at 1:40:01 PM UTC-7, Tatu Saloranta wrote:
I think this is what SimpleModule.registerSubtypes(NamedType...) lets you do.

-+ Tatu +-

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "messageName")

public abstract static class NamedBase

{

public float basef;

public NamedBase() {

basef = 4.5f;

}

@Override

public String toString() {

return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("basef", basef).toString();

}

}

public static class SubName1 extends NamedBase

{

public int a1;

public SubName1() {

a1 = 45;

}

@Override

public String toString() {

return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())

.append("a1", a1)

.toString();

}

}

public static class SubName2 extends NamedBase

{

public String b2;

public SubName2() {

b2 = "45";

}

@Override

public String toString() {

return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())

.append("b2", b2)

.toString();

}

}

public static class TwoNamedBases

{

private NamedBase x1, x2;

@JsonCreator

public TwoNamedBases(@JsonProperty("x1") NamedBase x1, @JsonProperty("x2") NamedBase x2) {

this.x1 = x1;

this.x2 = x2;

}

public NamedBase getX1() {

return x1;

}

public NamedBase getX2() {

return x2;

}

@Override

public String toString() {

return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("x1", x1)

.append("x2", x2)

.toString();

}

}

@Test

public void serialize_deserialize_named_classes() throws IOException {

NamedBase sut1 = new SubName1();

NamedBase sut2 = new SubName2();

TwoNamedBases sut = new TwoNamedBases(sut1, sut2);

SimpleModule subtypes = new SimpleModule().registerSubtypes(new NamedType(SubName1.class, "sub1"),

new NamedType(SubName2.class, "sub2"));

ObjectMapper mapper = new ObjectMapper().registerModule(subtypes);

String actual1 = mapper.writeValueAsString(sut);

assertThat(actual1).isEqualTo(

"{\"x1\":{\"messageName\":\"sub1\",\"basef\":4.5,\"a1\":45},\"x2\":{\"messageName\":\"sub2\",\"basef\":4.5,\"b2\":\"45\"}}");

TwoNamedBases actual2 = mapper.readValue(actual1, TwoNamedBases.class);

assertThat(actual2.toString()).isEqualTo(sut.toString());

}

 

Tatu Saloranta

unread,
Oct 4, 2015, 1:31:25 PM10/4/15
to jackso...@googlegroups.com
Correct, Wiki has not been significantly updated. This is one area where contributions would be very welcome.

Note, too, that Fasterxml wiki is planned to be deprecated and replaced either by Jackson github project wikis & READMEs, or perhaps by revised Fasterxml web site. Most up-to-date docs are within github project READMEs, wikis.
In fact, fasterxml wiki is mostly for 1.x features.

-+ Tatu +-


--

da...@bakins-bits.com

unread,
Dec 19, 2015, 1:12:34 AM12/19/15
to jackson-user


On Sunday, October 4, 2015 at 10:31:25 AM UTC-7, Tatu Saloranta wrote:
Correct, Wiki has not been significantly updated. This is one area where contributions would be very welcome.

Note, too, that Fasterxml wiki is planned to be deprecated and replaced either by Jackson github project wikis & READMEs, or perhaps by revised Fasterxml web site. Most up-to-date docs are within github project READMEs, wikis.
In fact, fasterxml wiki is mostly for 1.x features.

-+ Tatu +-

wiki.fasterxml.com appears to be gone,  so ... revisiting this answer, Tatu, is it time to start contributing to the GitHub project wikis?  And if so ... a) what's the mechanism?  pull requests? and b) how to coordinate with you on what pages/paragraphs the contributor wants to work on?  this forum, the dev forum, email, issues, or?  -- David

Tatu Saloranta

unread,
Dec 19, 2015, 12:10:17 PM12/19/15
to jackson-user, jacks...@googlegroups.com
Fasterxml.com being down should be temporary, but it's a good reminder agreed.
So far Pull Requests have worked for minor typo/link-rot fixes.

We had internal discussions (within fasterxml core team) on whether to find another Wiki, and/or blogging system, as blogs might be very useful for certain style of documentation, but most blogging platforms had one issue or another. But it is time to try to get that moving again; at this point anything is better than nothing :)

On access... I think that `jackson`, being mostly a place for links and very cursory descriptions, can remain more gated, and either issues or PRs should work.
But `jackson-docs` (https://github.com/FasterXML/jackson-docs) should be opened up. I could easily add team members with access, to allow modifications. There is the potential division between `gh-pages` (easy to do PRs for) and Wiki. I don't know if latter allows PRs, probably not? That's bit tricky. So it would require either use of `gh-pages`, or just giving access on request. I think latter is fine: so if you think you'd like to add significant content, feel free to post on `jackson-dev` (see below).

With that, on coordination: I think `jackson-dev` is probably the best place for initial coordination; `jackson-user` is more for usage questions, and `jackson-dev` about development of Jackson. Documentation affects both, obviously, but planning of documentation is probably more latter, and then  the results should be announced on `jackson-user`.
On coordination: I think it should be enough for contributors to mention they are planning/writing changes, or additional content. At least if and until we have issues with overlapping and/or conflicting work.

One other idea: maybe we could also "import" good content -- so people could write blog entries, gist pages, whatever, and then propose addition? So even if we do not have an authoring system or CMS, we could have sort of pipeline. I actually like this idea; it could lead to light-weight collaboration -- someone writing something, then proposing inclusion, and others could suggest minor edits, and also where it should live within fasterxml Github space.

This is not going to be an ideal system, but I think it would be some improvement over current limited ability, where we can mostly link to external content.

So those are my ideas. How do others feel? I'm sure this can be improved upon a lot.

-+ Tatu +-

ps. Actually, external content is great, too -- I'd rather link to good content elsewhere than have no content, or more limited content within fasterxml sites.


--
Reply all
Reply to author
Forward
0 new messages