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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:26:31+00:00 2026-05-28T01:26:31+00:00

Ok so I edited the question because it wasn’t clear enough. Edit 2 :

  • 0

Ok so I edited the question because it wasn’t clear enough.

Edit 2 : updated the JSON file.

I’m using GSON in an Android app, and I need to parse JSON files, that come from a server, and are a little too complexes. I don’t want to have my object structure too heavy, so I would like to simplify the contents : so the structure of my object won’t be the structure of the JSON file.

For example, if in the JSON I have this :

{
    "object1":{
        "attribute1" : "test1",
        "attribute40" : "test40",
        "user":{
            "id":1,
            "name":"foo"
        }
        ,"example":{
            "total":10,
            "list":[
            {
                "tag":"tag1",
                "name":"object name 1",
                "pos":1
            },
            {
                "tag":"tag10",
                "name":"object name 10",
                "pos":10
            }
        ]
        }
    }
    "object2":{
        "attribute1":"test..."
    }
}

I don’t want to keep in my current object structure, an object Example, that contains an ArrayList and an int “total”. But I would like to keep only a simple String with the value "object name 1;object name 2;...".

Moreover, I would like to store only the user Id, not the complete User, because I already have the complete user stored somewhere else, with an other server API call.

So my class class would be something like :

class Foo{
    int userId;
    String example; //"object name 1;object name 2;..."
    ...
}

So I suppose that we can achieve this with a custom deserializer, but I don’t find how. I would like if possible to minimize the memory, so I don’t think that having a full object example, and then use it to build my String example is a correct way.

In the worst case, if it’s too complicated, I would like to be able to store at least only the list of Tag items when I parse the Example Object : so I need a custom deserializer to get rid off the int total.

So I would have :

class Foo{
    int userId;
    ArrayList<Tag> example;
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:26:31+00:00Added an answer on May 28, 2026 at 1:26 am

    I adopted the answer to present the full solution designed in chat and to fit to the changed JSON string. The code assumes that the string json holds exactly the (updated) JSON from the question. The requirement is to fill the following class (setter and toString ommitted):

    class Object1
    {
        private String attribute1;
        private String attribute40;
        private int userId;
        private String  nameList;
    }
    

    GSON supports (as the most other REST-libs) three modes:

    • GSON_DOM

      Reads the whole JSON via JsonParser.parse() and builds a DOM tree in memory (object model access). Therefore this solution is good for small JSON files.
    • GSON_STREAM
      Reads only chunks of the JSON via JsonReader. Code is more complicated, but it is suited for large JSON files. As of Android 3.0 Honeycomb, GSON’s streaming parser is included as android.util.JsonReader.
    • GSON_BIND
      Databinding directly to classes via reflection, minimizes the code significantely. GSON allows mixed mode, which means to combine GSON_DOM and GSON_BIND or GSON_STREAM and GSON_BIND which this answer should show.

    To fill the class Object1 via GSON_DOM and GSON_BIND the implementation looks like:

    private static void deserializeViaObjectAccess(final String json)
    {
        Gson gson = new Gson();
    
        // Read the whole JSON into meomory via GSON_DOM
        JsonParser parser = new JsonParser();
        JsonObject object1 = parser.parse(json).getAsJsonObject().getAsJsonObject("object1");
    
        // map the Object1 class via GSON_BIND
        // (bind common attributes which exist in JSON and as properties in the class)
        // mapper acts as factory
        Object1 result = gson.fromJson(object1, Object1.class);
    
        // manually read the attribute from the user object
        int userId = object1.getAsJsonObject("user").getAsJsonPrimitive("id").getAsInt();
        result.setUserId(userId);
    
        // manually read the attributes from the example object
        String names = "";
        JsonArray list = object1.getAsJsonObject("example").getAsJsonArray("list");
        for (int i = 0; i < list.size(); ++i)
        {
            JsonObject entry = list.get(i).getAsJsonObject();
            String name = entry.getAsJsonPrimitive("name").getAsString();
    
            names = i == 0 ? name : names + "; " + name;
        }
        result.setNameList(names);
    
        // Output the result
        log.debug(result.toString());
    }
    

    To fill the class Object1 via GSON_STREAM and GSON_BIND the implementation looks like:

    At the moment, this is only possible when a node is completly loaded via GSON_BIND or
    GSON_STREAM. This example needs that a node itself should be splitted. This is only
    possible with the upcoming version 2.2. I will hand the code in later when
    GSON 2.2 is available.*

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heavily edited this question because responses indicated I wasn't being clear problem: UI
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Edited this question because of my bad examples.. Here is my updated question: Would
Edited Question: Trying to use a .dll file from Java using JNA. I've managed
EDITED because question is about same program. I'm trying to take the top fifty
Question edited for clarity I make an AJAX call, using the $.getJSON() method, and
EDIT: Based on evolution of the problem, I edited this question. First of all,
I flagged the other thread to be deleted, because the main question was edited
I edited the question after David Hanak's answer (thanks btw!). He helped with the
I edited the question so it would make more sense. I have a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.