Skip to content

Support for union types #17

@joelittlejohn

Description

@joelittlejohn

Original author: joelittl...@gmail.com (February 23, 2011 10:31:27)

The idea of union types is potentially quite incompatible with Java so we may end up with a fairly naive implementation here that offers support for union types only in the most basic sense. I'd like to think that jsonschema2pojo could at least avoid messy failure when a union type is encountered.

I have a few ideas for how this could be implemented:

  1. On detecting a union type, just take the first type and proceed as normal. This is not fantastically useful (union types would still be avoided when writing schemas for jsonschema2pojo) but at least code generation could continue.
  2. On detecting a union type, create some marker interface which all types in the union will implement. This allows relationships between the POJOs to remain rigidly enforced. This can't work for primitive types though, e.g. {"type" : ["integer","string"]}.
  3. On detecting a union type, proceed to generate the two or more types specified (if they are complex), but mark any usages of that union type as java.lang.Object. Relationships between the generated POJOs become a bit more difficult to understand, and it becomes quite easy to compose Java objects in a way that will produce invalid JSON.

Both option 2 and 3 would likely need to take advantage of Jackson's polymorphic deserialization features:
http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
http://jira.codehaus.org/browse/JACKSON-91

At the moment, I'm thinking Option 3 is the most sensible/useful.

Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=17

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions