Force Jackson to add addional wrapping using annotations

前端 未结 7 1029
渐次进展
渐次进展 2021-02-12 20:41

I have the following class:

public class Message {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(St         


        
7条回答
  •  庸人自扰
    2021-02-12 21:22

    It was sad to learn that you must write custom serialization for the simple goal of wrapping a class with a labeled object. After playing around with writing a custom serializer, I concluded that the simplest solution is a generic wrapper. Here's perhaps a more simple implementation of your example above:

    public final class JsonObjectWrapper {
        private JsonObjectWrapper() {}
    
        public static  Map withLabel(String label, E wrappedObject) {
            HashMap map = new HashMap();
            map.put(label, wrappedObject);
            return map;
        }
    }
    

提交回复
热议问题