Fastjson妙用之@JSONField注解
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">研发</span>的过程中<span style="color: black;">运用</span>json格式的<span style="color: black;">地区</span>非常多,<span style="color: black;">此刻</span>前后端分离的项目中,前后端数据交换的格式<span style="color: black;">通常</span>为json,这种格式的优/缺点<span style="color: black;">这儿</span><span style="color: black;">再也不</span>赘述,感兴趣的<span style="color: black;">能够</span>百度。把java中的实体类序列化为json的方式<span style="color: black;">亦</span>有<span style="color: black;">非常多</span>方式,今天来<span style="color: black;">瞧瞧</span>常用到的fastjson。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">都<span style="color: black;">晓得</span>fastjson是阿里开源的一个序列化/反序列化的jar包,在<span style="color: black;">平常</span>的<span style="color: black;">研发</span>过程中经常会碰到,<span style="color: black;">亦</span>是<span style="color: black;">运用</span>频率较高的一款工具。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>要<span style="color: black;">认识</span>一个概念,那<span style="color: black;">便是</span>序列化/反序列化,序列化是把一个java对象转化为其他的形式,如json、XML、对象字节;反序列化则是把json、XML、对象字节转化为java对象。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>初识Fastjson</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1、环境搭建</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">想要</span><span style="color: black;">运用</span>fastjson,<span style="color: black;">首要</span>需要引入fastjson的包,我<span style="color: black;">这儿</span><span style="color: black;">运用</span>的是maven工具,<span style="color: black;">因此</span><span style="color: black;">这儿</span>只要在pom文件中添加相应的依赖<span style="color: black;">就可</span>,</span></p><!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency><h1 style="color: black; text-align: left; margin-bottom: 10px;">2、一个小例子</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有一个实体类,</span></p>package com.atssg.test;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String address;
private String phoneNumber;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面看测试<span style="color: black;">办法</span></span></p>package com.atssg.test;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestFastJson {
public static void main(String[] args) {
Student student=new Student();
student.setId(1);
student.setName("tom");
student.setAddress("beijing");
student.setPhoneNumber("010-6668899");
//把对象转化为json串
String jsonString=JSON.toJSONString(student);
log.info("student序列化为:{}",jsonString);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面看测试结果</span></p>22:22:41.657 INFO com.atssg.test.TestFastJson - student序列化为:{"address":"beijing","id":1,"name":"tom","phoneNumber":"010-6668899"}
Process finished with exit code 0<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">从上面<span style="color: black;">能够</span>看到打印除了序列化为json的字符串,<span style="color: black;">这儿</span>是一个序列化的过程,当然<span style="color: black;">亦</span><span style="color: black;">能够</span>把一个字符串反序列化为java对象。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>@JSONField注解</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的例子中,student被序列化为一个json字符串,字符串是键值对的形式,键是Student的属性名。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>有<span style="color: black;">这般</span>的一个<span style="color: black;">需要</span>,要返回给前端Student的json字符串,但不想给前端phoneNumber字段,你要怎么做那?一个比较容易想到的方式是从字符串中把这个属性去掉,<span style="color: black;">或</span>重新生成一个不<span style="color: black;">包括</span>phoneNumber的实体对象。难道<span style="color: black;">无</span>更好的方式么</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在fastjson<span style="color: black;">供给</span>了@JSONField<span style="color: black;">这般</span>一个注解。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1、@JSONField(serialize = false)</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">@JSONField注解中有seaialize<span style="color: black;">这般</span>一个属性,看名<span style="color: black;">叫作</span>应该和序列化<span style="color: black;">相关</span>,尝试下<span style="color: black;">瞧瞧</span>效果,</span></p>package com.atssg.test;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String address;
@JSONField(serialize = false)
private String phoneNumber;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">再看上面的测试类的执行结果,</span></p>22:30:26.678 INFO com.atssg.test.TestFastJson - student序列化为:{"address":"beijing","id":1,"name":"tom"}
Process finished with exit code 0<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到轻<span style="color: black;">容易</span>松phoneNumber不见了,<span style="color: black;">亦</span><span style="color: black;">便是</span>说@JSONField(serialize=false)起<span style="color: black;">功效</span>了,该属性不会参与序列化,反之,不加该注解或serialize的值设为true,则会参与序列化。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">2、@JSONField(name= "XXX")</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在和前端的交互过程中,可能存在<span style="color: black;">这般</span>的<span style="color: black;">状况</span>,java类中定义的字段的形式为驼峰,但前端需要的是以_连接的形式,要怎么做那,在@JSONField中有name属性,</span></p>package com.atssg.test;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String address;
@JSONField(name = "phone_number")
private String phoneNumber;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在phoneNuber上注解@JSONField且name为phone_number,看测试结果,</span></p>21:47:48.766 INFO com.atssg.test.TestFastJson - student序列化为:{"address":"beijing","id":1,"name":"tom","phone_number":"010-6668899"}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到在序列化的json字符串中<span style="color: black;">显现</span>了phone_number,说明@JSONField起了<span style="color: black;">功效</span>,在序列化的时候指定序列化字段的名<span style="color: black;">叫作</span>,<span style="color: black;">倘若</span>指定了<span style="color: black;">运用</span>指定的(@JSONField中name的值),<span style="color: black;">倘若</span>不指定<span style="color: black;">运用</span>java类中属性的名字。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">3、@JSONField注解</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面,<span style="color: black;">咱们</span>看了@JSONField的两种用法,还有<span style="color: black;">非常多</span>用法,<span style="color: black;">这儿</span>不一一列举,看下@JSONField这个注解的定义,</span></p>//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.alibaba.fastjson.annotation;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface JSONField {
int ordinal() default 0;
String name() default "";
String format() default "";
boolean serialize() default true;
boolean deserialize() default true;
SerializerFeature[] serialzeFeatures() default {};
Feature[] parseFeatures() default {};
String label() default "";
boolean jsonDirect() default false;
Class<?> serializeUsing() default Void.class;
Class<?> deserializeUsing() default Void.class;
String[] alternateNames() default {};
boolean unwrapped() default false;
String defaultValue() default "";
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个注解上<span style="color: black;">运用</span>了@Target注解标注,@Target中的值为{ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER},<span style="color: black;">亦</span><span style="color: black;">便是</span>说@JSONField<span style="color: black;">能够</span>标注在<span style="color: black;">办法</span>上、字段、参数上。在上面的例子中,<span style="color: black;">咱们</span>把@JSONField用在了字段上,下面看<span style="color: black;">运用</span>在<span style="color: black;">办法</span>上的例子,在一个类中有getXXX/setXXX<span style="color: black;">办法</span>,看@JSONField用在getXXX/setXXX的用法。</span></p>package com.atssg.test;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String address;
private String phoneNumber;
//getXXX<span style="color: black;">办法</span>用在序列化过程中
@JSONField(serialize = false)
public String getPhoneNumber() {
return phoneNumber;
}
//setXXX<span style="color: black;">办法</span>用在反序列化过程中
@JSONField(deserialize = false)
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">看测试结果</span></p>21:59:28.334 INFO com.atssg.test.TestFastJson - student序列化为:{"address":"beijing","id":1,"name":"tom"}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">再来看反序列化的测试结果,测试类,</span></p>package com.atssg.test;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestFastJson {
public static void main(String[] args) {
Student student=new Student();
student.setId(1);
student.setName("tom");
student.setAddress("beijing");
student.setPhoneNumber("010-6668899");
//把对象转化为json串
String jsonString=JSON.toJSONString(student);
log.info("student序列化为:{}",jsonString);
//反序列化
String str="{\"address\":\"beijing\",\"id\":1,\"name\":\"tom\",\"phoneNumber\":\"010-6668899\"}";
Student stu=JSON.parseObject(str,Student.class);
log.info("反序列化的结果:{}",stu);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>了含有phoneNumber的字符串,看反序列化的结果,</span></p>22:04:04.746 INFO com.atssg.test.TestFastJson - student序列化为:{"address":"beijing","id":1,"name":"tom"}
22:04:04.776 INFO com.atssg.test.TestFastJson - 反序列化的结果:Student(id=1, name=tom, address=beijing, phoneNumber=null)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到phoneNumber的值为null,证明该属性未取得反序列化的值,证明了@JSONField用在setXXX<span style="color: black;">办法</span>上起到了反序列化的<span style="color: black;">掌控</span><span style="color: black;">功效</span>。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>总结</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本文分享了fastjson中@JSONField的用法,<span style="color: black;">重点</span>是在序列化/反序列化的过程中,<span style="color: black;">重点</span><span style="color: black;">表现</span>在以下几个方面,</span></p><span style="color: black;">@JSONField用在字段上影响序列化/反序列化两个过程;</span><span style="color: black;">@JSONField用在setXXX、getXXX<span style="color: black;">办法</span>上,分别影响反序列化、序列化过程;</span>
楼主继续加油啊!外链论坛加油! 回顾历史,我们感慨万千;放眼未来,我们信心百倍。
页:
[1]