@JsonPropertyOrder not working with jackson-module-jsonSchema

4.6k views Asked by At

I'm using the latest branch for Jackson module - jackson-module-jsonSchema i.e. 2.4.4-Snapshot.

I'm trying to use the @JsonPropertyOrder annotation to maintain the order of the POJO attributes, but it seems to be not respecting the annotation.

My sample POJO is as follows -

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"a", "b"})
class Pojo {
    private String a;
    private String b;

    public Pojo(final String a, final String b) {
        this.a = a;
        this.b = b;
    }

    public void setA(final String a) {
        this.a = a;
    }

    public void setB(final String b) {
        this.b = b;
    }

    public String getA() {
        return this.a;
    }

    public String getB() {
        return this.b;
    }
}

Jackson code is as follows -

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class Test {

    public static void main(String[] args) throws JsonProcessingException {
        // TODO Auto-generated method stub
        final ObjectMapper mapper = new ObjectMapper();
        final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(mapper.constructType(Pojo.class), visitor);
        final JsonSchema jsonSchema = visitor.finalSchema();            
        System.out.println(mapper.writeValueAsString(jsonSchema));
    }

}

Json Output is as follows -

{
    "type": "object",
    "id": "urn:jsonschema:Pojo",
    "properties": {
        "b": {
            "type": "string"
        },
        "a": {
            "type": "string"
        }
    }
}

Can somebody suggest if I'm doing something wrong or do we need to open an issue over jackson, since I see that this kind of issue is been already closed in 2.3.2 (https://github.com/FasterXML/jackson-dataformat-xml/issues/91)?

1

There are 1 answers

0
tyagi On

Use @JsonPropertyOrder(alphabetic=true) This will order all the properties in alphabetical order Works with Jackson 1.9.13