当前位置:网站首页>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
边栏推荐
- 重剑无锋,大巧不工
- Installation du compilateur croisé de la plateforme zynq
- Interaction of diet gut microbiota on cardiovascular disease
- The perfect combination of collaborative process and multi process
- zynq平台交叉编译器的安装
- The unity camera rotates with the mouse
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
- leetcode003--判断一个整数是否为回文数
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Unity攝像頭跟隨鼠標旋轉
猜你喜欢
补充番外14:cmake实践项目笔记(未完待续4/22)
520. Detect capital letters
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Coinbase:关于跨链桥的基础知识、事实和统计数据
Supplément: annotation
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
Mysql50 basic exercises
The perfect combination of collaborative process and multi process
[paper reading] [3D object detection] voxel transformer for 3D object detection
Spark small case - RDD, spark SQL
随机推荐
test
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
leetcode006--查找字符串数组中的最长公共前缀
leetcode003--判断一个整数是否为回文数
MySQL queries users logged in for at least N consecutive days
Use recyclerview to realize left-right side-by-side classification selection
Logger and zap log Library in go language
Go反射—Go语言圣经学习笔记
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
从MySQL数据库迁移到AWS DynamoDB
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
C language: spoof games
Installation and deployment of Flink and wordcount test
Spark case - wordcount
Basic operation of sequence table
Eksctl deploying AWS eks
Chapter 4 - understanding standard equipment documents, filters and pipelines
Spark small case - RDD, broadcast
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
leetcode002--将有符号整数的数字部分反转