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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:50:12+00:00 2026-06-17T16:50:12+00:00

I have a structure with circular references. And for debug purposes, I want to

  • 0

I have a structure with circular references.
And for debug purposes, I want to dump it. Basically as any format, but I chose JSON.

Since it can be any class, I chose GSON which doesn’t needs JAXB annotations.

But GSON hits the circular references and recurses until StackOverflowError.

How can I limit GSON to

  • ignore certain class members?
    Both @XmlTransient and @JsonIgnore are not obeyed.

  • ignore certain object graph paths? E.g. I could instruct GSON not to serialize release.customFields.product.

  • go to the depth of at most 2 levels?

Related: Gson.toJson gives StackOverFlowError, how to get proper json in this case? (public static class)

  • 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-17T16:50:12+00:00Added an answer on June 17, 2026 at 4:50 pm

    Simply make the fields transient (as in private transient int field = 4;). GSON understands that.

    Edit

    No need for a built-in annotation; Gson lets you plug in your own strategies for excluding fields and classes. They cannot be based on a path or nesting level, but annotations and names are fine.

    If I wanted to skip fields that are named “lastName” on class “my.model.Person”, I could write an exclusion strategy like this:

    class MyExclusionStrategy implements ExclusionStrategy {
    
        public boolean shouldSkipField(FieldAttributes fa) {                
            String className = fa.getDeclaringClass().getName();
            String fieldName = fa.getName();
            return 
                className.equals("my.model.Person")
                    && fieldName.equals("lastName");
        }
    
        @Override
        public boolean shouldSkipClass(Class<?> type) {
            // never skips any class
            return false;
        }
    }
    

    I could also make my own annotation:

    @Retention(RetentionPolicy.RUNTIME)
    public @interface GsonRepellent {
    
    }
    

    And rewrite the shouldSkipField method as:

    public boolean shouldSkipField(FieldAttributes fa) {
        return fa.getAnnotation(GsonRepellent.class) != null;
    }
    

    This would enable me to do things like:

    public class Person {
        @GsonRepellent
        private String lastName = "Troscianko";
    
        // ...
    

    To use a custom ExclusionStrategy, build Gson object using the builder:

    Gson g = new GsonBuilder()
           .setExclusionStrategies(new MyOwnExclusionStrategy())
           .create();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have folder structure and I would like to create JSON objects based on
What's wrong here? res.render('/somepage', {user:req.session.user}) It leads to Converting circular structure to JSON errors,
I have a big object I want to convert to JSON and send. However
JSON.stringify(eventObject); gives: TypeError: Converting circular structure to JSON dojox.json.ref.toJson(eventObject); gives: TypeError: Accessing selectionEnd on
I have set up a circular linked list data structure that represents a word,
I have structure /media images file1_dis.jpg file2_dis.jpg .... other_images subfolders file1_en.jpg file2_en.jpg I want
I have structure like this below. Now, I want swap 2 structures. public struct
I have structure like this. <ul class=gallerylist> <li> <image ...> <input ...> </li> <li>
I have menu structure as show here . All item have a background img.
I have a structure like this struct structure { BaseObject &A; //BaseObject has a

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.