In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "JSON serialization leads to Long types being made into Integer how to solve", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the problem that JSON serialization causes Long types to be Integer.
JSON serialization causes the Long type to be turned into an Integer pit
A recent error is that a field must be of type Long, converted to a JSON string through Map, and then saved to a field in DB.
Store words for Json and then deserialize them to Map (not custom regular objects)
For example, in order to provide general interface capability, a certain party caches the data of Map through JSON serialization and then JSON deserializes it to get Map.
Wait for the situation.
When you use it, you get the object through Key, and then you forcefully convert the object to Long, and then type conversion exception.
Java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Code @ Test (expected = ClassCastException.class) public void testJson () {String id = "id"; String name = "name"; Long idValue = 3000L; Map data = new HashMap (2); data.put (id, idValue); data.put (name, "Zhang San"); Assert.assertEquals (idValue, (Long) data.get (id)) String jsonString = JSON.toJSONString (data); / / Long is converted to Integer Map map = JSON.parseObject (jsonString, Map.class); Object idObj = map.get (id); Assert.assertTrue (idObj instanceof Integer); Assert.assertEquals (idValue, (Long) idObj);}
There is no need to serialize through JSON, although the type is Object, but the actual type is Long.
When serialized to Json, the Josn string has no Long type, and the reverse is also received by Object. If the number is less than the maximum value of Interger, it is converted to Integer!
Scheme
See my other article JSON deserialization Long to Integer or Double problem
Thinking
In short, try not to misuse some object attribute conversion tools in the development, resulting in attribute omissions or errors.
Don't abuse Map or JSONObject to pass parameters.
If it is JSON serialization with Object type, you should pay special attention to the above problems!
Error report from JSON format to int/long
Phenomenon: get the information by looking up the library (mysql), where the value of field An is 5 of the int type, and then in the front page, the data (json) is requested to the background through ajax (the type of An in the front page is Number), and the json data is received in the background with Map. At this time, the value of A changes to 5.0, from integer to floating point. At this point, a type conversion exception occurs when converted to int.
Problem: the json string passed from the front-end page can be received by map at the backend, and then use the get () method of map to obtain the corresponding attribute value. In this case, a conversion exception will occur when the attribute type is int or long.
The solution is to first convert the corresponding attributes to Number types, and then call the corresponding method of Number for conversion. There is one thing to note, however, that you need to determine whether the property is empty before converting, otherwise NullPointerException will appear.
Problems with Gson conversion causing int to double conversion
Usage scenario: the json string returned by the backend was originally of type int after being processed by gson, but now the string is of type double.
Resolve:
Int I = ((Number) fromJson.get ("int")). IntValue (); int I = Math.round (Double.parseDouble (fromJson.get ("int") .toString)); at this point, I believe you have a better understanding of "JSON serialization causes Long types to be turned into Integer". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.