简体   繁体   中英

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. In the POJO class, there is a field called requestMessage which contains a string of 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. 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 . I've something like this in my POJO class:

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.

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?

You need to tell Jackson to serialize the JSONObject field using its toString method, like this:

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. JSONObject has one, so it got deserialized. I would have expected, for consistency, that toString was used on serialization, but it doesn't. 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. I would stick to JSONObject 's equivalent in Jackson, which I guess is JsonNode , as suggested by Coderino Javarino .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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