Make POJO class generated from JSON schema serializable

Mihir Shinde picture Mihir Shinde · Feb 26, 2015 · Viewed 7.2k times · Source

I am using the jsonschema2pojo-maven-plugin v0.4.7 to generate POJO classes from a JSON schema. A sample schema is as follows:

 "features": {
            "title": "Feature",
            "description": "Name and type of every feature in the model",
            "type": "array",
            "items": {
                "properties": {
                    "columnName": {
                        "description": "Name of the table column",
                        "type": "string"
                    },
                    "featureName": {
                        "description": "Name of that column's feature for the pipeline",
                        "type": "string"
                    },
                    "type": {
                        "description": "Type of the feature",
                        "type": "string"
                    }
                },
                "required": ["columnName", "type"]
            }

The resulting POJO class is somewhat as follows:

public class Feature {

    /**
     * Name of the table column
     * 
     */
    @JsonProperty("columnName")
    private String columnName;
    /**
     * Name of that column's feature for the pipeline
     * 
     */
    @JsonProperty("featureName")
    private String featureName;
    /**
     * Type of the feature
     * 
     */
    @JsonProperty("type")
    private String type;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * Name of the table column
     * 
     * @return
     *     The columnName
     */
    @JsonProperty("columnName")
    public String getColumnName() {
        return columnName;
    }

    /**
     * Name of the table column
     * 
     * @param columnName
     *     The columnName
     */
    @JsonProperty("columnName")
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    /**
     * Name of that column's feature for the pipeline
     * 
     * @return
     *     The featureName
     */
    @JsonProperty("featureName")
    public String getFeatureName() {
        return featureName;
    }

    /**
     * Name of that column's feature for the pipeline
     * 
     * @param featureName
     *     The featureName
     */
    @JsonProperty("featureName")
    public void setFeatureName(String featureName) {
        this.featureName = featureName;
    }

    /**
     * Type of the feature
     * 
     * @return
     *     The type
     */
    @JsonProperty("type")
    public String getType() {
        return type;
    }

    /**
     * Type of the feature
     * 
     * @param type
     *     The type
     */
    @JsonProperty("type")
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(columnName).append(featureName).append(type).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof Feature) == false) {
            return false;
        }
        Feature rhs = ((Feature) other);
        return new EqualsBuilder().append(columnName, rhs.columnName).append(featureName, rhs.featureName).append(type, rhs.type).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

I am using one of the POJO classes generated from the Schema in a Spark Application which requires that this class implements Serializable to be able to use it a distributed setting.

I need the resulting class to implement Serialization like follows:

public class Feature implements Serializable {

}

Does anyone know a way to make a POJO class implement Serializable? Is their a JSON schema setting to make it serializable?

I have looked all over google with no luck. Thanks in advance.

Answer

Jama Djafarov picture Jama Djafarov · Feb 26, 2015

jsonschema2pojo team says it has been implemented:

jsonschema2pojo/issues

"Fixed for 0.3.7. There is now a new extension property "javaInterfaces" that takes an array of strings, where each string is the fully qualified name of a Java interface. The generated type will implement those interfaces."