Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Thoughts on merging JAXB annotations and JSON binding annotations

Currently, there are several implementations that provide JSON to object binding in Java. Several actually use the existing JAXB annotations that are XML driven. This results in oddities that you are exposing JSON binding rules with annotations that target XML (ie: @XmlType, @XmlValue, etc). As a result, there is a future spec coming for an official JSON bindings recommendation. This will most likely result in a new set of annotations (ie: @JsonType, @JsonValue). This will end up requiring double annotations leading to metadata bloat. The problem is that despite the two being very closely tied together, there are several differences. One could argue that JSON is a subset of XML, although that is not entirely accurate.

I would propose that indeed we have two separate annotations and two separate specifications that are aligned as consistently as possible (similar named annotations for common use cases such as @XmlValue and @JsonValue). I would propose a third joint effort spec that creates an aggregation of the common cases using higher order annotations that combine the two groups of annotations. Several other Java libraries already do this such as CDI with stereotype definitions. You essentially annotate annotations to build them up as inherited annotations.

For example, consider a future class such as:

public class Customer {
    private User user;
    private String type;
    private transient int state;

This could get only worse if more media types are supported such as YAML. One could define a new set of annotations to simplify this use case. The name could probably be made much better, but I will use MediaType for now as the prefix.

public @interface MediaTypeValue {

Now with this higher order annotation, we simply annotate:

public class Customer {
    private String type;

JAXB implementations and JSON Binding implementations could both handle that as they would be able to view the MediaTypeValue annotation and reference the associated XmlValue or JsonValue type specific to that implementation.

To further this case, you can take the parts of JAXB that do not translate to JSON and utilize those annotations directly, or vice-versa. As a developer, you can view the docs for the higher order annotations and see exactly what JAXB/JSON binding annotations they are composed of in case you wanted to break it apart and be more specific.

public class Customer {
    @MediaTypeValue private String type;
    private String otherProperty;
    private Object otherValue;


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.