Question

I am working on a project in which I am making a rest url call to my servers which gives me back a JSON String as a response. If there are any problems with the service then it will give me either of these below JSON String as the error response -

{"error":"no user_id passed"}

or

{"warning": "user_id not found", "user_id": some_user_id}

or

{"error": "user_id for wrong partition", "user_id": some_user_id, "partition": some_partition}

or

{"error":"no client_id passed"}

or

{"error": "missing client id", "client_id":2000}

But if it is a success response, then I will get json string back as -

{"@data": {"oo":"1205000384","p":"2047935"} 

Below is my code by which I am making a call and here response variable will have above JSON string if something has gone wrong on the service side or if it is a success.

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class);

// here response has the JSON string

ClientResponse clientResponse = checkJSONResponse(response);

return clientResponse;

Currently I am returning the response as it is without checking anything. But now I am thinking to verify the response and see if it is an error, then log it with specific errors and if it is a success response, then return it.

First Option:- So should I deserialize the above json response String every time for each call, and then see whether it has success or error response.

Second Option:- Or should I just check whether response string is starting with error or warning, and if it is starting with error or warning then deserialize the response and extract the specific error messages.

Because most of the time we will be getting success response with the data back and only 2% approx we will be getting above error response back so I am thinking deserializing everytime just to extract only the error case will be expensive as compared to startsWith error or warning option and then deserializing it?

First Option-

private ClientResponse checkJSONResponse(final String response) throws Exception {

Gson gson = new Gson();
ClientResponse clientResponse = null;
JsonObject jsonObject = gson.fromJson(response, JsonObject.class); // parse
if (jsonObject.has("error") || jsonObject.has("warning")) {

    final String error = jsonObject.get("error") != null ? jsonObject.get("error").getAsString() : jsonObject
        .get("warning").getAsString();

    // log error here
    ClientResponse clientResponse = new ClientResponse(response, "ERROR_OCCURED", "SUCCESS");
} else {
    ClientResponse clientResponse = new ClientResponse(response, "NONE", "SUCCESS");
}

    return clientResponse;
}

Second Option-

private ClientResponse checkJSONResponse(final String response) throws Exception {

Gson gson = new Gson();
ClientResponse clientResponse = null;
if(response.startsWith("{\"error\":") || response.startsWith("{\"warning\":")) {
    JsonObject jsonObject = gson.fromJson(response, JsonObject.class); // parse
    if (jsonObject.has("error") || jsonObject.has("warning")) {

        final String error = jsonObject.get("error") != null ? jsonObject.get("error").getAsString() : jsonObject
            .get("warning").getAsString();

        // log error here with specific messages
        ClientResponse clientResponse = new ClientResponse(response, "ERROR_OCCURED", "SUCCESS");
    }
} else {
        ClientResponse clientResponse = new ClientResponse(response, "NONE", "SUCCESS");
    }

    return clientResponse;
}

what is the best option here for my use case? I am mainly looking on the performance point of view which option will be more efficient?

Was it helpful?

Solution

The first one is obviously better. What you have done is called premature optimization. You second case is highly error prone, imagine what happens if elements are reordered. Do not do optimizations until you are sure this is a bottle neck.

UPD: In case you have profiled you code and find out that useless object parsing is really an issue. You can assign error codes, rather then passing a string value and use lastIndexOf() method instead of startsWith(). That would prevent you from object reordering problem, but will guaranty, that "error" word is not present in a warning message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top