fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

fastjson 1.2.58 Integer to Enum error on JSONObject.getObject using JavaBeanDeserializer smartMatch

Open TarekSaid opened this issue 7 years ago • 0 comments

java version: 1.8 fastjson version: 1.2.57

We have an API that returns a jSON we parse with JSONObject.parseObject:

String result = apiService.doPost(url, params);
JSONObject data = JSONObject.parseObject(result);
Response r = data.getObject("response", Response.class);

The json we receive is as such: {"response":{"status":2}}. As you can see, status receives an int. In the Response class, the Status field is an enum, and its setter correctly transforms int to enum:

public class Response {
  private Status status;

  public void setStatus(int status) {
    this.status = Status.of(code);
  }
}

With versions 1.2.57 and 1.2.58, we get the following error: JSONException thrown: Can not set Status field Response.status to (int)2.

It seems to be due to the fact that DefaultFieldDeserializer detects the field as int, so in the JavaBeanDeserializer it executes the code field.setInt(object, ((Number) value).intValue()) for the Status field.

As a workaround, we are using 1.2.56, but is there any way we can correctly deserialise Status as an enum with 1.2.58?

TarekSaid avatar Jul 08 '19 15:07 TarekSaid