It's possible to check ahead of time if a given type can be serialized to JSON (i.e. convert from strongly typed to weakly typed format), but there isn't a way to check if a given type can be deserialized to a POJO until you have an actual JSON value at runtime (i.e. convert from weakly typed format to strongly typed format).
In general, the rules for a type being serializable are simply:
- The class must be accessible (public or protected)
- If the class is an inner class it must be static
- Zero or more public fields/getters that contain/return serializable types
In general, the rules for a type being deserializable are:
- The class must be accessible (public or protected)
- If the class is an inner class it must be static
- Zero or more public fields/setters that contain/return deserializable types
- The class must have a public no-args constructor (or a ctor annotated with
@JsonbCreator
)
- OR The class must have a public static factory method annotated with
@JsonbCreator
You may find that what constitutes a "convertible type" is a very broad criteria in JSON-B (or any other Java JSON library). For example, the JSON string {}
can be deserialized into any Type that has a public no-args constructor. Likewise, a Java class that has no public fields or getter methods will serialize to the JSON string {}
.
Since a convertible type is such a broad term, it would be helpful if you specified a bit further what you are trying to achieve -- maybe there is a more specific approach I could suggest.
Additional info:
Many of the commonly used classes from the JDK are supported. See sections 3.3 - 3.5 of the JSON-B spec for the full list. Aside from the built-in classes, requirements are specified in section 3.7 of the JSON-B spec.
For deserialization there are more requirements than serialization. They are described in detail in section 3.7 and section 4.5.