gpt4 book ai didi

java - Apache Commons MultiValuedMap 和 Jackson

转载 作者:行者123 更新时间:2023-12-04 14:08:35 29 4
gpt4 key购买 nike

我想序列化/反序列化 MultiValuedMap 。很难相信,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 ]
}
}

关于java - Apache Commons MultiValuedMap 和 Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66351494/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com