0

I've setup a Java API like this

public void convertJsonToJavaClass(URL inputJsonUrl, File outputJavaClassDirectory, String packageName, String javaClassName) throws IOException {
    com.sun.codemodel.JCodeModel jcodeModel = new com.sun.codemodel.JCodeModel();

    GenerationConfig config = new JsonToJavaConfig().getJsonToJavaConfig();

    SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
    mapper.generate(jcodeModel, javaClassName, packageName, inputJsonUrl);

    jcodeModel.build(outputJavaClassDirectory);
}

with a JsonToJavaConfig like this:

public class JsonToJavaConfig {

public GenerationConfig getJsonToJavaConfig() {
    
    // Generate the minimum possible POJO from the SCHEMA
    return new DefaultGenerationConfig() {
        @Override
        public SourceType getSourceType() { return SourceType.JSONSCHEMA; }
        
        @Override
        public AnnotationStyle getAnnotationStyle() { return AnnotationStyle.NONE; }
        
        @Override
        public InclusionLevel getInclusionLevel() { return InclusionLevel.NON_NULL; }
        
        @Override
        public boolean isGenerateBuilders() { return false; }
        
        @Override
        public boolean isIncludeToString() { return false; }
        
        @Override
        public boolean isIncludeHashcodeAndEquals() { return false; }
        
        @Override
        public boolean isIncludeGetters() { return false; }
        
        @Override
        public boolean isIncludeSetters() { return false; }

        ...etc everything set to false...
    }
}

The input Json schema file like this

{
"type": "object",
"properties": {
    "Market": {
        "type": "string"
    },
    "Dealer": {
        "type": "integer"
    },
    "Side": {
        "type": "string",
        "enum": ["Buy", "Sell", "Pay", "Receive", "Undisclosed"]
    },
    "Package": {
        "type": "string",
        "enum": ["Y", "N"]
    }
},
"required": ["Market", "Side"]
}

The POJO that's produced still has comments.

/**
 * 
 * (Required)
 * 
 */

I would like to remove annotated with @JsonProperty

@JsonProperty("Market")
public String market;

I would like the type name to have the same case as the Json. So in this case

public String Market;

Finally the enum should not have anything generated. Instead of:

public enum Side {

    BUY("Buy"),
    SELL("Sell"),
    PAY("Pay"),
    RECEIVE("Receive"),
    UNDISCLOSED("Undisclosed");
    private final String value;
    private final static Map<String, V1 .Side> CONSTANTS = new HashMap<String, V1 .Side>();

    static {
        for (V1 .Side c: values()) {
            CONSTANTS.put(c.value, c);
        }
    }

    Side(String value) {
        this.value = value;
    }

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

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

    @JsonCreator
    public static V1 .Side fromValue(String value) {
        V1 .Side constant = CONSTANTS.get(value);
        if (constant == null) {
            throw new IllegalArgumentException(value);
        } else {
            return constant;
        }
    }
}

The enum should be

public enum Side { Buy, Sell, Pay, Receive, Undisclosed }

Can this be configured already or does it require a new branch ?

rupweb
  • 3,052
  • 1
  • 30
  • 57

0 Answers0