gpt4 book ai didi

java - 如何对 Map 使用 Jackson 自定义值序列化器?

转载 作者:行者123 更新时间:2023-12-02 04:38:30 26 4
gpt4 key购买 nike

我有一个来自数据库的结果集,格式为 Map<String, Object>我应该返回 json来自 REST 服务。 map 中的值可以是各种类型,包括 PGObject , String , IntegerDate .

我为 org.postgresql.util.PGObject 编写了一个自定义序列化器类型为“jsonb”的类,在 List<?> 中工作正常,但不在 Map<String, Object> 中.

public class PgObjectSerializer extends JsonSerializer<PGobject>{

@Override
public void serialize(PGobject pgObject, JsonGenerator gen, SerializerProvider serializers) throws IOException {
switch (pgObject.getType()) {
case "json":
case "jsonb":
gen.writeRawValue(pgObject.getValue());
break;
default:
gen.writeString(pgObject.getValue());
}
}
}

目标 PGObject 看起来像这样:

PGObject pgo = new PGObject();
pgo.setType("jsonb");
pgo.setValue("[{"id": 6, "name": "Foo"}, {"id": 7, "name": "Bar"}, {"id": 8, "name": "Baz"}]"); map.put("reason", pgo);

当 jackson 连载此PGObjectMap<String, Object> 中的值然后我得到 json值如:

  "reason": {
"type": "jsonb",
"value": "[{\"id\": 6, \"name\": \"Foo\"}, {\"id\": 7, \"name\": \"Bar\"}, {\"id\": 8, \"name\": \"Baz\"}]"
}

我需要什么:

  "reason": [
{
"id": 6,
"name": "Foo"
},
{
"id": 7,
"name": "Bar"
},
{
"id": 8,
"name": "Baz"
},
],

我尝试将模块添加到 ObjectMapper和定制MapTypeObjectWriterSerializing Map<Date, String> with Jackson的答案所示:

@Service
@Transactional
public class MyClass {
private final ObjectWriter writer;
private final MyRepo repository;

@Autowired
public MyClass(MyRepo repository) {
this.repository = repository;

SimpleModule module = new SimpleModule();
module.addKeySerializer(PGobject.class, new PgObjectSerializer());

ObjectMapper mapper = new ObjectMapper();
JavaType myMapType = mapper.getTypeFactory().
constructMapType(HashMap.class, String.class, PGobject.class);
writer = mapper.registerModule(module).writerFor(myMapType);
}

...

private String toJsonString(Map<String, Object> map) {
try {
return writer.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

但我对每个非 PGObject 都遇到序列化错误 map 中的对象:

[Test worker] ERROR 
com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class (through reference chain: java.util.HashMap["end_date"]->java.lang.String["type"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:348)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:343)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:698)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFieldsUsing(MapSerializer.java:736)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:534)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:30)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:416)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1425)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1158)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1031)

如何启用我的PGObjectSerializer在 Jackson 中,当 PGObject 在 Map<String, Object> 序列化期间被发现为值时?

最佳答案

一种解决方法可能是为 Object 添加序列化程序。

然后在序列化器本身中,您可以检查Object是否是instanceOf PGobject

并且在 myMapType 中您可以指定 Object.class:

JavaType myMapType = mapper.getTypeFactory().
constructMapType(HashMap.class, String.class, Object.class);

序列化器将是:

class PgObjectSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object object, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (object instanceof PGobject) {
PGobject pgObject = (PGobject) object;
switch (pgObject.getType()) {
case "json":
case "jsonb":
gen.writeRawValue(pgObject.getType());
break;
default:
gen.writeString(pgObject.getType());
}
} else {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(gen, object);
}
}

关于java - 如何对 Map<String, Object> 使用 Jackson 自定义值序列化器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46295013/

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