3

I have got a scenario to return list of status as json which are available in Status Enum, My Enum Looks below

Example:-

public enum Status {

CREATED("100", "CREATED"), UPDATED("200", "UPDATED"), DELETED("300", "DELETED");

private final String id;
private final String name;

private Status(String id, String name) {
    this.id = id;
    this.name = name;
}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return name;
}

public List<Map<String, String>> lookup() {
    List<Map<String, String>> list = new ArrayList<>();
    for (Status s : values()) {
        Map<String, String> map = new HashMap<>();
        map.put("ID", s.getId());
        map.put("name", s.getName());
        list.add(map);
    }
    return list;

}
}

need the output like this:

[{id:"100",name:"CREATED"},{id:"200", name:"UPDATED"}...] I have written lookup method with List Of maps to build the response, Is there any better way or utility to convert the Enum to Object with all the properties available in Enum.

Is there any better way to do this?

niemar
  • 612
  • 1
  • 7
  • 16
Mahesh B
  • 33
  • 1
  • 7

4 Answers4

2

You can use Jackson to convert to JSON. Just include @JsonFormat(shape = Shape.OBJECT) at Enum declaration. This should give you the result.

akhan001
  • 66
  • 5
1

You can use a library for JSON serialization like Gson or Jackson, and implement custom serialization in it.

Example Gson custom serializer:

class StatusSerializer implements JsonSerializer<Status> {
    public JsonElement serialize(Status status, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject object = new JsonObject();
        object.addProperty("ID", status.getId());
        object.addProperty("name", status.getName());
        return object;
    }
}

You then use it as follows:

Gson gson = new GsonBuilder()
        .registerTypeAdapter(Status.class, new StatusSerializer())
        .create();
String json = gson.toJson(Status.CREATED);

which yields:

{"ID":"100","name":"CREATED"}
Tomasz Linkowski
  • 4,386
  • 23
  • 38
1

You can use Stream API in Java 8. And use parallel to good perfomance.

List<Map<String, String>> list = Stream.of(Status.values()).parallel().map(temp -> {
        Map<String, String> obj = new HashMap<String, String>();
        obj.put("id", temp.getId());
        obj.put("name", temp.getName());
        return obj;
    }).collect(Collectors.toList());
VuNT
  • 21
  • 3
  • 1
    Using `parallel()` here will only degrade performance - see e.g. [this great answer for details](https://stackoverflow.com/a/20375622/2032415). – Tomasz Linkowski Oct 03 '18 at 09:00
  • @TomaszLinkowski Really, let me take the test. I have not used much Stream API. Thank you for your comment. – VuNT Oct 03 '18 at 09:09
  • 1
    You're welcome. Keep in mind that to run such a microbenchmark properly you need to use something like [Java Microbenchmark Harness](http://tutorials.jenkov.com/java-performance/jmh.html). – Tomasz Linkowski Oct 03 '18 at 11:08
0
Arrays.stream( Status.class.getEnumConstants() )
   .map( e -> Map.of( "id", e.getId(), "name", e.getName() )
   .collect( Collectors.toList() );

I’m on mobile and can’t test but you get the general idea

Patrick
  • 12,336
  • 15
  • 73
  • 115
Solace
  • 2,161
  • 1
  • 16
  • 33