当前位置:网站首页>Com alibaba. Common methods of fastjson
Com alibaba. Common methods of fastjson
2022-04-23 04:43:00 【Mount MONI】
Fastjson It's a Java library , Can be Java Object to JSON Format , Of course, it can also make JSON String conversion to Java object .
Provides toJSONString() and parseObject() Methods to Java Object and the JSON transformation . call toJSONString Fang Method to convert an object to JSON character string ,parseObject The method is reversed JSON Converts a string to an object .
Create a User object , Attributes are id,name,age,address,is_del,create_time,update_time wait
User user = new User();
user.setAddress("A1");
user.setAge(11);
user.setCreate_time(new Date());
user.setName("monishan");
1. call toJSONString Fang Method to convert an object to JSON character string
String result = toJSONStringIgnoreNullValue(user );
The output is :
{"address":"A1","age":11,"create_time":1623999789993,"name":"monishan"}
Some attribute values are null, Turn into JSON String will be ignored
add SerializerFeature.WriteMapNullValue That is to say value The value is null No display
String result = toJSONStringContainNullValue(object, SerializerFeature.WriteMapNullValue);
The output is :
{"address":"A1","age":11,"create_time":1623999789000,"del_time":null,"id":null,"is_del":null,"name":"monishan","update_time":null}
Some attributes need to be formatted , Or change value
String result = toJSONStringValueFilter(object);
Defining classes SimpleValueFilter, Realization ValueFilter Interface
public class SimpleValueFilter implements ValueFilter {
@Override
public Object process(Object object, String name, Object value) {
if (null == value) {
return "";//value The value is null Set to empty string
}
if ("name".equals(name)) {
return "***" + value + "***";
}
if ("create_time".equals(name)) {// Date formatting
return CommonUtils.dateFormat((Date)value, null);
}
return value;
}
}
The output is :
{"address":"A1","age":11,"create_time":"2021-06-18 15:03:09","del_time":"","id":"","is_del":"","name":"***monishan***","update_time":""}
2. take JSON String conversion to Java object
take json String to User user
User entity = toEntity(value, User.class);
take json String to List<User> userList
List<User> userList = toObject(value, new com.alibaba.fastjson.TypeReference<List<User>>() {});
[{"address":"A1","age":11,"create_time":1624003599005,"del_time":null,"id":null,"is_del":null,"name":"monishan","update_time":null}]
Encapsulation method :
/***
* <p>Title: toObject</p>
* <p>Description: take json Convert string to object of specified type , Use TypeReference You can explicitly specify the type of deserialization </p>
* @param <T>
* @param value
* @param typeReference
* @return
*/
public static <T> T toObject(String value, com.alibaba.fastjson.TypeReference<T> typeReference) {
return JSONObject.parseObject(value, typeReference, Feature.values());
}
/***
* <p>Title: toEntity</p>
* <p>Description: take json Convert string to object </p>
* @param <T>
* @param value
* @param clazz
* @return
*/
public static <T> T toEntity(String value, Class<T> clazz) {
return JSONObject.parseObject(value, clazz);
}
/***
* <p>Title: toJSONStringIgnoreNullValue</p>
* <p>Description: Convert object to json strand , The property value is null It doesn't show up </p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringIgnoreNullValue(T object) {
String result = JSONObject.toJSONString(object);
return result;
}
/***
* <p>Title: toJSONStringContainNullValue</p>
* <p>Description: Convert object to json strand , The property value is null It will also show </p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringContainNullValue(T object) {
String result = JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue);
return result;
}
/***
* <p>Title: toJSONStringValueFilter</p>
* <p>Description: Convert object to json strand , Rewrite the value filter to handle the serialized values uniformly according to the rules </p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringValueFilter(T object) {
String result = JSONObject.toJSONString(object, new SimpleValueFilter());
return result;
}
版权声明
本文为[Mount MONI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558291809.html
边栏推荐
- MySQL - data read / write separation, multi instance
- leetcode001--返回和为target的数组元素的下标
- 2021数学建模国赛一等奖经验总结与分享
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Logger and zap log Library in go language
- Unity rawimage background seamlessly connected mobile
- Spark small case - RDD, spark SQL
- 1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
- The last day of 2021 is the year of harvest.
- shell wc (统计字符数量)的基本使用
猜你喜欢

Improving 3D object detection with channel wise transformer

Simply drag objects to the item bar

Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
![[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN](/img/c5/3b3f9cf9a39bf14a68ac100294ca6c.png)
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN

How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics

Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition

Supplement: Annotation

520. Detect capital letters

補:注解(Annotation)

Innovative practice of short video content understanding and generation technology in meituan
随机推荐
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Better way to read configuration files than properties
Create VPC in AWS console (no plate)
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
简单的拖拽物体到物品栏
Practice and exploration of knowledge map visualization technology in meituan
AWS eks add cluster user or Iam role
华为机试--高精度整数加法
Unity3D 实用技巧 - 理论知识库(一)
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
FAQ of foreign lead and alliance Manager
Installation and use of Apache bench (AB pressure test tool)
Special topic of data intensive application system design
做数据可视化应该避免的8个误区
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
General enumeration constant class
Leetcode003 -- judge whether an integer is a palindrome number
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN