【技术累积】【点】【java】【27】@JSONField

814386-20181030102602864-1082958472.png

@JSONField

该注解隶属于阿里fastjson,方便fastjson处理对象时的一些操作

源码

@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 {};/*** @since 1.2.31*/boolean unwrapped() default false;
}
  • ordinal() 顺序设置。

    缺省fastjson序列化一个java bean,是根据fieldName的字母序进行序列化的,你可以通过ordinal指定字段的顺序。这个特性需要1.1.42以上版本

  • name() 指定名字。指定后,序列化后字段名变为指定名称,反序列化同理;
  • format() 指定日期格式;
  • serialize() deserialize() 是否序列化和反序列化,默认true。
  • serialzeFeatures() 序列化特性选择,具体参考后面文章吧;通过枚举选择
  • parseFeatures() 解释特性选择;通过枚举选择
  • label() 打标记,可定制化输出

@JSONField(label = "normal")public int getId() {return id;}public void setId(int id) {this.id = id;}@JSONField(label = "normal")public String getName() {return name;}
System.out.println(JSON.toJSONString(v, Labels.includes("normal")));
  • jsonDirect() 直接输出而不经过json转译

    在fastjson-1.2.12版本中,JSONField支持一个新的配置项jsonDirect,它的用途是:当你有一个字段是字符串类型,里面是json格式数据,你希望直接输入,而不是经过转义之后再输出。

  • serializeUsing() deserializeUsing() 指定序列化、反序列化使用自定义Serialize、Parser(注意,直接写在类中即可,格式固定,关键是取值)

//示例1
public static class TimeStampDeserializer implements ObjectDeserializer {@Overridepublic <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {String time = parser.getLexer().stringVal();if (StringUtils.isBlank(time)) {return (T)String.valueOf(new Date().getTime());} else {return (T)(time + "000");}}@Overridepublic int getFastMatchToken() {return 0;}}//示例2
public static class Model {@JSONField(serializeUsing = ModelValueSerializer.class)public int value;
}public static class ModelValueSerializer implements ObjectSerializer {@Overridepublic void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,int features) throws IOException {Integer value = (Integer) object;String text = value + "元";serializer.write(text);}
}
  • alternateNames() 多名字。允许多个名字的变量转成一个
@JSONField(alternateNames = {"user", "person"})
public String name;
  • unwrapped() 不封箱

搭配JSON.parseObject

给输出结构体中field配置上@JSONField(name="aaa")等,直接转出得到的json串

Response response = JSON.parseObject(res, Response.class);@Setter
@Getter
@NoArgsConstructor
@ToString
public class Response {@JSONField(name = "answerList")private List<Answer> answers;@JSONField(name = "timeStamp")private String timeStamp;@JSONField(name = "type")private String answerType;@JSONField(name = "id")private String erp;@JSONField(name = "session")private String sessionId;@JSONField(name = "topic")private String topic;@JSONField(deserialize = false, serialize = false)private String answerType = AnswerTypeEnum.ANSWER.getCode();
}

参考文章

  • Fastjson JSONField
  • JSONField_jsonDirect_cn
  • FastJson 自定义Serialize、Parser
  • fastjson使用(八) -- LabelFilter的使用
  • fastjson的@JSONField注解的一点问题
  • fastjson SerializerFeature详解

转载于:https://www.cnblogs.com/andy1202go/p/9875324.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279080.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

感谢支持,超预期重印并加码

今天&#xff0c;要向广大读者朋友带来一个&#xff0c;连我自己和出版社都感到十分意外的好消息&#xff0c;几天前接到出版社的通知&#xff0c;说今年元月出版的《Cisco/H3C交换机配置与管理完全手册》&#xff08;第二版&#xff09;马上就要下单重印了&#xff0c;而且一下…

如何从手机远程控制uTorrent

You’re a geek on the go and it’s important to keep tabs on your torrents when you’re away from home. Today we take a peak at how you can monitor, manage, and even start your torrent downloads when you’re away from your computer. 您是旅途中的怪胎&#x…

php获取一个文件名的函数,PHP 文件系统函数之获取文件名及文件名后缀-php文件...

获取文件名(包含扩展):1.用PHP 文件函数 basename获取例&#xff1a;$filename "/home/httpd/html/index.php";$file basename($filename);2.先获取位置再获取文件名例:$filename "/home/httpd/html/index.php";$pos strrpos($filename, /);if ($pos …

tasker使用手册_如何开始使用Tasker调整Android手机

tasker使用手册Tasker is a powerful app for Android that lets you customize how your phone works and automate tasks. Unfortunately, it’s got a bit of a learning curve. We’re here to show you how to get started and turn your phone into a flashlight in the …

iPhone 软件:xlate free 编码的好帮手!

功能菜单&#xff1a; 1 文本 2 二进制 3 Char 值 4 Base64 5 反向 如果需要把一段中文编码请选择UTF16&#xff0c;如果是英文就选择UTF8。对于需要经常使用编码切换的朋友是个好帮手。 也可以用来简单加密&#xff1a;我们先在文本状态下输入一段不想让别人知道或需要保密的文…

如何提取幻灯片表格_如何查看对Google文档,表格或幻灯片文件的最新更改

如何提取幻灯片表格The Google Suite offers you a handy way to view all the changes that have occurred in a file on Google Docs, Sheets, or Slides. This is extremely useful when you’ve made lots of changes to a file or are working as part of a team and need…

支付宝红包php,支付宝红包赏金跳转源码,一键复制红包码,裂变推广

[html]代码库支付宝到店红包搜索码跳转推广裂变-引流*{padding:0;margin:0;}.main{overflow: hidden;}a {color:black;}.main img{width:100%;outline-width:0px;vertical-align:top;}.main{position: relative;}.main .copy-container{width: 100%;height: 0.42rem;position: …

apt-get更新软件包_如何使用Apt-fast加速软件包下载和更新

apt-get更新软件包By Default, Ubuntu uses apt-get to install packages and updates. Apt-get is a good tool but you can get much faster download speeds using Apt-Fast when downloading and updating your Ubuntu box. 默认情况下&#xff0c;Ubuntu使用apt-get安装软…

ipad iphone开发_如何在iPad或iPhone上使用外部GPS设备

ipad iphone开发If you bought a Wi-Fi only iPad and now you wish you could use GPS with it, this is the guide for you. Follow along to hook your iPad up to an external GPS unit and/or GPS-enabled smartphone phone. 如果您购买了仅支持Wi-Fi的iPad&#xff0c;现…

fc-ae-1553_什么是AE-L,AF-L和*按钮,它们的作用是什么?

fc-ae-1553DSLRs and mirrorless cameras have a lot of buttons. If you’re just starting to get the hang of manually controlling your camera, you’re probably wondering what all the—seemingly non-essential—ones do. Let’s take a look at the AE-L, AF-L, AF-…

大学留级两年不敢和家人说_您说什么:如何与家人保持联系?

大学留级两年不敢和家人说Earlier this week we asked you to share your tips, tricks, and techniques for staying connected when you’re away from your home broadband connection. Now we’re back with a roundup of what you said. 本周早些时候&#xff0c;我们要求…

DevExpress v17.2新版亮点—WinForms篇(四)

2019独角兽企业重金招聘Python工程师标准>>> DevExpress首推团队升级培训套包&#xff0c;最高可省10万元&#xff01;查看详情>>> 用户界面套包DevExpress v17.2终于正式发布&#xff0c;本站将以连载的形式为大家介绍各版本新增内容。开篇介绍了DevExpre…

Solr 11 - Solr集群模式的部署(基于Solr 4.10.4搭建SolrCloud)

目录 1 SolrCloud结构说明2 环境的安装2.1 环境说明2.2 部署并启动ZooKeeper集群2.3 部署Solr单机服务2.4 添加Solr的索引库3 部署Solr集群服务(SolrCloud)3.1 启动ZooKeeper3.2 ZooKeeper管理配置文件3.3 修改SolrCloud监听端口3.4 关联Solr与ZooKeeper3.5 分发SolrCloud服务3…

matlab怎么画一箭穿心,MATLAB学习与使用:如何绘制三维心形图 经验告诉你该这样...

MATLAB是MATrix & LABoratory(矩阵实验室)的缩写&#xff0c;是一款强大的科学软件&#xff0c;具有编程、绘图、仿真等功能。利用MATLAB绘制一款三维的心形图&#xff0c;然后送给心爱的姑娘&#xff0c;也是理工男撩妹的一项小技能。工具/材料MATLAB三维心形图操作方法01…

mac重置系统_如何在Mac上重置打印系统

mac重置系统Printers are notorious for failing frequently. A quick restart of the printer or computer usually fixes most intermittent issues, but occasionally it’s best to reset your printing settings and reinstall the printer completely. Consider this a f…

COW奶牛!Copy On Write机制了解一下

前言 只有光头才能变强 在读《Redis设计与实现》关于哈希表扩容的时候&#xff0c;发现这么一段话&#xff1a; 执行BGSAVE命令或者BGREWRITEAOF命令的过程中&#xff0c;Redis需要创建当前服务器进程的子进程&#xff0c;而大多数操作系统都采用写时复制&#xff08;copy-on-w…

windows 全局变量_如何在Windows中使用全局系统环境变量

windows 全局变量Any system administrator who spends a good bit of time in the command prompt or batch scripts is probably well aware of built in environment variables Windows offers (i.e. Path, WinDir, ProgramFiles, UserProfile, etc.). If you find yourself…

twitter api使用_使用P2创建自己的Twitter风格的组博客

twitter api使用Would you like a great way to post stuff quickly online and communicate with your readers? Here’s how you can use the P2 theme to transform WordPress into a great collaboration and communications platform. 您是否想以一种很好的方式在网上快…

10_30_unittest

1、断言 1&#xff09;、self.assertEqual(2,res)#期望值qian、结果值hou2&#xff09;TextTestRunner 源码 必要的参数3&#xff09;测试结果 上下文管理器 with open("test.txt",w,encodingutf-8) as file:runner unittest.TextTestRunner(streamfile,verbosity2)…

laravel graphql php,结合 Laravel 初步学习 GraphQL

本文字数&#xff1a;7134&#xff0c;大概需要14.27分钟。按照官网所述的&#xff1a;A query language for your API一种用于 API 的查询语言GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a…