首先这个注解有个参数可以设置,具体是:ordinal,name,format,serialize,deserialize。等等,后面的不常用啊。
先看model类。
package com.lxk.model; import com.alibaba.fastjson.annotation.JSONField; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; /** * 鸟 * * @author LiXuekai on 2018/10/25 */ @Data @NoArgsConstructor @AllArgsConstructor public class Bird { //@JSONField(ordinal = 6, name = "内容是json字符串的属性", jsonDirect = true) //private String dog1; //@JSONField(ordinal = 5, name = "内容是json字符串的属性,对比下差异") //private String dog2; /** * ordinal,默认值为0,不用设置啦。 */ @JSONField(ordinal = 4, name = "生产日期", format = "yyyy年MM月dd日 HH时mm分ss秒") private Date birthday; @JSONField(ordinal = 3, name = "颜色") private String color; @JSONField(ordinal = 2, name = "体型大小") private String size; @JSONField(ordinal = 1, name = "年龄") private int age; /** * 反序列化false,那么在反序列化的时候,就不会把json的值转给对象的这个属性。 */ @JSONField(name = "名称", deserialize = false) private String name; /** * 不序列化此属性字段,那么在转json的时候,就不会在json中出现 */ @JSONField(ordinal = 7, name = "不序列化的属性字段", serialize = false) private String deserialize; }model类,使用了这个注解,然后,main方法调用。看看各个注解的作用。
/** * 测试 fast json 中的JsonField注解的使用 */ private static void testJsonField() { String dog = JsonUtils.parseObjToJson(new Dog("大师兄的dog", true, true)); Bird bird = new Bird(new Date(), "红色皮肤", "巨大无比", 18, "典韦", "不序列化的字段,是不会被转json输出的"); System.out.println(bird.toString()); String s = JsonUtils.parseObjToJson(bird); System.out.println(s); Bird deserialize = JsonUtils.parseJsonToObj(s, Bird.class); System.out.println(deserialize == null ? "" : deserialize.toString()); }上面是测试方法,然后看运行结果:
分析一下:
大致步骤。先是序列化为json字符串。然后再从字符串变成对象,反序列化。
1,ordinal
这个值的设置,可以使对象的属性按这个顺序来输出,默认值是0,要是都不设置,那就按属性名称的字母顺序来输出,我不嫌弃麻烦,还真就删除model类里面的注解,又执行了一下,看下图的效果。可以和上面的那个对比一下。
可以看到,默认是按照属性的名称的字母顺序输出的。我这依次是a b c d n s开头的属性名称。
2,name
这个name厉害,直接把原来的属性名称代码,给替换成别名(姑且这么叫吧)这样子的话,这个json的易读性就大大提高啦。具体可以看下执行结果截图中的,自定义的toString方法的返回和json之后的差别。
3,format
这个针对日期属性,至于他的值,你可以随意设置格式化格式,前提是复合那个SimpleDateFormat这个类的格式化时间的格式就好,估计是通用的。可以参考下面这个连接,看看格式化日期的格式如何设置。
java 日期格式化-- SimpleDateFormat 的使用。字符串转日期,日期转字符串
4,serialize
序列化,默认值是true,都是序列化的,我在model里面设置了个false,然后可以看到,在输出json字符串中,这个属性对应的key和value都不见啦。就是这么个作用。
5,deserialize
这个反序列化,默认值也是true,默认都是需要反序列化的,我对name属性设置是false,在字符串转对象对时候,发现对象对name属性是null啦。就是这么个作用。
6,jsonDirect
这个说是,如果对象对某个属性的值是json字符串的话,就不要在做处理啦,直接就是json。对此我也测试了下,但是出了点问题。具体看执行效果图。
可以看到,序列化是没毛病的,但是在反序列化的时候,就失败了,具体原因嘛,
//(他在解析那个属性是直接json的时候,出错了,也就是json属性值没带斜杆的时候,出的问题。)
最后,看下他的源码类:
/* * Copyright 1999-2101 Alibaba Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.fastjson.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; /** * @author wenshao[szujobs@hotmail.com] */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) public @interface JSONField { /** * config encode/decode ordinal * @since 1.1.42 * @return */ 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 ""; /** * @since 1.2.12 */ boolean jsonDirect() default false; /** * Serializer class to use for serializing associated value. * * @since 1.2.16 */ Class<?> serializeUsing() default Void.class; /** * Deserializer class to use for deserializing associated value. * * @since 1.2.16 */ Class<?> deserializeUsing() default Void.class; /** * @since 1.2.21 * @return the alternative names of the field when it is deserialized */ String[] alternateNames() default {}; }属性不少啊,还有几个是使用指定的序列化类进行序列化和反序列化。label,没实验出来干啥的,等等吧。