1

Consider the json schema:

{
    "type":"object",
    "$schema":"http://json-schema.org/draft-04/schema",
    "required":false,
    "properties":{
        "inventory":{
            "type":"object",
            "required":false,
            "properties":{
                "count":{
                    "type":"number",
                    "required":false
                }
            }
        }
    }
}

When using jsonschema2pojo, this generates a corresponding java object. Extracted from this is the following declaration for count (full class below):

@JsonProperty("count")
private Double count;

package com.test.json;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "count"
})
public class Inventory{

    /**
     * 
     */
    @JsonProperty("count")
    private Double count;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The count
     */
    @JsonProperty("count")
    public Double getCount() {
        return count;
    }

    /**
     * 
     * @param count
     *     The count
     */
    @JsonProperty("count")
    public void setCount(Double count) {
        this.arpu = count;
    }

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

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object other) {
        return EqualsBuilder.reflectionEquals(this, other);
    }

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

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

}

If the JSON interface changes, and count becomes countOfItems, both the JSON property annotation, AND the java variable name change, which affects getters and setters:

@JsonProperty("countOfItems")
private Double countOfItems;

@JsonProperty("countOfItems")
public Double getCountOfItems()

@JsonProperty("countOfItems")
public void setCountOfItems(Double countOfItems)

I would like to be able to customise the generated class so that implementing java code didn't have to change, i.e.:

@JsonProperty("countOfItems")
private Double count;

@JsonProperty("countOfItems")
public Double getCount()

@JsonProperty("countOfItems")
public void setCount(Double count)

Is this kind of feature supported using JSON schema and jsonschema2pojo? I can't find any information on how to do this on the json schema or jsonscema2pojo documentation.

Obviously this could be achieved by hand crafting the java object, but I'd rather keep using generated classes.

Clarkey
  • 1,553
  • 5
  • 22
  • 34
  • 1
    This is unrelated to jsonschema. Json-schema does not specify how types must be created in programming languages. About your problem, the only possible solution I see is to generate the POJOs just once, and from there do manual changes to mappings to schema when this change. And I doubt there exist a general solution (in fact, the use of json-schema in this type-generation is very limited). – jruizaranguren Sep 19 '14 at 10:46

0 Answers0