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

  • Home
  • SEARCH
  • 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 8105661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:13:16+00:00 2026-06-06T00:13:16+00:00

I would like to serialize this object to JSON String public class Person {

  • 0

I would like to serialize this object to JSON String

public class Person {
   public String id;
   public String name;
   public Person parent;
}

and obtain a result like this:

{id: 1, name: "Joe", parent: 2}

I tried to use

Person p = new Person(1, "Joe", new Person(2, "Mike"));
Gson gson = new GsonBuilder()
            .registerTypeAdapter(Persona.class, new PersonSerializer()).create();
String str = gson.toJson(p);

but instead of that, I got:

"1"

PersonSerializer:

public class PersonSerializer implements JsonSerializer<Person> {
    public JsonElement serialize(Person src, Type typeOfSrc, ...) {
        return new JsonPrimitive(src.id);
    }
}

Please any suggestion is welcome

Thanks,
Mario

  • 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-06T00:13:17+00:00Added an answer on June 6, 2026 at 12:13 am

    In order to get the result you desire, you need to write the serializer like this:

    public static class PersonSerializer implements JsonSerializer<Person> {
        public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) {
            JsonObject result = new JsonObject();
            result.add("id", new JsonPrimitive(person.getId()));
            result.add("name", new JsonPrimitive(person.getName()));
            Person parent = person.getParent();
            if (parent != null) {
                result.add("parent", new JsonPrimitive(parent.getId()));
            }
            return result;
        }
    }
    

    The result for

        Person p = new Person(1, "Joe", new Person(2, "Mike"));
        com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer())
                .create();
        System.out.println(gson.toJson(p));
    

    will be

    {"id":1,"name":"Joe","parent":2}
    

    Complete code:

    import java.lang.reflect.Type;
    
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonPrimitive;
    import com.google.gson.JsonSerializationContext;
    import com.google.gson.JsonSerializer;
    
    public class GsonSimpleTest {
    
        public static class Person {
            public int id;
            public String name;
            public Person parent;
    
            public Person(final int id, final String name) {
                super();
                this.id = id;
                this.name = name;
            }
    
            public Person(final int id, final String name, final Person parent) {
                super();
                this.id = id;
                this.name = name;
                this.parent = parent;
            }
    
            public int getId() {
                return id;
            }
    
            public void setId(final int id) {
                this.id = id;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(final String name) {
                this.name = name;
            }
    
            public Person getParent() {
                return parent;
            }
    
            public void setParent(final Person parent) {
                this.parent = parent;
            }
    
        }
    
        public static class PersonSerializer implements JsonSerializer<Person> {
            public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) {
                JsonObject result = new JsonObject();
                result.add("id", new JsonPrimitive(person.getId()));
                result.add("name", new JsonPrimitive(person.getName()));
                Person parent = person.getParent();
                if (parent != null) {
                    result.add("parent", new JsonPrimitive(parent.getId()));
                }
                return result;
            }
        }
    
        public static void main(final String[] args) {
            Person p = new Person(1, "Joe", new Person(2, "Mike"));
            com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer())
                    .create();
            System.out.println(gson.toJson(p));
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this schema : public class Student { public String name; public School
I am serializing a class like this into JSON using Flexjson: public class Item
I would like to XML serialize an object that has (among other) a property
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I'm trying to serialize a class hierarchy to a Json string using DataContractJsonSerializer ,
I wish to serialize from C sharp to JSON. I would like the output
I have an object, and before I serialize it to Json, I'd like to
I have three jobs that I would like to serialize in Jenkins. They should
instead of using getParameterByName('Field', PostData) (PostData == $('form').serialize();) I would like to write PostData.Field,
I want to serialize an Entity Framework Self-Tracking Entities full object graph (parent +

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.