简体   繁体   English

如何让 Jackson 将 JSONObject 转换为负载中的字符串?

[英]How to get Jackson to convert a JSONObject as string in the payload?

I have a POJO class which I use to serialise and deserialise between JSON messages and the POJO object for use within my Java code.我有一个 POJO 类,用于在 JSON 消息和 POJO 对象之间进行序列化和反序列化,以便在我的 Java 代码中使用。 In the POJO class, there is a field called requestMessage which contains a string of JSON.在 POJO 类中,有一个名为requestMessage的字段,其中包含一个 JSON 字符串。 When the payload is sent between the services, this field is literally just a string.当有效负载在服务之间发送时,该字段实际上只是一个字符串。

For example, this is how the payload would look:例如,这是有效载荷的外观:

{
   "name": "John Smith",
   "status": true,
   "requestMessage": "{\"id\": \"some-id\", \"timestamp\": \"2019-11-30\"}"
}

To cater for this field, I created an attribute requestMessage in my POJO class and made the type as JSONObject , which is a type from the org.json package.为了满足这个领域的需求,我在我的 POJO 类中创建了一个属性requestMessage并将类型设为JSONObject ,它是来自org.json包的类型。 I was thinking this kind of make sense because in case I need to use it in my code, I could easily access the information as a JSONObject .我认为这种做法很有意义,因为万一我需要在代码中使用它,我可以轻松地将信息作为JSONObject访问。 I've something like this in my POJO class:我的 POJO 类中有这样的东西:

public class Message {
    private String name;
    private boolean status;
    private JSONObject requestMessage;

    @JsonCreator
    public Message(
            @JsonProperty("name") String name,
            @JsonProperty("status") boolean status,
            @JsonProperty("requestMessage") JSONObject requestMessage
    ) {
        this.name = name;
        this.status = status;
        this.requestMessage = requestMessage;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public JSONObject getRequestMessage() {
        return requestMessage;
    }

    public void setRequestMessage(JSONObject requestMessage) {
        this.requestMessage = requestMessage;
    }

    @Override
    public String toString() {
        return "Message{" +
                "name='" + name + '\'' +
                ", status=" + status +
                ", requestMessage=" + requestMessage +
                '}';
    }
}

However, it seems Jackson wasn't able to convert it as a string properly when sending out the message.但是,似乎杰克逊在发送消息时无法将其正确转换为字符串。 The requestMessage field is always converted into a string as an empty {} object in the payload. requestMessage字段始终转换为字符串作为负载中的空{}对象。

How can I get Jackson to convert and map the requestMessage attribute in the Message POJO class correctly as a string when it's sending out the payload?在发送有效负载时,如何让 Jackson 将Message POJO 类中的requestMessage属性正确转换和映射为字符串?

You need to tell Jackson to serialize the JSONObject field using its toString method, like this:您需要告诉 Jackson 使用其toString方法序列化JSONObject字段,如下所示:

public static class Message {
    private String name;
    private boolean status;
    @JsonSerialize(using=ToStringSerializer.class)
    private JSONObject requestMessage;
    // ...
}

Deserialization was working because Jackson defaults to use a constructor that takes a String parameter for deserialization.反序列化之所以有效,是因为 Jackson 默认使用带有String参数的构造函数进行反序列化。 JSONObject has one, so it got deserialized. JSONObject有一个,所以它被反序列化了。 I would have expected, for consistency, that toString was used on serialization, but it doesn't.为了一致性,我原以为toString用于序列化,但事实并非如此。 I imagine there must be a good design reason behind it.我想这背后一定有一个很好的设计原因。

That being said, I don't understand why you try to use JSONObject from json.org if you are already using Jackson.话虽json.org如果您已经在使用 Jackson,我不明白为什么您尝试使用来自json.org JSONObject I would stick to JSONObject 's equivalent in Jackson, which I guess is JsonNode , as suggested by Coderino Javarino .我会坚持JSONObject在 Jackson 中的等价物,我猜是JsonNode ,正如Coderino Javarino所建议的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM