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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:09+00:00 2026-06-14T17:06:09+00:00

I know how to serialize null objects. but what if I have an objects

  • 0

I know how to serialize null objects. but what if I have an objects and inside another object that is null?

e.g.

My 1st class:

public class Invoice {

   private Adresse adresse;
   private Double betrag;
   private Double Ust;
   private String zweck;

}

My 2nd class:

public class Adresse {
   private String name;
   private Ort ort;
}

And more sub-classes…

And if I do:

Gson gson = new GsonBuilder().serializeNulls().create();

I only get this:

{"adresse":null,"betrag":null,"Ust":null,"zweck":null}

instead of this:

{"adresse":{"name":null,"ort"{"plz":null,"name":null}},"betrag":null,"Ust":null,"zweck":null}

There should be an automated approach to it. There will always be other sub-classes. So a manual approach is a no-go.

  • 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-14T17:06:09+00:00Added an answer on June 14, 2026 at 5:06 pm

    I got it working! With Java Reflection!

    Here the code:

    1. userDefined() checks if a class is user defined or primitive, can be customized as you wish.

      public boolean isUserDefined(Class o) {
        if (o.isAssignableFrom(String.class)) {
          return false;
        }
        if (o.isAssignableFrom(Double.class)) {
          return false;
        }
        if (o.isAssignableFrom(Integer.class)) {
          return false;
        }
        if (o.isAssignableFrom(Boolean.class)) {
          return false;
        }
        if (o.isAssignableFrom(Short.class)) {
          return false;
        }
        if (o.isAssignableFrom(Float.class)) {
          return false;
        }
        if (o.isAssignableFrom(Long.class)) {
          return false;
        }
        return true;
      }
      
    2. getSetterMethod() returns the setter Method of a field.

      private static Method getSetterMethod(Object o, Field f) {       
          for (Method method : o.getClass().getMethods()) {
              String fieldname = "set" + f.getName();
              String methodname = method.getName().toLowerCase();
              if (fieldname.equals(methodname)) {
                  return method;
              }
          }
          return null;
      }
      
    3. And finally, invokeSetters() invoke user defined object setter Methods and those of the subclasses! yea.

          private void invokeSetters(Object o) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchMethodException, InvocationTargetException {
          System.out.println("INVOKING THE OBJECT " + o.getClass().toString());
            for (Field f : o.getClass().getDeclaredFields()) {
              System.out.println("NOW IN THE FIELD: " + f.getName());
              f.setAccessible(true);
              Object obj = null;
              if (isUserDefined(f.getType()) && f.get(o) == null) {
                  System.out.println("FIELD is USER DEFINED AND NULL");
                  Class c = Class.forName(f.getType().getName());
                  System.out.println("CLASS IS " + c.getName());
                  obj = c.getConstructor().newInstance();
                  System.out.println("GOING INSIDE " + obj.toString() + " NOW.");
                  invokeSetters(obj);
              }
              if (obj != null) {
                  Method setter = getSetterMethod(o, f);
                  System.out.println("IM IN PARENT OBJECT " + o + " AND INVOKING METHOD " + setter.getName() + " WITH PARAMETER " + obj.toString());
                  setter.invoke(o, obj);
                  System.out.println("METHOD WAS INVOKED");
              }
            }
          }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to serialize java objects. but what if I have an objects
I have tried to serialize the class which contains the list of objects but
I have an extension method for System.Object to serialize and deserialize objects using Json.Net
I know how to serialize in F# using mutable objects, but is there a
I know that you can't serialize/deserialize using an interface but I'm confused by behaviour
I know how to serialize an object in the following way: void encodeMsg(char **msg,
I know it is possible to serialize and unserialize in PHP and then have
I know that Phonegap has an event for back button, but it's only available
I am unable to deserialize my custom object. public class UserInfo { public int
I'm trying to serialize an interface. I know that this is impossible throught the

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.