Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

java - Inherit model with different JSON property names

I have a class, let's say Person, that I want to populate from JSON using Jackson, but the property names vary by source. Here's how the code looks currently:

class Person {
    protected String firstName;
    protected String lastName;
    protected String address;

    public abstract void setFirstName(String firstName);
    public abstract void setLastName(String lastName);
    public abstract void setAddress(String address);

    // getters etc.
}

class PersonFormat1 extends Person {
    @Override
    @JsonProperty("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Override
    @JsonProperty("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override("address")
    public void setAddress(String address) {
        this.address = address;
    }
}

class PersonFormat2 extends Person {
    @Override
    @JsonProperty("fName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Override
    @JsonProperty("lName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override("addr")
    public void setAddress(String address) {
        this.address = address;
    }
}

As you can see, PersonFormat1 and PersonFormat2 are identical in structure, but I need different subclasses in order to specify different property names.

Is there some way to enforce the model without the boilerplate of having to redeclare and reimplement each method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way is to use PropertyNamingStrategy http://wiki.fasterxml.com/PropertyNamingStrategy

Here is nice simple demo how you can use it Link to How to Use PropertyNamingStrategy in Jackson


The other is use MixInAnnotations http://wiki.fasterxml.com/JacksonMixInAnnotations

with MixInAnnotations you can create just one Person class and Mixin for any other alternative property name setup.

public static void main(String[] args) throws IOException {
    ObjectMapper mapper1 = new ObjectMapper();
    String person1 = "{"firstName":null,"lastName":null,"address":null}";
    Person deserialized1 = mapper1.readValue(person1,Person.class);

    ObjectMapper mapper2 = new ObjectMapper();
    mapper2.addMixIn(Person.class, PersonMixin.class);
    String person2 = "{"fName":null,"lName":null,"addr":null}";
    Person deserialized2 = mapper2.readValue(person2,Person.class);
}

public static class Person {
    @JsonProperty("firstName")
    String firstName;
    @JsonProperty("lastName")
    String lastName;
    @JsonProperty("address")
    String address;

}

public class PersonMixin {
    @JsonProperty("fName")
    String firstName;
    @JsonProperty("lName")
    String lastName;
    @JsonProperty("addr")
    String address;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...