Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JSON List of Lists not parse properly

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a List which contains multiple Lists. Each list contains objects of particular type.
The problem is when I am convert the List into Json, it wont show anything.
The List is like:


I have a class which contains this list.


When I am trying to convert this Response class into Json, it created the Json like this:

{"type":"json","message":[[{},{},{}],[{},{}],[{}]]}



It wont convert the lists its contains into Json. The data is there, the list contains the objects but not able to parse it and convert into Json.
I am using Gson for this.

Can anybody tell me, what exactly I am missing.


Thanks,
Atul
 
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anything JSON related in your code.  How are you converting your Java object to JSON?
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron,

Thanks for response.

I am converting the Response object into Json.
I set the objects of Component according to type in the respective list and then add that list to message list.
And finally convert the response object into json.

Even I print those individual list objects after coverting them to json.

Might be missing something, just want to understand what it is.

Thanks,
Atul
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atul More wrote:I am converting the Response object into Json.


Show the code.
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

The code is like this:










This is a sample code, similar to real. Only thing similar is to set the object into list and conversion.

Thanks,
Atul
 
Ron McLeod
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your Response class and a simplified version of your TextProcess class, and I got the JSON output that I expected, so it doesn't look like your problem is with the Response class or with Gson.


 
Sheriff
Posts: 22511
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MOXy (dreadful library, but we have to use it at work) does not look at the actual object types, but at the declared types. In class Response that is Component which has zero properties.

If Gson does the same thing, that would explain the empty objects; [[{},{},{}],[{},{}],[{}]] is a list of lists of empty objects. It would also explain why Ron's example does work - the declared type and actual type are the same.

@Ron: could you try adding an instance of a sub class of Translation that adds some extra properties, and make sure these are set to non-null values? My guess is that these also will not show up.
 
Rob Spoor
Sheriff
Posts: 22511
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've done some investigating of my own, and it's not the MOXy problem. I have tried the following:

  • Create the sub class and add an instance. The objects got serialized properly.
  • Create a dummy interface for Translation and use that as the inner type (List<List<ITranslation>>*). The objects got serialized properly.
  • Changed the list type to use a wildcard (List<List<? extends ITranslation>>). The objects did not get serialized properly but instead showed up as empty objects.


  • Conclusion: it's the wildcard that's causing the issue.


    * Sorry for the I in ITranslation, but I was too lazy to rename anything or to come up with something better.
     
    Atul More
    Ranch Hand
    Posts: 138
    1
    jQuery Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    Thanks for response Rob, it helps me alot.
    I am able to solve the issue. The things I have applied are:
    1. Extends the interface Component with Serializable.
    2. Use another json library for conver java to json. The library is Jackson JSON Java Parser.

    Hope it will help someone.


    Thanks,
    Atul
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic