Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[4] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...