1

I'm new to JSON hence the answer to my question would be a huge help!

I have an enum like below:

enum Error
{
    private final String message; 

    INVALID("failed"),
    VALID("succeeded");

    Error(String message){
       this.message = message;
    }

}

And my class is like:

class Response {
    String id;
    Error error;
}

How do I create a sample JSON payload for this?

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Phoenix
  • 8,695
  • 16
  • 55
  • 88
  • I don't know java - but usually it's a value of enum underlying type like {"id":"someId", "error":"enum_value_here"} – lavrik Nov 01 '13 at 16:09

1 Answers1

0

If you instantiate and serialize you class using Gson, you will get a JSON string that is the exactly payload you are looking for.

For example, if you execute this:

 Response r = new Response();
 r.id="AA";
 r.error = Error.INVALID;

 Gson defaultGson = new Gson();
 System.out.println(defaultGson.toJson(r));

you will get

{"id":"AA","error":"INVALID"}

Of course, you could use alternative way to serialize/deserialize your enum like asked here

Community
  • 1
  • 1
giampaolo
  • 6,906
  • 5
  • 45
  • 73