fastjson简单使用demo,@JSONField注解属性字段上与set、get方法上。实体类toString(),实体类转json的区别;_下划线-减号大小写智能匹配
一、demo代码
@JSONField注解属性字段上与set、get方法上。使用@Data注解(lombok插件安装最下方),对属性“笔名”【pseudonym】手动重写setter/getter方法
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data; @Data
public class Stu { private String name;
//下划线自动转换
private String alias_name; //下划线自动转换
// @JSONField(name="pen_name")
private String penName; //实体类转为json,使用注解name值
@JSONField(name="pen_name2")
private String penName2; private String pseudonym; //实体类转为json,使用注解name值
@JSONField(name="pseud")
//不使用注解为属性名{"penName":"333","name":"1111","alias_name":"222","pen_name2":"444","pseudonym":"555"}
public String getPseudonym() {
return pseudonym;
} //json转为实体类,使用注解name值
@JSONField(name="pseudo")
public void setPseudonym(String pseudonym) {
this.pseudonym = pseudonym;
} public static void main(String[] args) {
String json = "{'name':'1111','aliasName':'222','pen_name':'333','pen_name2':'444','pseudo':'555'}";
Stu stu = JSON.parseObject(json, Stu.class); System.out.println(stu);
System.out.println(JSONObject.toJSON(stu));
}
}
二、main方法运行结果(控制台)
Stu(name=1111, alias_name=222, penName=333, penName2=444, pseudonym=555)
{"pseud":"555","penName":"333","name":"1111","alias_name":"222","pen_name2":"444"} Process finished with exit code 0
三、@Data备注
idea安装lombok插件。

四、源码
类:
JavaBeanDeserializer implements ObjectDeserializer
public FieldDeserializer smartMatch(String key, int[] setFlags) {
if (key == null) {
return null;
} else {
FieldDeserializer fieldDeserializer = this.getFieldDeserializer(key, setFlags);
if (fieldDeserializer == null) {
long smartKeyHash = TypeUtils.fnv1a_64_lower(key);
if (this.smartMatchHashArray == null) {
long[] hashArray = new long[this.sortedFieldDeserializers.length];
for(int i = ; i < this.sortedFieldDeserializers.length; ++i) {
hashArray[i] = TypeUtils.fnv1a_64_lower(this.sortedFieldDeserializers[i].fieldInfo.name);
}
Arrays.sort(hashArray);
this.smartMatchHashArray = hashArray;
}
int pos = Arrays.binarySearch(this.smartMatchHashArray, smartKeyHash);
boolean is = false;
if (pos < && (is = key.startsWith("is"))) {
smartKeyHash = TypeUtils.fnv1a_64_lower(key.substring());
pos = Arrays.binarySearch(this.smartMatchHashArray, smartKeyHash);
}
if (pos >= ) {
if (this.smartMatchHashArrayMapping == null) {
short[] mapping = new short[this.smartMatchHashArray.length];
Arrays.fill(mapping, (short)-);
for(int i = ; i < this.sortedFieldDeserializers.length; ++i) {
int p = Arrays.binarySearch(this.smartMatchHashArray, TypeUtils.fnv1a_64_lower(this.sortedFieldDeserializers[i].fieldInfo.name));
if (p >= ) {
mapping[p] = (short)i;
}
}
this.smartMatchHashArrayMapping = mapping;
}
int deserIndex = this.smartMatchHashArrayMapping[pos];
if (deserIndex != - && !isSetFlag(deserIndex, setFlags)) {
fieldDeserializer = this.sortedFieldDeserializers[deserIndex];
}
}
if (fieldDeserializer != null) {
FieldInfo fieldInfo = fieldDeserializer.fieldInfo;
if ((fieldInfo.parserFeatures & Feature.DisableFieldSmartMatch.mask) != ) {
return null;
}
Class fieldClass = fieldInfo.fieldClass;
if (is && fieldClass != Boolean.TYPE && fieldClass != Boolean.class) {
fieldDeserializer = null;
}
}
}
return fieldDeserializer;
}
}
“_”下划线及“-”短横线(减号),大小写都可以智能匹配。
类:
TypeUtils
public static long fnv1a_64_lower(String key) {
long hashCode = -3750763034362895579L;
for(int i = ; i < key.length(); ++i) {
char ch = key.charAt(i);
if (ch != '_' && ch != '-') {
if (ch >= 'A' && ch <= 'Z') {
ch = (char)(ch + );
}
hashCode ^= (long)ch;
hashCode *= 1099511628211L;
}
}
return hashCode;
}
fastjson简单使用demo,@JSONField注解属性字段上与set、get方法上。实体类toString(),实体类转json的区别;_下划线-减号大小写智能匹配的更多相关文章
- Fastjson妙用之@JSONField注解
在开发的过程中使用json格式的地方非常多,现在前后端分离的项目中,前后端数据交换的格式一般为json,这种格式的优/缺点这里不再赘述,感兴趣的可以百度.把java中的实体类序列化为json的方式也有 ...
- Django学习——Django测试环境搭建、单表查询关键字、神奇的双下划线查询(范围查询)、图书管理系统表设计、外键字段操作、跨表查询理论、基于对象的跨表查询、基于双下划线的跨表查询
Django测试环境搭建 ps: 1.pycharm连接数据库都需要提前下载对应的驱动 2.自带的sqlite3对日期格式数据不敏感 如果后续业务需要使用日期辅助筛选数据那么不推荐使用sqlite3 ...
- spring mvc简单的demo(注解版)
tomcat配置文件:web.xml <?xml version="1.0" encoding="UTF-8"? > <web-app ver ...
- C# 获取属性字段上DescriptionAttribute的值
var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...
- fastjson的@JSONField注解的一点问题
@JSONField 看源码它可以作用于字段和方法上. 引用网上说的, 一.作用Field @JSONField作用在Field时,其name不仅定义了输入key的名称,同时也定义了输出的名称. 但是 ...
- fastjson的@JSONField注解
@JSONField作用:在字段和方法上1.Field:@JSONField作用在Field时,name可以定义输入key的名字,反序列化的时 值不会赋值到属性上2.作用在setter和getter方 ...
- 私有化 : _x: 单前置下划线,私有化属性或方法;__xx:双前置下划线;__xx__:双前后下划线;属性property
私有化 xx: 公有变量 _x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问 __xx:双前置下划线,避免与子类中的属性命名冲突,无 ...
- FastJson中@JSONField注解使用
最近做项目中,使用了json格式在服务器之间进行数据传输.但是发现json格式数据不符合JAVA中的变量定义规则,并且难以理解,因此需要在后台中做二次处理,将数据处理成我们系统中定义的格式. 思路: ...
- 用jackson的@JsonProperty注解属性名,会多出一个字段
遇见了这个情况,我的字段定义是xVal,yVal,用的lombok的@Data注解.然后查询到了下面这偏文章,https://bbs.csdn.net/topics/392305619,里面的回答是图 ...
随机推荐
- docker 的安装和镜像
一.docker的 安装 : 第一种: yum -y install docker systemctl start docker.service systemctl status docker 第二种 ...
- pika常见问题解答(FAQ)
1 编译安装 Q1: 支持的系统? A1: 目前只支持Linux环境,包括Centos,Ubuntu: 不支持Windowns, Mac Q2: 怎么编译安装? A2: 参考编译安装wiki Q3: ...
- RedisTemplate5种数据结构操作
1 操作字符串 redisTemplate.opsForValue(); 2 操作hash redisTemplate.opsForHash(); 3 操作list redisTemplate.ops ...
- 外连接的用法 -- 《SQL进阶教程》 jupyter note
import pandas as pd import sqlite3 conn = sqlite3.connect('1-5.db') 用外连接进行行列转换1(行 -> 列): 制作交叉表 怎么 ...
- "在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配" 问题总结
最近C#连接ODBC数据源时,总是提示"[Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配",百度查询之后才知道原来是 ...
- 【神经网络与深度学习】【C/C++】ZLIB学习2
Zlib文件压缩和解压 开源代码:http://www.zlib.net/ zlib使用手册:http://www.zlib.net/manual.html zlib wince版:http://ww ...
- 51CTO下载-html+javascript+css学习宝典
一.html知识 1. meta标签 Meta: 提供网页信息,搜索引擎使用 <meta name=“aa” content=“bbb”> <meta http-equiv=“exp ...
- linux批量删除
find . -name "*.bcp" | xargs rm -rf "*.bcp"
- C关于字符串操作的库函数实现总结
常用C关于字符串操作的库函数实现: //获取字符串长度 int Strlen(const char* s) { assert(s != NULL); ; while (*s++ != '\0') { ...
- PTA(Advanced Level)1046.Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...