2

I've defined to JSON Pojo classes as shown below, In the below MyMessage class, sometimes I receive kpMessage class as string, how can I convert the received string to object.

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "kp.message", "kp.version" })
public class MyMessage {

    @JsonProperty("kp.message")
    private KpMessage kpMessage;
    @JsonProperty("kp.version")
    private String kpVersion;

    @JsonProperty("kp.message")
    public KpMessage getKpMessage() {
        return KpMessage;
    }

    @JsonSetter
    public void setKpMessage(KpMessage kpMessage) {
        this.kpMessage = kpMessage;
    }

    @JsonProperty("kp.version")
    public String getKpVersion() {
        return kpVersion;
    }

    @JsonProperty("kp.version")
    public void setKpVersion(String kpVersion) {
        this.kpVersion = kpVersion;
    }

}

KpMessage.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "domain", "request_id", "auth_token"})
public class KpMessage {

    @JsonProperty("domain")
    private Object domain;
    @JsonProperty("request_id")
    private String requestId;
    @JsonProperty("auth_token")
    private String authToken;

    @JsonProperty("domain")
    public Object getDomain() {
        return domain;
    }

    @JsonProperty("domain")
    public void setDomain(Object domain) {
        this.domain = domain;
    }

    @JsonProperty("request_id")
    public String getRequestId() {
        return requestId;
    }

    @JsonProperty("request_id")
    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    @JsonProperty("auth_token")
    public String getAuthToken() {
        return authToken;
    }

    @JsonProperty("auth_token")
    public void setAuthToken(String contextAuthToken) {
        this.authToken = authToken;
    }
}

The Below sample input works as expected.

{  
   "kp.message":{  
      "domain":null,
      "request_id":"req-11ef0ffa-0180-4040-a47e-9f78b23b49e9",
      "auth_token":"gAAAA"
   },
   "kp.version":"2.0"
}

However the below input fails with error no String-argument constructor/factory method to deserialize from String value

{
    "kp.message": "{
    \"domain\": null, 
    \"request_id\": \"req-11ef0ffa-0180-4040-a47e-9f78b23b49e9\", 
    \"auth_token\": \"gAAAA\",
    }", 
    "kp.version": "2.0"
}

How to add a deserializer for the above input such that if String is received dueto escape character ", the String is converted to KpMessage Object

Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112

1 Answers1

0

For this use case, one solution I found was to do as the error message suggests and create a constructor with a single string argument; then parse the raw value using Jackson or Gson. Something like:

public KpMessage(String json) throws IOException {
    KpMessage k = new ObjectMapper().readValue(json, KpMessage.class);
    this.domain = k.domain;
    this.requestId = k.requestId;
    this.authToken = k.authToken;
}

Ref: Convert JSON String (Which includes different lenients) to Java Class with RestTemplate

Another way is using JsonDeserialize.

awgtek
  • 1,483
  • 16
  • 28