3

Let's say I have an enum:

public enum ParameterList {
    FREQUENCY_ID("500");
    ...
    lot's of different constants here
    ...

    private final String param;

    ParameterList(String param) {
        this.param = param;
    }

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

And let's say I have a DTO with field:

private String frequency;

Let's say I have a JSON:

{"500" : "100Hz"}

I want to map this json to my dto, so DTO.frequency will have a value of json's "500" field (which will be "100Hz").

I understand that only constants must be used as attribute values, but is there some workaround to make the following work?

@JsonProperty(ParameterList.FREQUENCY_ID)
private String frequency;

(@JsonProperty is com.fasterxml.jackson.annotation.JsonProperty, version 2.8.0)

The idea is to minimize the code edits when the enum ParameterList.FREQUENCY_ID will be changed from "500", to some other value.

htshame
  • 6,599
  • 5
  • 36
  • 56

2 Answers2

5

You could try the following:

public enum ParameterList {

    FREQUENCY_ID(Constants.FREQUENCY_ID_VALUE);

    private final String value;

    ParameterList(String value) {
        this.value= value;
    }

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

    public static class Constants {
        public static final String FREQUENCY_ID_VALUE = "500";
    }
}

Then use:

@JsonProperty(ParameterList.Constants.FREQUENCY_ID_VALUE)
private String frequency;
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • 1
    This works because the JsonProperty requires a compile time string. Java allows an annotation to accept enums at compile time, but only if they accept the type needed. Whilst it doesn't answer OP's question directly, it may be a solution. I'm still interested in seeing a solution that would probably required a custom Jackson annotation that accepts the enum of the correct type. – Ryan Leach May 17 '18 at 10:08
  • 1
    @htshame if you are using a constant, do you even need the enum? – Ryan Leach May 17 '18 at 10:09
  • 1
    @RyanTheLeach I need the enum because I use this constant in lot's of places, and there are a lot of similar constants. And the main point is that this constants will change from time to time due to new requirements. I think this solution works perfectly for my needs. – htshame May 17 '18 at 10:12
2

The json is a little weird in regards that {"admin": "superuser"} would be more clear if it were like {"role": "superuser"} (or userType instead of role). However, to map a value to an enum, all you have to do is to specify it in your DTO. For your json and an enum holding also superuser as a value

public enum UserType {
    ADMIN, SUPERUSER
....
}

all you have to do is to make your DTO have the enum as type

private UserType admin; // <-- admin because that's your key's name (maybe change that)

Note that you could also use

@JsonProperty("admin") private UserType userType; // or whatever name you like 

or something like this, if you can't change the json.

baao
  • 71,625
  • 17
  • 143
  • 203
  • Sorry I mislead you. The point of my question was a bit different. I've updated the question, hope it makes more sense now – htshame May 17 '18 at 09:55