Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

java - Jackson custom annotation for custom value serialization

I'm trying to create custom jackson annotation that would affect serialization value.

Meaning:

class X {
@Unit("mm") int lenght;
 ...
}

Now serializaling object X(10) would result in:

{
  "lenght" : "10 mm"
}

How can I achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// Create own annotation storing your unit value

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@interface Unit {
    String value();
}

// Create custom serializer checking @Unit annotation

class UnitSerializer extends StdSerializer<Integer> implements ContextualSerializer {

    private String unit;

    public UnitSerializer() {
        super(Integer.class);
    }

    public UnitSerializer(String unit) {
        super(Integer.class);
        this.unit = unit;
    }

    @Override
    public void serialize(Integer value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeString(String.format("%d %s", value, unit));
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        String unit = null;
        Unit ann = null;
        if (property != null) {
            ann = property.getAnnotation(Unit.class);
        }
        if (ann != null) {
            unit = ann.value();
        }
        return new UnitSerializer(unit);
    }
}

@NoArgsConstructor
@AllArgsConstructor
@Data
class X {
    @JsonSerialize(using = UnitSerializer.class)
    @Unit("mm")
    private int length;
}

public class Runner {
    public static void main(String[] args) throws JsonProcessingException {

        X x = new X(10);
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(x));
    }
}

Result:

{"length":"10 mm"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...