1

I'm currently working on a project where I upload a file through their REST API and their server returns the following in JSON.

[{"code":0,"id":"19348139481","name":"file.bin"}]

with "code" having the possible 3 values:

  • 0 upload successful
  • 1 file too big
  • 2 could not save file

I am able to get each pair of key/value after removing the brackets, but is there any way that I associate the "code" with its message? What I would like to do is to define the code like in C++

 define 0 UPLOAD_SUCCESSFUL
 define 1 FILE_TOO_BIG
 define 2 COULDNT_SAVE_FILE

So that way when i get the "code" i can display the corresponding message like:

System.out.println(code.msg);
greg-449
  • 109,219
  • 232
  • 102
  • 145
retArdos
  • 135
  • 2
  • 4
  • 17
  • You can use an array, you can use a switch statement in a function, you can use an `enum`, you can use a `Map`... you have many possibilities :) I would personally avoid using an array because it would force you to use consecutive code numbers (you couldn't use the code `99` after `0`,`1` and `2`). The switch statement is not very elegant... if you care about performance maybe the `Map` would be the best choice – Oneiros May 24 '17 at 13:58
  • @Oneiros you should put that in an answer, and add a piece of code to illustrate your Mpa suggestion. – fvu May 24 '17 at 14:12
  • Your best solution for an enumerated list of possible values is an enum. – chrylis -cautiouslyoptimistic- May 24 '17 at 14:14
  • @fvu I've no time to write and share a compilable and tested piece of code right now, that's why I just added a comment. Here you can find some complete answers about creating a static Map: https://stackoverflow.com/questions/507602/how-can-i-initialise-a-static-map – Oneiros May 24 '17 at 14:18

3 Answers3

1

Since the list of possible return values is stable an enum could be used. For example:

public class App {

    public static void main(String[] args) {
        for (int responseCode = 0; responseCode <= 2; responseCode++) {
            UploadResponse response = UploadResponse.getUploadResponse(responseCode);
            System.out.println(response.getMessage());
        }
    }

    private enum UploadResponse {
        SUCCESS(0, "upload successful"),
        FILE_SIZE_ERROR(1, "file too big"),
        FILE_SAVE_ERROR(2, "could not save file");

        private int code;
        private String message;

        private UploadResponse(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getMessage() {
            return message;
        }

        public static UploadResponse getUploadResponse(int code) {
            for (UploadResponse response : UploadResponse.values()) {
                if (response.code == code) {
                    return response;
                }
            }

            throw new IllegalArgumentException("Unsupported UploadResponse code: " + code);
        }
    }
}
Andrew S
  • 2,509
  • 1
  • 12
  • 14
0

A simple solution might be, if of course the return codes are actually 0, 1 and 2:

String message[] = {"upload successful","file too big","could not save file"};
...
System.out.println(message[code]);

Sidenote: if I am able to get each pair of key/value after removing the brackets actually means that you retrieve code by manually breaking down the JSON message: don't. Use a proper JSON API like Jackson or GSON. JSON is important enough in today's coding landscape to invest in learning a robust, versatile API.

fvu
  • 32,488
  • 6
  • 61
  • 79
  • actually i use GSON but with the brackets [] surrounding the json it produces an error. JsonObject repCode = new Gson().fromJson(result.toString().replaceAll("[\\[\\]]", ""), JsonObject.class); System.out.println(repCode.get("code").getAsInt()); Displays: 0 (for example) – retArdos May 24 '17 at 14:30
  • For the upload response yes the code are 0 1 and 2. However for other functions it's not so i can't use it. – retArdos May 24 '17 at 15:11
  • The error is caused by the fact that what you get back is an **array** and should be handled as such. You can change your code to `JsonObject[] repCode = new Gson().fromJson(result, JsonObject[].class); System.out.println (repCode[0].get("code").getAsInt());`. Much cleaner than altering the input string. – fvu May 24 '17 at 15:15
-1

A simple solution could be to create a method that uses a switch case. Something like this:

public static String getCodeMessage(int code) {
    switch (code) {
    case 0: return "UPLOAD_SUCCESSFUL";
    case 1: return "FILE_TOO_BIG";
    case 2: return "COULDNT_SAVE_FILE";
    default: return "Uknown code";
    }
}

Then you could call it by using System.out.println(getCodeMessage(code));

Sore Throat
  • 189
  • 5
  • You don't need that when you use return. – Sore Throat May 24 '17 at 14:00
  • Thanks, that's what i was gonna do at first but i thought it would be too heavy, especially since the website i query has other functions with way more error codes. Probably go with enum and i'll see what i can do. – retArdos May 24 '17 at 14:35