我想序列化/反序列化MultiValuedMap<String,Object>很难相信,在www中没有示例代码。有人知道一个例子吗?
这是我的代码片段进口org.apache.commons.collections4.MultiValuedMap;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class MultiValuedMapSerializer extends StdSerializer<MultiValuedMap<String,Object>> {
private static final long serialVersionUID = 1L;
public MultiValuedMapSerializer() {
this(null);
}
protected MultiValuedMapSerializer(Class<MultiValuedMap<String,Object>> klass) {
super(klass);
}
@Override
public void serialize(MultiValuedMap<String,Object> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
for (Entry<String, Object> entry : value.entries()) {
if(entry.getValue() instanceOf MultiValuedMap){
@SuppressWarnings("unchecked")
MultiValuedMap<String, Object> map = (MultiValuedMap<String, Object>) value;
if (key != null) {
gen.writeFieldName(key);
serialize(map, gen, provider);
} else {
serialize(map, gen, provider);
}
} else if(){....} else if(){....} ...
}
gen.writeEndObject();
}
}
您可以使用org.apache.commons.collections4.MultiValuedMap
可以很容易地转换为Map
的事实。Jackson
已经有一个序列化器,所以所有角落的情况下自动处理。它可以像下面这样:
class MultiValuedMapJsonSerializer extends JsonSerializer<MultiValuedMap> {
@Override
public void serialize(MultiValuedMap value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
return;
}
gen.writeObject(value.asMap());
}
@Override
public Class<MultiValuedMap> handledType() {
return MultiValuedMap.class;
}
}
下面,你可以找到一个如何注册和使用它的例子:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import java.io.IOException;
public class JsonMultiValuedMapApp {
public static void main(String[] args) throws IOException {
SimpleModule collections4Module = new SimpleModule();
collections4Module.addSerializer(new MultiValuedMapJsonSerializer());
ObjectMapper mapper = JsonMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.addModule(collections4Module)
.build();
MapWrapper wrapper = new MapWrapper();
wrapper.getMap().put("numbers", 1L);
wrapper.getMap().put("numbers", 2L);
wrapper.getMap().put("strings", "value");
mapper.writeValue(System.out, wrapper);
}
}
class MapWrapper {
private MultiValuedMap<String, Object> map = new HashSetValuedHashMap<>();
public MultiValuedMap<String, Object> getMap() {
return map;
}
public void setMap(MultiValuedMap<String, Object> map) {
this.map = map;
}
}
以上代码打印:
{
"map" : {
"strings" : [ "value" ],
"numbers" : [ 1, 2 ]
}
}