0

We have a common API to get the payload(consider JSON) along with client ID from different clients.

How can I convert the payload to appropriate POJO using client ID.

Rough Idea is to deserialise the payload as java object and based on the clientId, convert the JAVA object to respective POJO.

I have a ENUM class in place for the config which looks something as follows.

public enum SourceEnum {
    ClientId1(A.class);
    ClientId2(B.class);

    Class clazz;
    SourceEnum(Class clazz) {
        this.clazz = clazz;
    }

    public Class getClazz(){
        return this.clazz;
    }
}

We can use SourceEnum.valueOf(clientId).getClazz() to get the Class.

How can I use this information to convert the java Object to respective class object?

And is this the correct way of solving this problem? Want to try out if there are any better ways of solving this

starkk92
  • 5,754
  • 9
  • 43
  • 59
  • 1
    You will want to use [Jackson](https://github.com/FasterXML/jackson-databind/) to do what you need. – ericbn Apr 03 '17 at 17:26
  • 2
    Possible duplicate of [JSON to object in Java](http://stackoverflow.com/questions/30587161/json-to-object-in-java) – ericbn Apr 03 '17 at 17:29
  • 2
    Hint : the term casting in your question is misleading. You mean converting. – GhostCat Apr 03 '17 at 17:30
  • 1
    Why would you want to use different classes for different clients?! Just use the same class, just different instances with different values... And of course Jacson would be the best way to go about it – Michael Gantman Apr 03 '17 at 17:30

1 Answers1

-1

If you want to convert the given json into java object, try using Gson by google.

Darish
  • 11,032
  • 5
  • 50
  • 70
  • 1
    link-only (or link-mostly) answers are frowned-upon and quickly voted down. Please consider expanding your answer to be self-sufficient (external inks can become unavailable for many reasons making the answer incomplete and almost useless) – blurfus Apr 03 '17 at 17:30