[java] How to convert a Java object (bean) to key-value pairs (and vice versa)?

When using Spring, one can also use Spring Integration object-to-map-transformer. It's probably not worth adding Spring as a dependency just for this.

For documentation, search for "Object-to-Map Transformer" on http://docs.spring.io/spring-integration/docs/4.0.4.RELEASE/reference/html/messaging-transformation-chapter.html

Essentially, it traverses the entire object graph reachable from the object given as input, and produces a map from all primitive type/String fields on the objects. It can be configured to output either:

  • a flat map: {rootObject.someField=Joe, rootObject.leafObject.someField=Jane}, or
  • a structured map: {someField=Joe, leafObject={someField=Jane}}.

Here's an example from their page:

public class Parent{
    private Child child;
    private String name; 
    // setters and getters are omitted
}

public class Child{
   private String name; 
   private List<String> nickNames;
   // setters and getters are omitted
}

Output will be:

{person.name=George, person.child.name=Jenna, person.child.nickNames[0]=Bimbo . . . etc}

A reverse transformer is also available.

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to reflection

Get properties of a class Get class name of object as string in Swift Set field value with reflection Using isKindOfClass with Swift I want to get the type of a variable at runtime Loading DLLs at runtime in C# How to have Java method return generic list of any type? Java reflection: how to get field value from an object, not knowing its class Dynamically Add C# Properties at Runtime Check if a property exists in a class

Examples related to collections

Kotlin's List missing "add", "remove", Map missing "put", etc? How to unset (remove) a collection element after fetching it? How can I get a List from some class properties with Java 8 Stream? Java 8 stream map to list of keys sorted by values How to convert String into Hashmap in java How can I turn a List of Lists into a List in Java 8? MongoDB Show all contents from all collections Get nth character of a string in Swift programming language Java 8 Distinct by property Is there a typescript List<> and/or Map<> class/library?

Examples related to javabeans

DTO and DAO concepts and MVC Spring cannot find bean xml configuration file when it does exist What is java pojo class, java bean, normal class? org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customerService' is defined javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean For a boolean field, what is the naming convention for its getter/setter? What is a JavaBean exactly? Difference between DTO, VO, POJO, JavaBeans? How to convert a Java object (bean) to key-value pairs (and vice versa)?