1

I am working on code written few years back. It builds POJO out of JSON. JSON has a statusType field. Value of all other fields are dependent on statusType field. If statusType is SUCCESS, fields successCode, totalValue, concession etc are populated, on the other hand if it is FAILURE, fields errorCode, exceptionType, retryCounts are populated.

{
   "statusType": "SUCCESS",
   "successCode": "20.304",
   "total": 1000
}

{
   "statusType": "FAILED",
   "errorCode": "50.31",
   "retryCounts": 2
}

POJO is defined like following:

public class JsonPOJO {
    public String statusType;
    public String successCode;
    public int retryCounts;
    public int total;
    public String errorCode;
}

Client code is like following:

JsonPOJO pojo = buildPojo();
if (pojo.getStatusCode.equals(“SUCCESS”)) {
       String successCode = pojo.getSuccessCode();
} else {
       String errorCode = pojo.getErrorCode();
}

Above code is scattered throughout (around 10 places). Very soon, we'll be getting another status code "IN_PROGRESS", which will introduce few more fields in JSON and then POJO will have few more fields and consequently more if/else statements throughout the code checking for statusType. I don't want to go through this route. I want to elegantly refactor code, so that if I get another status code (for example, "SUSPENDED"), I need not touch whole codebase again.

I couldn't find (maybe my Googling skills suck) any pattern, in general, to handle this kind of situation, so I am reaching out to experts here for their opinions/thoughts.

Patrick
  • 1,717
  • 7
  • 21
  • 28
Skandh
  • 426
  • 3
  • 18
  • Unless you require building something from scratch yourself, consider using a library like GSON, which already has this problem covered well, [see this SO question](https://stackoverflow.com/questions/5128442/how-to-convert-a-string-to-jsonobject-using-gson-library). – Tim Biegeleisen Nov 29 '17 at 05:44
  • You can look at some thing like GSON or jackson libs which will help you in creating the same – Rahul Singh Nov 29 '17 at 05:45
  • Thanks, it'll solve JSON parsing problem. Is there any better way to refactor above code so that I won't need to add another if statement in client code at multiple places when new status code comes? – Skandh Nov 29 '17 at 06:15

1 Answers1

0

Adding a new status code alone doesn't add more fields. If you add more JSON, you need to update the Java, otherwise, you can't get the new fields.

I know Jackson has the option to store all extra JSON values into a Hashmap. I've not used Gson enough to see that feature.

However, you can also forget about writing Java yourself and use a Maven plugin to generate your classes simply from an JSON schema definition

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks, it'll solve JSON parsing problem. Is there any better way to refactor above code so that I won't need to add another if statement in client code at multiple places when new status code comes? – Skandh Nov 29 '17 at 06:16
  • You can use a Hashmap for the status and the Command Pattern, but you need to then refactor the code into a bunch of classes rather than simple if / switch statement. https://dzone.com/articles/design-patterns-command also https://stackoverflow.com/questions/1554180/why-is-the-if-statement-considered-evil – OneCricketeer Nov 29 '17 at 06:23