0

I have a use case where we have to send different JSONs to different servers.

The difference is only between JSON keys, the meaning the keys carry is same and so is the data.

For example server XYZ wants JSON data to be sent in this format:

{ "firstName":"Sam", "lastName":"Jones"}

Now server ABC wants JSON data to be sent in this format:

{ "fName":"Sam", "lName":"Jones"}

And firstName and lastName data is populated via a POJO.

So, How do I achieve this? I do not want to clutter the code with if-else conditions.

But wnat to have something which would work like a template loaded dynamically and create the JSON data and also retrieve it back to the POJO.

Sam
  • 2,352
  • 4
  • 32
  • 45
  • Use a JSON library like GSON or Jackson (or another). If you don't want to use any of these fancy libraries, you can manipulate your own JSON templates using [this one](http://json.org/java/) – Luiggi Mendoza Mar 22 '13 at 05:34
  • if order of the Keys is same then you may go with the index. Like `1st` will always the `FirstName` and `2nd` would always the `LastName` and So on – Muhammad Haseeb Khan Mar 22 '13 at 05:36
  • @MuhammadHaseebKhan The order of the keys could be different. – Sam Mar 22 '13 at 05:37

2 Answers2

0

You should create two POJOs. One for each server. Each POJO can have different property names to satisfy each of the server's requirements.

Or the POJOs can have the same property names, but be annotated to generate different JSON properties. A JSON library like Jackson can do this using the JsonProperty annotation.

Rob Breidecker
  • 604
  • 1
  • 7
  • 12
  • Is it possible to create these POJOs dynamically? So that depending on the server the respective POJO could be passed to Jackson library to create the JSON? Why I am asking is because in future I would store this POJO as a template in DB and use it for generation of JSON. This way I will just add the entry in DB and I am ready with new JSOn for any new Server we would be communicating. – Sam Mar 22 '13 at 05:47
  • Creating stubs from definitions is what Web Services try to achieve with wsdl files. The real life experience is extremely painful; any small change and you have to regenerate all the stubs, parsers differ in behaviour, etc.. It is much easier to agree on the specifications of the JSON ant let every implementer of a server generate it its way. If you control the servers, just generate a jar with the stubs, and make it available. – Bruno Grieder Mar 22 '13 at 05:55
  • 1
    @Sam You might want to check out this. http://stackoverflow.com/questions/12134231/jackson-dynamic-property-names – Rob Breidecker Mar 22 '13 at 06:00
  • @robbr I have one clue in my mind. What if I use JAXB for this? The idea is JAXB xml will create the POJOs and we will just have GSON annotations for each class variable so that we get the desired key name in JSON. Let me know what do you think about this? – Sam Mar 22 '13 at 11:52
  • That sounds like a great approach. – Rob Breidecker Mar 22 '13 at 14:49
  • If my answer was helpful, can you up vote it and/or select it as the correct answer? Thanks! – Rob Breidecker Mar 23 '13 at 16:52
0

How about this strategy?

1. Defines the interface to be used as a common..

interface People{
    public String getRegularFirstName();
    public String getRegularLastName();
}

2. Define each POJO with implemented interface

//class for "{ "firstName":"Sam", "lastName":"Jones"}"

class PeopleData2 implements People{
    private String firstName;
    private String lastName;

    public String getRegularFirstName(){
        return firstName;
    }
    public String getRegularLastName(){
        return lastName;
    }
    //getter setter here..
}

//class for "{ "fName":"Sam", "lName":"Jones"}"

class PeopleData1 implements People{
    private String fName;
    private String lName;

    public String getRegularFirstName(){
        return fName;
    }
    public String getRegularLastName(){
        return lName;
    }
    //getter setter here..
}

3. Make each json format deserved each POJO classes..

It is not dinamically strategy because it need to add class whe new format comes up. but it will help system scalability

namxee
  • 233
  • 1
  • 9
  • The class PeopleData1 has to follow JavaBean naming convention. What I mean is the getter getRegularFirstName should getfName. Otherwise JSOn will not be generated correcly by by JSOn library. – Sam Mar 22 '13 at 08:03