Nesting multiple levels of Jackson WRAPPER_OBJECTs

By no means am I a Jackon / JSON master, which is probably obvious from the following problem that I am facing:

I have two possible data structures that I receive. The first is called amountTransaction:

{ "amountTransaction": { "clientCorrelator":"54321", "endUserId":"tel:+16309700001" } } 

which is represented by the following Java object:

 @JsonIgnoreProperties(ignoreUnknown = true) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) @JsonTypeName(value = "amountTransaction") @JsonInclude(JsonInclude.Include.NON_NULL) public class AmountTransaction { private String clientCorrelator; private String endUserId; ... } 

However, the amountTransaction object is also displayed as a child of the paymentTransactionNotification object:

 { "paymentTransactionNotification": { "amountTransaction": { "clientCorrelator": "54321", "endUserId": "tel:+16309700001" } } } 

.. which I thought would be presented:

 @JsonIgnoreProperties(ignoreUnknown = true) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) @JsonTypeName(value = "paymentTransactionNotification") @JsonInclude(JsonInclude.Include.NON_NULL) public class PaymentTransactionNotification { private AmountTransaction amountTransaction; ... } 

Parsing JSON using the amountTransaction object works fine. This is a fairly simple example of WRAPPER_OBJECT.

However, when trying to parse the JSON for paymentTransactionNotification, I get an exception indicating that it cannot correctly process the transaction value as an element of the paymentTransactionNotification parameter:

 com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'clientCorrelator' into a subtype of [simple type, class com.sf.oneapi.pojos.AmountTransaction] 

Any thoughts on how I can properly annotate this so that my code can correctly handle both standalone and encapsulated transaction amount objects?

+8
java json jackson wrapper
source share
1 answer

Jackson root node wrapper is disabled by default. You can wrap the internal objects, but if you want to wrap the root node, you need to enable the jackson function for it ( https://jira.codehaus.org/browse/JACKSON-747 ):

 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE); objectMapper.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE); 

When you turned on these features, you already said that Jackson wraps the root element, and you no longer need @JsonTypeInfo and @JsonTypeName. You can simply delete them. But now you need to configure the root name of the node, and you can use the name @JsonRootName for it. Your classes should look like this:

 @JsonIgnoreProperties(ignoreUnknown = true) @JsonRootName("amountTransaction") @JsonInclude(JsonInclude.Include.NON_NULL) public class AmountTransaction { private String clientCorrelator; private String endUserId; ............... } 

and

 @JsonIgnoreProperties(ignoreUnknown = true) @JsonRootName("paymentTransactionNotification") @JsonInclude(JsonInclude.Include.NON_NULL) public class PaymentTransactionNotification { private AmountTransaction amountTransaction; ............. } 

I tried and Jackson converted both JSON requests as expected.

+5
source share

All Articles