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

The Archive Base Latest Questions

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

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

  • 0

I know how to serialize java 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;

}

My 3rd class:

public class Ort {

  private String plz;
  private String name;

}

And maybe more generated sub-classes…

I want to get this json object:

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

I am not allowed to change my POJO’s.
What kind of framework can do this for me? I tried gson but gson is not working with subclasses.

regards, dave

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

    I got it working! With Java Reflection, json and it’s recursive!

    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. So my Invoice object I would put here as a parameter:

      Invoice i = new Invoice();
      invokeSetters(i);
      

    and here it is:

            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 that you can mark a scala object as @serializable , but I
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 want to send a serialized object from a Java class to a servlet
Does somebody know a Java library which serializes a Java object hierarchy into Java
I'm using the Gson library in Java to serialize java objects to jSon and
I have an object from old Java code, and I now changed the serialized
I'm using the java encog library and wanted to know how to serialize the
I'm trying to serialize a Java Dynamic proxy using Jackson library but I get

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.