如何使用基于java.util.Map的类序列化Jackson [英] How to serialize with Jackson a java.util.Map based class

查看:672
本文介绍了如何使用基于java.util.Map的类序列化Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类看起来像这样:

  @JsonFormat(形状= JsonFormat.Shape.OBJECT)
公共类MyMap实现Map< String,String>
{
protected Map< String,String> myMap = new HashMap< String,String>();

protected String myProperty =my property;
public String getMyProperty()
{
return myProperty;
}
public void setMyProperty(String myProperty)
{
this.myProperty = myProperty;
}

//
// java.util.Map mathods实现
// ...
}

使用此代码的主要方法:

  MyMap map = new MyMap(); 
map.put(str1,str2);

ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig()。withAnnotationIntrospector(new JacksonAnnotationIntrospector());
mapper.getSerializationConfig()。withAnnotationIntrospector(new JacksonAnnotationIntrospector());
System.out.println(mapper.writeValueAsString(map));

执行此代码时,我得到以下输出:{str1:str2}



我的问题是为什么内部属性myProperty没有与地图序列化?
如何序列化内部属性?

解决方案

最有可能的是,您最终会实现自己的序列化程序,它将处理您的自定义Map类型。请参阅这个问题了解详情。



如果你选择用合成替换继承,那就是让你的类包含一个不扩展地图的地图字段,那么使用它来解决这个问题很容易 @JsonAnyGetter注释



'p>下面是一个例子:

 公共类JacksonMap {

公共static class Bean {
private final String field;
private final Map< String,Object>地图;

public Bean(String field,Map< String,Object> map){
this.field = field;
this.map = map;
}

public String getField(){
return field;
}

@JsonAnyGetter
公共地图< String,Object> getMap(){
返回地图;
}
}

公共静态无效的主要(字串[] args)抛出JsonProcessingException {
豆地图=新的Bean( VALUE1,类别<字符串,Object> singletonMap(key1,value2));
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(map));
}
}

输出:



'pre> { 字段: VALUE1, KEY1: VALUE2}


I have a class which looks like this:

@JsonFormat(shape=JsonFormat.Shape.OBJECT)
public class MyMap implements Map<String, String>
{
    protected Map<String, String> myMap = new HashMap<String, String>();

    protected String myProperty = "my property";
    public String getMyProperty()
    {
        return myProperty;
    }
    public void setMyProperty(String myProperty)
    {
        this.myProperty = myProperty;
    }

    //
    // java.util.Map mathods implementations
    // ...
}

And a main method with this code:

            MyMap map = new MyMap();
            map.put("str1", "str2");

            ObjectMapper mapper = new ObjectMapper();
            mapper.getDeserializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
            mapper.getSerializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
            System.out.println(mapper.writeValueAsString(map));

When executing this code I'm getting the following output: {"str1":"str2"}

My question is why the internal property "myProperty" is not serialized with the map? What should be done to serialize internal properties?

解决方案

Most probably you will end up with implementing your own serializer which will handle your custom Map type. Please refer to this question for more information.

If you choose to replace inheritance with composition, that is to make your class to include a map field not to extend a map, then it is pretty easy to solve this using the @JsonAnyGetter annotation.

Here is an example:

public class JacksonMap {

    public static class Bean {
        private final String field;
        private final Map<String, Object> map;

        public Bean(String field, Map<String, Object> map) {
            this.field = field;
            this.map = map;
        }

        public String getField() {
            return field;
        }

        @JsonAnyGetter
        public Map<String, Object> getMap() {
            return map;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Bean map = new Bean("value1", Collections.<String, Object>singletonMap("key1", "value2"));
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(map));
    }
}

Output:

{"field":"value1","key1":"value2"}

这篇关于如何使用基于java.util.Map的类序列化Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆