Java Bean to Map

1、 Background

In some business scenarios, Java beans need to be converted into Maps for reuse.

2、 Those pits

2.0 Test object

2.1 JSON deserialized and lost type

2.1.1 Problem recurrence

The most common way to convert Java beans to Maps is to use JSON frameworks, such as fastjson, gson, jackson, etc.

However, using JSON to convert Java beans to Maps will cause some data types to be lost.

For example, if you use fastjson, when the attribute is of Long type but the number is less than the maximum value of Integer, it will become of Integer type after the inverse sequence is converted into Map.

2.2.2 Problem description

There are two problems

(1) The type will change when the Java bean is converted to a Map through fastjson.

For example, Long becomes Integer, Date becomes Long, and Double becomes Decimal.

(2) In some scenarios, the key of the Map does not exactly correspond to the attribute name, such as the attribute name "inferred" through the get set method.

2.2 BeanMap conversion attribute name error

2.2.1 BeanMap of commons-beanutils

From the code, we can see that

(1) When the length of name is>1 and the first and second characters are capitalized, the parameter is directly returned as PropertyDescriptor name.

(2) Otherwise, convert name to lowercase

The original intention of this processing is to prevent the attribute from being converted into uRL from such abbreviations as URL, which "hurts" our scenario.

2.2.2 BeanMap using cglib

Cglib dependency

We found that the type is correct, but the property name is still incorrect.

Key code:

net.sf.cglib.core.ReflectUtils#getBeanGetters

The underlying layer will also use java. beans. Introspector # decapitalize, so it is not surprising that the attribute name has the same problem.

3、 Solution

3.1 Solution

There are many solutions. This article provides a solution based on dubbo.

Maven depends on:

3.2 Principle analysis

You can download the source code for a simple study.

https://github.com/apache/dubbo

Core code:

org.apache.dubbo.common.utils.PojoUtils#generalize(java.lang.Object)

public static Object generalize(Object pojo) {

eturn generalize(pojo, new IdentityHashMap());

}

It can be seen from this that when the attribute name is relatively special, it is also easy to cause problems, but the tool class dubbo is more in line with our expectations.

For more details, you can debug and learn by yourself according to DEMO.

If you want to be strictly consistent with attributes, you can use reflection to obtain attribute names and attribute values, and add caching mechanism to improve the efficiency of resolution.

4、 Summary

There are many pitfalls in converting Java beans to Maps. The most common problems are type loss and attribute name parsing errors.

You should be particularly careful when using the JSON framework and the Java Bean to Map framework.

When using some frameworks, write more DEMO for verification, read more source code, debug more, and make fewer trips to the pit.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us