我试图序列化/反序列化任意对象的Map<?, ?>
作为键与Jackson版本2.8。JSON对应的应该是一对数组,即给定
public class Foo {
public String foo;
public Foo(String foo) {
this.foo = foo;
}
}
public class Bar {
public String bar;
public Bar(String bar) {
this.bar = bar;
}
}
然后Map<Foo, Bar> map;
map.put(new Foo("foo1"), new Bar("bar1"));
map.put(new Foo("foo2"), new Bar("bar2"));
应该用JSON
表示[
[ { "foo": "foo1" }, { "bar": "bar1" } ],
[ { "foo": "foo2" }, { "bar": "bar2" } ]
]
所以我把序列化器部分写成
public class MapToArraySerializer extends JsonSerializer<Map<?, ?>> {
@Override
public void serialize(Map<?, ?> value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartArray();
for (Map.Entry<?, ?> entry : value.entrySet()) {
gen.writeStartArray();
gen.writeObject(entry.getKey());
gen.writeObject(entry.getValue());
gen.writeEndArray();
}
gen.writeEndArray();
}
}
,但我不知道如何写一个JsonDeserializer
做相反的工作。有什么建议吗?
注意:我需要[ [ "key1", "value1" ], [ "key2", "value2" ] ]
符号能够在JavaScript中使用JSON, new Map( ... )
和JSON.stringify(map)
也会产生该符号(参见https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map)。
需要说明的是,这样的映射将是其他类的字段,例如
public class Baz {
@JsonSerialize(using = MapToArraySerializer.class)
@JsonDeserialize(using = ArrayToMapDeserializer.class, keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
和ArrayToMapDeserializer extends JsonDeserializer<Map<?, ?>>
是我寻求帮助的地方
我想出了这个解决方案:
public class ArrayToMapDeserializer extends JsonDeserializer<SortedMap<Object, Object>>
implements ContextualDeserializer {
private Class<?> keyAs;
private Class<?> contentAs;
@Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return this.deserialize(p, ctxt, new HashMap<>());
}
@Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt,
Map<Object, Object> intoValue) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
ObjectCodec codec = p.getCodec();
if (node.isArray()) {
node.forEach(entry -> {
try {
JsonNode keyNode = entry.get(0);
JsonNode valueNode = entry.get(1);
intoValue.put(keyNode.traverse(codec).readValueAs(this.keyAs),
valueNode.traverse(codec).readValueAs(this.contentAs));
} catch (NullPointerException | IOException e) {
// skip entry
}
});
}
return intoValue;
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
JsonDeserialize jsonDeserialize = property.getAnnotation(JsonDeserialize.class);
this.keyAs = jsonDeserialize.keyAs();
this.contentAs = jsonDeserialize.contentAs();
return this;
}
}
可以这样使用:
public class Baz {
@JsonSerialize(using = MapToArraySerializer.class)
@JsonDeserialize(using = ArrayToMapDeserializer.class,
keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
反序列化:
@Override
public Map<?, ?> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
Map map = new LinkedHashMap();
ObjectCodec oc = p.getCodec();
JsonNode anode = oc.readTree(p);
for (int i = 0; i < anode.size(); i++) {
JsonNode node = anode.get(i);
map.put(node.get(0), node.get(1));
}
return map;
}
我在原始解决方案中添加了一些测试用例,使用新的Oson实现,其中我使用Oson进行转换,但使用不同的转换:映射到json: {key1: value1, key2: value2,…},所以json输出变成:
{
{
"foo": "foo1"
}: {
"bar": "bar1"
},
{
"foo": "foo2"
}: {
"bar": "bar2"
}
}
您可以查看源代码!