1

I have a Gender enum (which I can't edit) which is used in a Java class. Now, when I need to Serialise the Java class to JSON, I want the value of the Enum as part of the JSON and not the Enum name. For example below in my enum, and when this enum is serialized, I want the value as {"gender":"Male"} I am using:

String underWritingJSONString = objectMapper.writeValueAsString(myObject);

public enum Gender {
    MALE("Male"),
    FEMALE("Female");

    Gender(String gender) { 
        this.name = gender;
    }
    private String name; 

    String toValue() {
        return name;
    }
}

expected result = {"gender":"Male"}

current result = {"gender":"MALE"}

Following is the sample class

  public class MyObject {

    @JSONField
    public Gender gender;

    public MyObject() {

    }


    public Gender getGender() {
        return this.gender;
   }
}
2
  • As mentioned above, I can't edit the enum. It is part of anothe liberary. I need to either do some change in myObject class which is using this enum or in ObjectMapper settings Commented Sep 11, 2019 at 6:46
  • @ode-master-88 Your enum is not completed could you add/edit the enum(full declaration with methods) As it is accepting a paramete ther should be a constructor and is there a toString method by any chance? Commented Sep 11, 2019 at 7:27

3 Answers 3

1

You need to add a method with annotation @JsonValue:

enum Gender {
    MALE("Male"),
    FEMALE("Female");

    Gender(String gender) { // constructor
        this.name = gender;
    }
    private String name; // variable

    @JsonValue
    String toValue() {
        return name;
    }
}

This method will be called when you are serializing your object to JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

As mentioned above, I can't edit the enum. It is part of anothe liberary. I need to either do some change in myObject class which is using this enum or in ObjectMapper settings.
it is the same as your above sample without the @JsonValue annotation
1

If enum has toString() method which returns the value then

mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

Is enough. But your enum don't have that as @Dinesh Kondapaneni mentioned you shold write a custom serializer like

class GenderSerializer extends JsonSerializer<Gender> {

@Override
public void serialize(Gender value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (null == value) {
    } else {
        try {
            gen.writeString(value.toValue());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

}

And use it in your MyObject as

@JsonSerialize(using = GenderSerializer.class)
public Gender gender;

FYI: Am using jackson here

1 Comment

this should be selected as the correct answer . yes this is enough :mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
0

(Jackson 2.6.2 and above) you can now simply write:

public enum Gender {
 @JsonProperty("Male")
 MALE,
 @JsonProperty("Female")
 FEMALE
  }

2 Comments

As mentioned above, I can't edit the enum. It is part of anothe liberary. I need to either do some change in myObject class which is using this enum or in ObjectMapper settings
will it be helpful for you? Enum Custom Deserilization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.