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 7864801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:53:37+00:00 2026-06-02T23:53:37+00:00

I am parsing a POJO into json using Google’s gson library. These are the

  • 0

I am parsing a POJO into json using Google’s gson library. These are the objects involved:

// Order.java
public class Order {
    // ...
    private int id;
    private String name;
    private Location location;
    // ...
}
// Location.java
public class Location {
    // ...
    private String address;
    private float latitude;
    private float longitude;
    private String postcode
    // ...
}

When I run it though gson (making sure the Location member variable is not null) using new Gson().toJson(order) I get this results:

{
    "id" : 1,
    "name" : "nameValue"
}

but I was expecting to get something like this:

{
    "id" : 1,
    "name" : "nameValue",
    "location" : {
            "address" : "some address",
            "latitude" : 53.346346,
            "longitude" : -3.346363,
            "postcode" : "23563"
        }
}

Can’t figure out why is this happening. Has anybody come across this? What can it be that I may be doing wrong?

I have tried setting a custom Type adapter with no luck.

Thanks for your help.

  • 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-06-02T23:53:40+00:00Added an answer on June 2, 2026 at 11:53 pm

    Ok, I figured out what the issue was. I had a mock object factory that created the mock objects using anonymous constructor in the Location type, like this:

    Location location = new Location() {
        {
             setAddress("some address");
             setLatitude(53.346346);
             setLongitude(-3.356363);
             setPostCode("23563");
        }
    };
    

    So Gson interpreted it as an anonymous class and as it is said in the documentation:

    Fields corresponding to the outer classes in inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization

    I changed it into the normal way of setting up the GsonBuilder and iworks as expected. However I wonder if it would be possible to use this kind of initiation, since it does work for Collections (ArrayList). It makes my mock factories easier to read. 🙂

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

Sidebar

Related Questions

Parsing Google Plus JSON objects Using C# and Newtonsoft.Json library how one can parse
I am using the Jackson JSON library to map JSON streams into POJO's. My
Hi i am trying to parse Json using gson library.My Json String is this
When Parsing JSON I normally just constuct an object and use the gson library
Parsing some data and inserting it into 3 tables from .NET. Using Table Valued
Parsing an XML file using the Java DOM parser results in: [Fatal Error] os__flag_8c.xml:103:135:
I`m parsing a Google Maps RSS with javascript and using the following code to
For parsing JSON like this twitter API users/show response I've been using Jackson and
Im using scala.util.parsing.json and I want to create a json with the following format:
Im parsing the source of a website and Im using this regex: /page\.php\?id\=([0-9]*)\\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content) self.agent.page.content

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.