Fastjson JSONField


Fastjson JSONField


1. JSONField introduction


Note: 1. If the attribute is private Yes, there must be a set* method. Otherwise it cannot be deserialized.

package com.alibaba.fastjson.annotation;

public @interface JSONField {
    // 配置序列化和反序列化的顺序,1.1.42版本之后才支持
    int ordinal() default 0;

     // 指定字段的名称
    String name() default "";

    // 指定字段的格式,对日期格式有用
    String format() default "";

    // 是否序列化
    boolean serialize() default true;

    // 是否反序列化
    boolean deserialize() default true;
}

2. JSONField configuration method

FieldInfo can be configured on the getter/setter method or field. For example:

2.1 Configure on getter/setter

 
 public class A {
      private int id;

      @JSONField(name="ID")
      public int getId() {return id;}
      @JSONField(name="ID")
      public void setId(int value) {this.id = id;}
 }

2.2 Configure on field Up

  public class A {
      @JSONField(name="ID")
      private int id;

      public int getId() {return id;}
      public void setId(int value) {this.id = id;}
 }

3. Use format to configure date formatting

public class A {
      // 配置date序列化和反序列使用yyyyMMdd日期格式
      @JSONField(format="yyyyMMdd")
      public Date date;
 }

4. Use serialize/deserialize specifies fields not to be serialized

 public class A {
      @JSONField(serialize=false)
      public Date date;
 }

 public class A {
      @JSONField(deserialize=false)
      public Date date;
 }

#5. Use ordinal to specify the order of fields

The default fastjson serializes a java bean, It is serialized according to the alphabetical order of fieldName. You can specify the order of fields through ordinal. This feature requires version 1.1.42 or above.

public static class VO {
   @JSONField(ordinal = 3)
   private int f0;

   @JSONField(ordinal = 2)
   private int f1;

   @JSONField(ordinal = 1)
   private int f2;
}
6. 使用serializeUsing制定属性的序列化类

After fastjson version 1.2.16, JSONField supports the new customized configuration serializeUsing, which can individually customize the serialization of a certain attribute of a class, such as:

public static class Model {
    @JSONField(serializeUsing = ModelValueSerializer.class)
    public int value;
}

public static class ModelValueSerializer implements ObjectSerializer {
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        Integer value = (Integer) object;
        String text = value + "元";
        serializer.write(text);
    }
}

Test code

Model model = new Model();
model.value = 100;
String json = JSON.toJSONString(model);
Assert.assertEquals("{\"value\":\"100元\"}", json);

7. JSONField alternateNames

Fastjson provides a feature borrowed from gson in version 1.2.21, which supports the use of multiple different field names during deserialization. The method used is Configure the alternateNames of JSONField.

Demo

public static class Model {
    public int id;

    @JSONField(alternateNames = {"user", "person"})
    public String name;
}

String jsonVal0 = "{\"id\":5001,\"name\":\"Jobs\"}";
String jsonVal1 = "{\"id\":5382,\"user\":\"Mary\"}";
String jsonVal2 = "{\"id\":2341,\"person\":\"Bob\"}";

Model obj0 = JSON.parseObject(jsonVal0, Model.class);
assertEquals(5001, obj0.id);
assertEquals("Jobs", obj0.name);

Model obj1 = JSON.parseObject(jsonVal1, Model.class);
assertEquals(5382, obj1.id);
assertEquals("Mary", obj1.name);

Model obj2 = JSON.parseObject(jsonVal2, Model.class);
assertEquals(2341, obj2.id);
assertEquals("Bob", obj2.name);

8, JSONField jsonDirect

In fastjson-1.2.12 version, JSONField supports A new configuration item jsonDirect, its purpose is: when you have a field of string type, which contains json format data, you want to input it directly instead of outputting it after escaping.

Model

import com.alibaba.fastjson.annotation.JSONField;

public static class Model {
    public int id;
    @JSONField(jsonDirect=true)
    public String value;
}

Model model = new Model();
model.id = 1001;
model.value = "{}";

String json = JSON.toJSONString(model);
Assert.assertEquals("{\"id\":1001,\"value\":{}}", json);