Search code examples
javajsonpojo

Generic pojo for representation of a JSON object


Simply I'm trying to write a POJO that represents a JSON object similar to this :

{
    "errorCode": "SYS101",
    "errorMessage": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }   
}

Because the name of each key is random unique and not in any pattern and the count could be too much, writing like the following is not useful :

public class Response {
    private String errorCode;
    private ErrorMessage errorMessage;

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public ErrorMessage getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(ErrorMessage errorMessage) {
        this.errorMessage = errorMessage;
    }
}

public class ErrorMessage {

    private String key1;
    private String key2;
    private String key3;

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }

    public String getKey2() {
        return key2;
    }

    public void setKey2(String key2) {
        this.key2 = key2;
    }

    public String getKey3() {
        return key3;
    }

    public void setKey3(String key3) {
        this.key3 = key3;
    }
}

I'm wondering if there is a generic way to write a POJO that represents that JSON?

Update : the keys are in random string and they are not in any numeric pattern :

for instance you can consider like this :

{
    "errorCode": "SYS101",
    "errorMessage": {
        "firstName": "first name could not be empty",
        "age": "age could not be less thatn 18",
        "gender": "could not be null"

    }   
}

Solution

  • Something like this maybe? It is not very "generic" but you can add as many error messages as you want and it still keeps the structure of your original JSON, where errorMessageis an Object (and not an array)

    public class Response {
        private String errorCode;
        private Map<String, String> errorMessage;   // JSONs are like maps with a key-value mapping
    
        public Response(String errorCode, String... errorMessages){   // example for a constructor with the logic to assign the errorMessages (no setters)
            this.errorCode = errorCode; 
            this.errorMessage= new HashMap<>();   // first you initialize the map
            int count =0;                         // a counter to name your "keyX" keys
            for(String msg:errorMessages){ 
               errorMessage.put("key"+(++count),msg); // and filling the map
            }          
        }
    }
    
    

    Update after comment

    public class Response {
        private String errorCode;
        private Map<String, String> errorMessage;   // you can still keep this map
       
        public Response(String errorCode){        // I suppose the error messages can be added separately in setters 
            this.errorCode = errorCode; 
            this.errorMessage= new HashMap<>();  
        }
    
        public void addErrorMessage(String key, String value){
           if(this.errorMessage == null){
                this.errorMessage= new HashMap<>();   // in case you don't want to initialize the map in your constructor
           }
           this.errorMessage.put(key, value);          // there you go, just put any key value you like
        }
    }