0

On server I have something like this

public class Instrument {
    private String myPropOne;
    private String myPropTwo;
    private String myPropThree;
    public String getMyPropOne() {
        return myPropOne;
    }
    public void setMyPropOne(String myPropOne) {
        this.myPropOne = myPropOne;
    }
    public String getMyPropTwo() {
        return myPropTwo;
    }
public void setMyPropTwo(String myPropTwo) {
    this.myPropTwo = myPropTwo;
}
public String getMyPropThree() {
    return myPropThree;
}
public void setMyPropThree(String myPropThree) {
    this.myPropThree = myPropThree;
    }
}

and on Browser side it a map is to be sent

{ my_prop_one : 'val1', my_prop_two : 'val2', my_prop_three : 'val3'}

One way is to create define toMap() methods and put all the properties one by one and do the same when it is coming from UI-side something like fromMap()

Now question is I want write some common function

set('my_prop_one', 'val2')

so that it will look up the instance and set correct value, i will be extending this class to create more instruments types with different properties I can do this using annotations

public class Instrument {
    private String myPropOne;
    private String myPropTwo;
    private String myPropThree;
    @KeyMap(value="my_prop_one")
    public String getMyPropOne() {
        return myPropOne;
    }
    @KeyMap(value="my_prop_one")
    public void setMyPropOne(String myPropOne) {
        this.myPropOne = myPropOne;
    }
    @KeyMap(value="my_prop_two")
    public String getMyPropTwo() {
        return myPropTwo;
    }
    @KeyMap(value="my_prop_two")
    public void setMyPropTwo(String myPropTwo) {
        this.myPropTwo = myPropTwo;
    }
    @KeyMap(value="my_prop_three")
    public String getMyPropThree() {
        return myPropThree;
    }
    @KeyMap(value="my_prop_three")
    public void setMyPropThree(String myPropThree) {
        this.myPropThree = myPropThree;
    }
}

I will have to write annotations twice. is there any better way to do this? Some thing like I add annotations on properties and getter/setter get somehow linked and can be used to extract properties, some way to create a hashMap?

JsonUtil I am already using gives me this out put

{ myPropOne : 'val1', myPropTwo : 'val2', myPropThree : 'val3'}

But again setter/getter for one property? how to do that? EDIT 3 :

@JsonAnyGetter & @JsonAnySetter

like explained in example here But they work on only map not real properties which is my requirement.

2
  • 1
    Is it a requirement that you roll your own solution to this? Frameworks like Jackson (github.com/FasterXML/jackson) and Gson (code.google.com/p/google-gson) exist to do this for you. Commented May 7, 2014 at 17:19
  • 1
    No there's no such requirement, I am already using JackSon. But how do I make my getter/setter work and mapping of fields? Commented May 7, 2014 at 17:23

2 Answers 2

3

Have you tried https://github.com/flori/json-utils? it contains functionalists you are asking for.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're using Jackson (per your comment) then something like this should handle it all for you automatically:

public class Instrument {
    @JsonProperty private String myPropOne;
    @JsonProperty private String myPropTwo;
    ...
}

Then you can use the ObjectMapper class to create your JSON by calling:

Instrument instrument = new Instrument();
instrument.setMyPropOne("val1");
// set other values

ObjectMapper mapper = new ObjectMapper();
// configure mapper if needed
mapper.writeValueAsString(instrument);

2 Comments

Thanks, but I still could not find the answer, because it does not completely solve my problem, i need setter/getters, jackson provides something JsonAnyGetter & JsonAnySetter, but still not what I am looking for. because these work for additional map, not for properties.
One more try, perhaps @JsonValue is what you need? It lets you define a method that returns a Map which is then used instead of fields to create your JSON object. It's sort of like toString() but toJSON() instead. fasterxml.github.io/jackson-annotations/javadoc/2.1.0/com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.