当前位置:   article > 正文

@JSONField的一些使用基础_jsonfield deserializeusing

jsonfield deserializeusing

@JSONField介绍

fastjson是阿里巴巴出品的快速解析json的一个工具,

 @JSONField就是里面为数不多的注解之一.也是最为重要的注解.它的内容如下:

  1. /*
  2. * Copyright 1999-2017 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.fastjson.annotation;
  17. import java.lang.annotation.ElementType;
  18. import java.lang.annotation.Retention;
  19. import java.lang.annotation.RetentionPolicy;
  20. import java.lang.annotation.Target;
  21. import com.alibaba.fastjson.parser.Feature;
  22. import com.alibaba.fastjson.serializer.SerializerFeature;
  23. /**
  24. * @author wenshao[szujobs@hotmail.com]
  25. */
  26. @Retention(RetentionPolicy.RUNTIME)
  27. @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
  28. public @interface JSONField {
  29. /**
  30. * config encode/decode ordinal
  31. * @since 1.1.42
  32. * @return
  33. */
  34. int ordinal() default 0;
  35. String name() default "";
  36. String format() default "";
  37. boolean serialize() default true;
  38. boolean deserialize() default true;
  39. SerializerFeature[] serialzeFeatures() default {};
  40. Feature[] parseFeatures() default {};
  41. String label() default "";
  42. /**
  43. * @since 1.2.12
  44. */
  45. boolean jsonDirect() default false;
  46. /**
  47. * Serializer class to use for serializing associated value.
  48. *
  49. * @since 1.2.16
  50. */
  51. Class<?> serializeUsing() default Void.class;
  52. /**
  53. * Deserializer class to use for deserializing associated value.
  54. *
  55. * @since 1.2.16
  56. */
  57. Class<?> deserializeUsing() default Void.class;
  58. /**
  59. * @since 1.2.21
  60. * @return the alternative names of the field when it is deserialized
  61. */
  62. String[] alternateNames() default {};
  63. /**
  64. * @since 1.2.31
  65. */
  66. boolean unwrapped() default false;
  67. }

其中里面最常用的属性是:name,format,serialize,deserialize,serializeUsing,deserializeUsing

fastjson编程式如何使用以及痛点

一般最常见的是这两个方法:

JSON.toJSONString(object):序列化对象,生产字符串
JSON.parse(text):反序列化对象,字符串转json

对于一般的没有什么变更的操作来说,这两个其实够用了.

但有时候情况会有些特殊,比如说:某个字段我想转成json时换个键名.或者我忽略某个字段等特殊操作,如果用编程式开发的话实在是有些烦闷的.比较坑爹.用注解的方式就很不错.

 

采用注解:

一个简单的demo如下:

  1. public class JSonTest {
  2. private String id ="111";
  3. private String name ="tom ";
  4. private Date date = new Date();
  5. ...getter/setter
  6. }
  1. public static void main(String[] args) {
  2. System.out.println(JSON.toJSONString(new JSonTest()));
  3. }

 

如果此时我想将这个类转成json,但要像"userid:111,name:tom"注意是userid,而不是id,那么这样既可.

  1. @JSONField(name="userid")
  2. private String id ="111";
  3. private String name ="tom ";

打印结果:{"date":1561545203036,"name":"tom ","userid":"111"}

加上这个注解使用name属性不止可以由类转成json,还可以解决由于json转成类时字段不一致的问题

如果我还想将userid放到前面形成{,"userid":"111","name":"tom "}这样呢?如下即可,ordinal是用来排序的.

  1. @JSONField(name="userid",ordinal=0)
  2. private String id ="111";
  3. @JSONField(ordinal=1)
  4. private String name ="tom ";

如果我在序列化的时候想过滤掉name这个字段怎么做呢?如下:

  1. @JSONField(serialize = false)
  2. private String name ="tom ";

如果我在反序列化的时候想过滤掉呢?如下:

@JSONField(deserialize = false)

如果我想让我的date时间变成特殊格式呢?

  1. @JSONField(format = "yyyy-MM-dd")
  2. private Date date = new Date();

如果我想在生成json或者解析json时获得的对象名字前面都加上"aaa"呢?这就需要具体的定制了.下面是解析和生成结合到了一起

  1. @JSONField(deserializeUsing = NameDeserializer.class,serializeUsing=NameDeserializer.class)
  2. private String name ="tom ";
  1. class NameDeserializer implements ObjectDeserializer,ObjectSerializer{
  2. @Override
  3. public <T> T deserialze(DefaultJSONParser parser, Type type,
  4. Object fieldName) {
  5. String val = (String) parser.parse();
  6. return (T) ("aaa " + val);
  7. }
  8. @Override
  9. public int getFastMatchToken() {
  10. // TODO Auto-generated method stub
  11. return 0;
  12. }
  13. @Override
  14. public void write(JSONSerializer serializer, Object object,
  15. Object fieldName, Type fieldType, int features)
  16. throws IOException {
  17. serializer.write("aaa " + object);
  18. }
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/982389
推荐阅读
相关标签
  

闽ICP备14008679号