How to skip wrapper object in jackson json deseria

2019-08-01 09:09发布

问题:

I am trying to deserialize the following string using the jackson.

{
  "roomName": "u8ec29p0j7q2m9f",
  "broadcastPresenceRoles": {
    "broadcastPresenceRole": [
      "moderator",
      "participant",
      "visitor"
    ]
  },
  "owners": {
    "owner": "anewuser@shahanshah"
  },
  "admins": {
    "admin": "sanjeet@shahanshah"
  },
  "members": null,
  "outcasts": {
    "outcast": [
      "sawan@shahanshah",
      "sus@shahanshah"
    ]
  },
  "ownerGroups": {
    "ownerGroup": "Friends"
  }
}

This the response from the openfire rest apis. I am having problem with the wrapper object wrapping the arrays. Here

"broadcastPresenceRoles": {
    "broadcastPresenceRole": [
      "moderator",
      "participant",
      "visitor"
    ]
  },

I tried this to unwrap the container but not getting success. I don't think writing wrapper classes is good idea (Since I will have to write several wrapper classes).Also I need the generalized solution so i can use it with other responses since the apis is having other responses in the similar wrapped objects format. Thanks in advance.

回答1:

You can create a custom annotation with @JsonDeserialize inside and create a custom JsonDeserializer that implements ContextualDeserializer. The idea is inspired from the solution you mentioned but it is more general to unwrap any one property in a json object.

Following is the custom annotation using @JacksonAnnotationsInside as annotation container contains @JsonDeserialize:

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonDeserialize(using = JsonUnwrapPropertyDeserializer.class)
public @interface JsonUnwrapProperty {
}

and the custom JsonDeserializer that implements ContextualDeserializer:

public class JsonUnwrapPropertyDeserializer extends JsonDeserializer<Object> implements ContextualDeserializer {

    private JavaType unwrappedJavaType;
    private String unwrappedProperty;

    @Override
    public JsonDeserializer<?> createContextual(final DeserializationContext deserializationContext, final BeanProperty beanProperty) throws JsonMappingException {
        unwrappedProperty = beanProperty.getMember().getName();
        unwrappedJavaType = beanProperty.getType();
        return this;
    }

    @Override
    public Object deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
        final TreeNode targetObjectNode = jsonParser.readValueAsTree().get(unwrappedProperty);
        return jsonParser.getCodec().readValue(targetObjectNode.traverse(), unwrappedJavaType);
    }
}

and the usage example:

public class MyBean {

    @JsonProperty("broadcastPresenceRoles")
    @JsonUnwrapProperty
    private List<String> broadcastPresenceRole;

    @JsonProperty("admins")
    @JsonUnwrapProperty
    private String admin;

    // constructor, getter and setter
}

@JsonProperty is to locate the wrapper object and @JsonUnwrappProperty is to deserialize the json object and extract a property into the annotated field.

Edited:

Following is an example with ObjectMapper:

String json = "{\n" +
        "  \"broadcastPresenceRoles\": {\n" +
        "    \"broadcastPresenceRole\": [\n" +
        "      \"moderator\",\n" +
        "      \"participant\",\n" +
        "      \"visitor\"\n" +
        "    ]\n" +
        "  },\n" +
        "  \"admins\": {\n" +
        "    \"admin\": \"sanjeet@shahanshah\"\n" +
        "  }\n" +
        "}";

final ObjectMapper mapper = new ObjectMapper();
final MyBean myBean = mapper.readValue(json, MyBean.class);

System.out.println(myBean.getBroadcastPresenceRole());
System.out.println(myBean.getAdmin());

Output:

[moderator, participant, visitor]

sanjeet@shahanshah