Jackson XJC Plugin

A XJC plugin to automate application of Jackson annotations.

License

License

Categories

Categories

Jackson Data JSON
GroupId

GroupId

io.fares.bind.xjc.plugins
ArtifactId

ArtifactId

jackson-xjc-plugin
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Jackson XJC Plugin
A XJC plugin to automate application of Jackson annotations.
Project URL

Project URL

https://github.com/bertramn/jackson-xjc-plugin
Source Code Management

Source Code Management

https://github.com/bertramn/jackson-xjc-plugin.git

Download jackson-xjc-plugin

How to add to project

<!-- https://jarcasting.com/artifacts/io.fares.bind.xjc.plugins/jackson-xjc-plugin/ -->
<dependency>
    <groupId>io.fares.bind.xjc.plugins</groupId>
    <artifactId>jackson-xjc-plugin</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/io.fares.bind.xjc.plugins/jackson-xjc-plugin/
implementation 'io.fares.bind.xjc.plugins:jackson-xjc-plugin:0.0.2'
// https://jarcasting.com/artifacts/io.fares.bind.xjc.plugins/jackson-xjc-plugin/
implementation ("io.fares.bind.xjc.plugins:jackson-xjc-plugin:0.0.2")
'io.fares.bind.xjc.plugins:jackson-xjc-plugin:jar:0.0.2'
<dependency org="io.fares.bind.xjc.plugins" name="jackson-xjc-plugin" rev="0.0.2">
  <artifact name="jackson-xjc-plugin" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.fares.bind.xjc.plugins', module='jackson-xjc-plugin', version='0.0.2')
)
libraryDependencies += "io.fares.bind.xjc.plugins" % "jackson-xjc-plugin" % "0.0.2"
[io.fares.bind.xjc.plugins/jackson-xjc-plugin "0.0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-annotations jar 2.9.8
org.jvnet.jaxb2_commons : jaxb2-basics-tools jar 0.12.0

provided (3)

Group / Artifact Type Version
javax.xml.bind : jaxb-api jar 2.3.1
org.glassfish.jaxb : jaxb-xjc jar 2.3.0
org.glassfish.jaxb : codemodel jar 2.3.0

Project Modules

There are no modules declared in this project.

Jackson XJC Plugin

This plugin provides tooling to add Jackson annotations to generated Java Beans.

Usage

  1. Configure the XJC compiler

The plugin can be used with any JAXB compiler that is capable of registering XJC plugins. The plugin jar needs to be made available to the XJC compiler classpath. In maven this is not the project classpath but the classpath of the plugin that generates code from one or more XML schema.

Example configuration for the JAXB2 commons compiler:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <configuration>
    <plugins>
      <plugin>
        <groupId>io.fares.bind.xjc.plugins</groupId>
        <artifactId>jackson-xjc-plugin</artifactId>
        <version>0.0.2</version>
      </plugin>
    </plugins>
    <extension>true</extension>
    <args>
      <arg>-Xjackson</arg>
    </args>
  </configuration>
</plugin>

Enum Annotations

The plugin will now enhance the enum VehicleType with the Jackson annotations so that the values are serialised/deserialised as defined in the schema, NOT as dictated by Java convention.

@XmlType(name = "VehicleType")
@XmlEnum
public enum VehicleType {

    @XmlEnumValue("Car")
    CAR("Car"),
    @XmlEnumValue("Boat")
    BOAT("Boat"),
    @XmlEnumValue("Plane")
    PLANE("Plane");
    private final String value;

    VehicleType(String v) {
        value = v;
    }

    @JsonValue
    public String value() {
        return value;
    }

    @JsonCreator
    public static VehicleType fromValue(String v) {
        for (VehicleType c: VehicleType.values()) {
            if (c.value.equals(v)) {
                return c;
            }
        }
        throw new IllegalArgumentException(v);
    }

}

Fixed Value Attributes as Constants

When enabling the fixedAttributeAsConstantProperty binding customization, the generated code becomes non-processable by Jackson. When a xsd:attribute is fixed, the plugin will generate a read-only getter method for the attribute so Jackson can serialise the object including this piece of data.

<xsd:schema>
  <xsd:annotation>
    <xsd:appinfo>
      <jaxb:globalBindings fixedAttributeAsConstantProperty="true"/>
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:complexType name="SomeType">
    <xsd:attribute name="fixedAttribute" type="xsd:string" fixed="fixed value"/>
  </xsd:complexType>
</xsd:schema>

will generate

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeType")
public class SomeType {

    @XmlAttribute(name = "fixedAttribute")
    public final static String FIXED_ATTRIBUTE = "fixed value";

    @XmlTransient
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    public String getFixedAttribute() {
        return FIXED_ATTRIBUTE;
    }

}

Versions

Version
0.0.2
0.0.1