gpt4 book ai didi

Java Jackson,使用 Map 编码类,无需访问类代码库

转载 作者:行者123 更新时间:2023-12-02 01:35:08 25 4
gpt4 key购买 nike

我正在尝试使用 Jackson 映射器编码(序列化)类。类具有 Map 属性。属性必须具有某种序列化...我得到的一切都是序列化的字节流或错误序列化的map.toString()。

我尝试过使用 mixins 或设置 Jackson 映射器...没有任何帮助。

com.fasterxml.jackson 2.8.11com.rabbitmq.client 5.4.3

我的代码:

private RawMessage parseMetadata(RawMessage rawMessage, Envelope envelope, AMQP.BasicProperties properties) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);


JsonNode rootNode = mapper.createObjectNode();

JsonNode message = mapper.valueToTree(new String(rawMessage.getPayload()));
((ObjectNode) rootNode).set("message", message);

JsonNode envelopeNode = mapper.valueToTree(envelope);
((ObjectNode) rootNode).set("envelope", envelopeNode);

JsonNode propertiesNode = mapper.valueToTree(properties);
((ObjectNode) rootNode).set("properties", propertiesNode);

return new RawMessage(mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(rootNode));
}

结果:

{
"properties": {
"bodySize": 0,
"headers": {
"connection_name": {
"bytes": "MTcyLjE5LjAuMTo0NTgzNiAtPiAxNzIuMTkuMC40OjU2NzI=",
"stream": {
"in": { "buf": "MTcyLjE5LjAuMTo0NTgzNiAtPiAxNzIuMTkuMC40OjU2NzI=", "pos": 0, "mark": 0, "count": 35 },
"bytearr": "A...",
"chararr": "\u0000...",
"readBuffer": "AAAAAAAAAAA="
}
},
"timestamp_in_ms": 1565957758662,
"protocol": {
"bytes": "ezAsOSwxfQ==",
"stream": {
"in": { "buf": "ezAsOSwxfQ==", "pos": 0, "mark": 0, "count": 7 },
"bytearr": "AAA...",
"chararr": "\u0000\u0000\...",
"readBuffer": "AAAAAAAAAAA="
}
},...
},
"ssl": false
},
"deliveryMode": 2,
"timestamp": 1565957758000,
"classId": 60,
"className": "basic"
}
}
class BasicProperties extends com.rabbitmq.client.impl.AMQBasicProperties {
private String contentType;
private String contentEncoding;
private Map<String,Object> headers; <---
private Integer deliveryMode;
private Integer priority;
private String correlationId;
private String replyTo;
private String expiration;
private String messageId;
private Date timestamp;
private String type;
private String userId;
private String appId;
private String clusterId;
...}

复制:

import com.rabbitmq.client.AMQP;

Map<String, Object> map = new HashMap<>();
byte[] test = "test".getBytes();
map.put("test", test);
AMQP.BasicProperties prop = new AMQP.BasicProperties(null, null, map,null,null,null,null,null,null,null,null,null,null,null);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

JsonNode root = objectMapper.createObjectNode();

JsonNode propertiesN = objectMapper.valueToTree(prop);
((ObjectNode) root).set("properties", propertiesN);

LOG.info(root.toString());

结果:

{
"properties": {
"bodySize": 0,
"headers": { "test": "dGVzdA==" },
"classId": 60,
"className": "basic"
}
}

三个点意味着还有更多。它太长了。字节流太多。

我希望 map 的输出是 -> key: String

我想输出看起来像他的 ->properties.headers.test: "test"

没有 AMQP.BasicProperties 类的任何注释。

我认为 Jacksons Mixins 应该是实现这一目标的方法。

最佳答案

回答这个问题: - 让你拥有 Jackson Mixin,用自定义 JsonSerialize 类注释你的类 - 创建您自己的 Serialize 类来扩展 JsonSerializer 类 - 使用 addMixIn(DataClass.class, Mixin.class) 方法将 Mixin 注册到映射器

示例:

创建混音

abstract class MixIn {
@JsonSerialize(using = MyPairSerializer.class)
abstract Map<String, Object> get_map();
}

创建MapSerializer

class MyPairSerializer extends JsonSerializer<Map<String, Object>> {
public void serialize(Map<String, Object> map, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
for (Map.Entry<String, Object> entry : map.entrySet()) {
gen.writeStringField(entry.getKey(), entry.getValue().toString());
}
}
}

注册序列化器和映射对象

// API call
Data data = getData();

ObjectMapper objectMapper = new ObjectMapper();
JsonNode node = objectMapper.valueToTree(data);
System.out.println("node = " + node);

之前:{“数据”:“测试”,“ map ”:{“测试”:“dGVzdA ==”}}

之后:{“数据”:“测试”,“ map ”:{“测试”:“测试”}}

TLDR 答案是 Mixins 与自定义序列化器...

关于Java Jackson,使用 Map<String, Object> 编码类,无需访问类代码库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57524652/

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