Gio*_*ato 2 java json jackson json-deserialization
我正在尝试Map<?, ?>使用Jackson对象2.8 将带有任意对象的a序列化/反序列化为键。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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后
Map<Foo, Bar> map;
map.put(new Foo("foo1"), new Bar("bar1"));
map.put(new Foo("foo2"), new Bar("bar2"));
Run Code Online (Sandbox Code Playgroud)
应该用这个JSON表示
[
[ { "foo": "foo1" }, { "bar": "bar1" } ],
[ { "foo": "foo2" }, { "bar": "bar2" } ]
]
Run Code Online (Sandbox Code Playgroud)
所以我做了序列化部分
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何写一个JsonDeserializer做相反的工作。有什么建议么?
注意:我需要一种[ [ "key1", "value1" ], [ "key2", "value2" ] ]表示法,以便能够在JavaScript a中使用该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;
}
Run Code Online (Sandbox Code Playgroud)
这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;
}
}
Run Code Online (Sandbox Code Playgroud)
可以这样使用:
public class Baz {
@JsonSerialize(using = MapToArraySerializer.class)
@JsonDeserialize(using = ArrayToMapDeserializer.class,
keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8148 次 |
| 最近记录: |