Net. Net in Java sf. The JSON package contains information about the interaction between JSON and objects

Data interaction is indispensable in the process of web development, so it is necessary to specify the relevant format of interactive data so that the data can be transmitted between the client and the server. There are usually two data formats: 1. XML; 2、JSON。 Generally speaking, JSON is used to pass data. This article introduces some problems and related suggestions when converting JSON and objects in Java.

First, it is clear that there are two concepts for JSON:

JSON object (JavaScript object notation). It seems that it is only customized by bit JavaScript, but as a syntax, it is independent of language and platform. It just means that we usually work on the client When transmitting data to the server, the (browser) uses JSON format, which is used to represent JavaScript objects. It is composed of a series of "key values", such as {"Id": 1, "name": "Kevin"} , this is somewhat similar to how map key value pairs are stored. The JSON object described in Java actually refers to the jsonobject class, which is usually named after this name in each third-party jsonobjar package. Different jar packages have slightly different internal implementations.

JSON string. The conversion between JSON objects and JSON strings is a process of serialization and deserialization, which is like serialization and deserialization of Java objects. In the network, data is transmitted through strings or binary streams, That is, when the client (browser) needs to transfer the data in JSON format, the string is transmitted in the network, and the server is also a string (string type) after receiving the data. Sometimes it is necessary to convert the JSON string into a JSON object before the next operation (string type into JSON object type).

The clarity of the above two concepts basically defines the JSON data format, or JSON syntax. There are many jar packages for JSON in Java, and the most "commonly used" is the jar package provided by "net. SF. JSON". This article focuses on this pit package. Although it is a pit, it has a wide range of applications. In fact, there are other excellent JSON packages for us to use, such as fastjson, the fastest JSON package claimed by Ali, gson of Google and Jackson. Try or never use the "net. SF. JSON" package. It's not only pit, but also very old. It's too old to download the source code in the idea. Maven warehouse shows that it stopped updating in version 2.4 in 2010. Let's talk about the two known "net. SF. JSON" bugs (I think they are bugs) and how they are generated.

JSON package in Java -- net sf. json

1. When Java objects convert JSON objects, all methods starting with get will be converted

What does this mean, for example, the following existing Java objects.

When we convert student objects into JSON objects, we want the converted JSON format to be:

However, in jsonobject using the "net. SF. JSON" package, JSON = jsonobject fromObject(student); The result of API conversion is:

In other words, it can be guessed that "net. SF. JSON" obtains the method beginning with the public modifier get in the Java object, defines its suffix as the "key" of the JSON object, and defines the return value of the method beginning with get as the "value" of the corresponding key. Note that the method beginning with the public modifier get has a return value.

I think this is an unreasonable conversion rule. If I define a method in a Java object, just because the method starts with "get" and has a return value, it will be exposed as the "key value" of the converted JSON object? Or return to the client (browser) is directly exposed to the front-end console? The author stipulates this conversion rule. I think the general reason is: since you define the public method and name it get, you deliberately expose this method so that the calling client can get it. But I still think this is unreasonable, and even I define it as a bug. I don't think so Xu is also unreasonable, because according to my actual measurement, not only the "net. SF. JSON" package will be converted according to this rule, but also fastjson and Jackson. Only Google's gson does not convert objects to JSON according to this rule.

Through jsonobject. JSON = jsonobject fromObject(student); Convert the constructed Student object into JSON object, as described above. After entering this method, the overloaded method of fromobject (object, jsonconfig) will continue to be called. In this overloaded method, instanceof will be used to judge whether the object to be converted is of enumeration, annotation and other types. These special types will have special judgment methods. Here is an ordinary Java POJO object, so you will enter_ From object (object, jsonconfig), there will be some judgment in this method, and finally create a JSON object by calling defaultbeanprocessing. This method is the key, and will continue through propertyutils The getpropertydescriptors (bean) method to obtain the "property descriptor" is actually to obtain the method with get, which is encapsulated here as a propertydescriptor. Four of these students will be obtained in this class: getClass, getid, getcourseids and getsql.

In fact, the propertydescriptor has been encapsulated in great detail, and all read-write methods have been assigned.

For example, the getsql method has been resolved to the propertydescriptor in the figure above. Then filter out some methods through this class. For example, getClass method is not a method in POJO, so it does not need to be converted into JSON object. The propertydescriptor is obtained through beaninfo #getpropertydescriptors, and beaninfo is obtained through new introspector (beanclass, null, use_all_beaninfo) getBeanInfo(); Continue to deepen, and finally reach the following methods.

Yes. Net sf. After a brief analysis of the source code of JSON, it is found that as guessed, there are many specific source codes with limited space, so you need to check and track them yourself.

2. When converting JSON objects to Java objects, list < long > will have conversion errors

The title can't be explained clearly in one sentence. I'm sure it's a bug.

Now there is a JSON string of {"Id": 1,3]}, which needs to be converted to the student object mentioned above. There are two attribute fields of int and list < long > types in the student object, that is, this JSON string should be converted to the corresponding data type.

The above output should be true, but unfortunately it is false. To be exact, it is long at compile time and integer at run time. This has to be said to be a pit. This error does not occur in the other three JSON packages. So I'm sure it's a bug. Take a look at this bug in net sf. You also need to compare the source code to see how JSON occurs. I found net. Net when I interrupted the debug and went deeper sf. JSON found this method numberutils#createnumber when processing integer data. This class judges its data type when taking data from the string. The original intention is to process the number as long if it is followed by "L" or "L". From here, the last result should be right.

Indeed, so far net sf. JSON accurately judges the data type through the identifier after the number. The problem is that after obtaining the value and its data type, it needs to be stored in jsonobject. In the process of storage, there is the method jsonutils#transformnumber. At least at present, the existence of this method is just adding to the snake.

The above code clearly shows the culprit, Whether it's long (long type in the range of integer), including byte and short, will be converted to integer. I still don't understand the meaning of this code. Before that, I have to determine the exact data type according to the letter after the number, and then convert the exact data type again, which leads to the bug mentioned at the beginning. This problem is almost unavoidable, so the best way is not to avoid it Yes.

These two pits are discovered by accident. It is recommended not to use net which has not been maintained for a long time sf. JSON package of JSON. In addition, net sf. The JSON package does not verify the JSON format strictly. If the format "{" Id ": 1," courseids ":" [1,3]} ", exceptions will be thrown in the other three packages, but net sf. JSON does not.

The above article discusses net. Net in Java in detail sf. The pit of JSON package on the mutual transformation between JSON and objects is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>