Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186141
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T17:33:11+00:00 2022-06-10T17:33:11+00:00

Any way for Jackson JSON to remove wrapper for array/collection?

  • 0

[ad_1]

Working from an existing API SDK and I’m trying to get rid of a large number of wrapper classes, which are there (mostly) to provide an object wrapper node.

eg: ProductRoot is a class, with a single property named product, of type Product (which is the actual class). As the API returns JSON that looks like this for a Product:

"product": {
"id": 632910392,
"title": "IPod Nano - 8GB",
....

I can remove the wrapper class ProductRoot, and instead annotate the Product class like this:

@JsonTypeName(value = "product")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)

And it works just fine.

HOWEVER, when the API is returning an array of products, it passes in JSON that looks like this:

{
"products": [
{
"id": 632910392,
"title": "IPod Nano - 8GB",
"body_html": "

Note the lack of a “product” node. So my code blows up with this error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id ‘id’ as a subtype of com.test.api.rest.model.Product: known type ids = [product] (for POJO property ‘products’)
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 15] (through reference chain: com.test.api.rest.model.ProductsRoot[“products”]->java.util.ArrayList[0])

Where ProductsRoot is a collection wrapper class, like so:

private List<Product> products = new LinkedList<Product>();

So I THINK the issue is that when it’s a single Product, I need the deserializer/serializer to wrap with a “product” node. However, when it’s an array/collection of products, I need each product to be unwrapped, or not wrapped.

So far I haven’t figured out how to do this using annotations or configuration. I think I could probably do it using a custom deserializer and custom serializer, but would need to do a fair bit of magic for it to work correctly, for all relevant types, etc… And I’m not sure that ends up being a lot better/clearer than the current wrapper class approach:(

Any suggestions or solutions? Thanks!

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 396 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 369 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 361 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.