boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

@JsonValue for ENUM #349

Closed mann-ed closed 8 years ago

mann-ed commented 8 years ago

Is there a way with enum to set that value field i want output when writing to json?

Such as

public enum DelayType {
    MIll(1, "Delay by milliseconds"), SEC(10, "Delay by seconds"), MIN(20, "Delay by minutes"), HOUR(30,
            "Delay by hour"), DAY(40, "Delay by day(s)"), YEAR(50, "Delay by years. HUM REALLY?");

    private Integer delayCode;

    private String delayDesc;

    private DelayType(Integer delayCode, String delayDesc) {
        this.delayCode = delayCode;
        this.delayDesc = delayDesc;
    }

    /**
     * @return
     */
    @JsonValue
    public Integer getDelayCode() {
        return this.delayCode;
    }

    /**
     * @return
     */
    public String getDelayDesc() {
        return this.delayDesc;
    }

The field with @JsonValue is what i want to be placed in the output json.

This is a feature request, or some direction on how i can get my desired output.

Current Output:

{"alertClosed":true,"delayType":"MIN","notifyDelay":14}

Desired Outut:

{"alertClosed":true,"delayType":20,"notifyDelay":14}

RichardHightower commented 8 years ago

You can use @Ignore to ignore the fields and then expose the properties.