当前位置:网站首页>Encapsulation of conversion between Json and objects (Gson)
Encapsulation of conversion between Json and objects (Gson)
2022-07-31 12:07:00 【wresource】
I. Preface
Some time ago, due to the backend of spring boot, it was often necessary to convert between Json and objects, so using Gson combined with the characteristics of Kotlin extension functions to encapsulate two very convenient functions, if it is in other languages, it can also be encapsulatedFor a moment, it is not convenient without Kotlin.The following shows the final packaging effect
Object to Json
val cat = Cat()cat.toMyJson()Json to Object
//Single object, here it needs to be converted into a single object manually, mainlyis an example of a unified formval cat = Cat()val data = cat.toMyJson()data.toMyObject()[0]//list typeval cats = List()val dataList = cats.toMyObject()[0] 2. Object to Json
This needs to be extended under Any. Since Any is the parent class of all classes, this is somewhat similar to Object in Java
fun Any.toMyJson():String{return Gson().toJson(this)}It can be used directly after encapsulation. Gson is not so difficult to deal with, but only the way of writing points saves the time of bracketing
val cat = Cat()cat.toMyJson()Three, Json transfer object
This is a bit problematic when encapsulating it at first. First of all, it is easier to find the extended object, which is the String type, but there is one thing that needs to be paid attention to. Here, the reflection mechanism is used for Gson conversion, so two processes need to be done
- Generic materialization processing
- Dealing with reflection-related issues
Final code effect
inline fun String.toMyObject(): List {val listType: Type = `$Gson$Types`.newParameterizedTypeWithOwner(null, ArrayList::class.java, T::class.java)return if(!contains("[")){Gson().fromJson("[${this}]", listType)}else{Gson().fromJson(this, listType)}} Use of packaging
val cat = Cat()val data = cat.toMyJson()data.toMyObject()[0] However, there is a disadvantage here that if it is a single object, you need to manually take the first value as the object, because the return value is of the list type. If there is a way to change the return value type, please leave a message in the comment area
/p>
Fourth, issues that need attention
Object to Json needs attention
This only needs to pay attention to the format after conversion, list or single object can be directly converted
Json to object needs attention
What needs to be noted is that the type of the object should be clear, and the non-Json format string cannot be converted successfully, although it has this method, and then my current plan is to convert it into a unified list type, so you need to manually get the value
V. Summary
It was quite a headache when I encountered these before, and I often needed to go back and forth. Many articles on the Internet did not explain it clearly. Although the packaged library is only a few lines of code, in short, convenient development is the bestYes, the simpler the better.
边栏推荐
- CWE4.8 -- The 25 most damaging software security issues in 2022
- A40i/T3 uboot启动时对PMU部分初始化
- 全动力学约束的机器人高效时间最优轨迹规划
- 连续变量离散化教程
- Use docker to build mysql master-slave
- 这款悄然崛起的国产API接口管理工具,你一定要晓得
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- Docker practical experience: Deploy mysql8 master-slave replication on Docker
- [Shader] Shader official example [easy to understand]
- 关于IDEA开发工具的介绍
猜你喜欢

ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger

DCM middleware family welcomes a new member

Android studio connects to MySQL and completes simple login and registration functions

一文吃透接口调用神器RestTemplate

订song餐系统

JVM 运行时数据区与JMM 内存模型详解

am335x 看门狗驱动&看门狗应用例程序

Data Persistence Technology - MP

JVS轻应用的组成与配置

imx6ull看门狗使用
随机推荐
SAP Commerce Cloud Product Review 的添加逻辑
502 bad gateway causes and solutions
0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
MySQL index usage and optimization
MySQL row-level locks (row locks, adjacent key locks, gap locks)
在 Excel 内使用 ODBC 消费 SAP ABAP CDS view
Shengxin Weekly Issue 38
Data Persistence Technology - MP
vb.net 画曲线
基于稳态视觉诱发电位和注意力脑电的混合脑机接口系统
Json和对象之间转换的封装(Gson)
Use docker to build mysql master-slave
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
一周精彩内容分享(第14期)
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
In PLC communication error or timeout or download the prompt solution of the model
Qt鼠标穿透
建情人节表白网站(超详细过程,包教包会)
CWE4.8 -- The 25 most damaging software security issues in 2022
Spark GC日志分析