fastjson简单使用demo,@JSONField注解属性字段上与set、get方法上。实体类toString(),实体类转json的区别;_下划线-减号大小写智能匹配

一、demo代码

@JSONField注解属性字段上与set、get方法上。使用@Data注解(lombok插件安装最下方),对属性“笔名”【pseudonym】手动重写setter/getter方法

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

@Data
public class Stu {

    private String name;
    //下划线自动转换
    private String alias_name;

    //下划线自动转换
//    @JSONField(name="pen_name")
    private String penName;

    //实体类转为json,使用注解name值
    @JSONField(name="pen_name2")
    private String penName2;


    private String pseudonym;

    //实体类转为json,使用注解name值
    @JSONField(name="pseud")
    //不使用注解为属性名{"penName":"333","name":"1111","alias_name":"222","pen_name2":"444","pseudonym":"555"}
    public String getPseudonym() {
        return pseudonym;
    }

    //json转为实体类,使用注解name值
    @JSONField(name="pseudo")
    public void setPseudonym(String pseudonym) {
        this.pseudonym = pseudonym;
    }

    public static void main(String[] args) {
        String json = "{'name':'1111','aliasName':'222','pen_name':'333','pen_name2':'444','pseudo':'555'}";
        Stu stu = JSON.parseObject(json, Stu.class);

        System.out.println(stu);
        System.out.println(JSONObject.toJSON(stu));
    }
}

 

二、main方法运行结果(控制台)

 

Stu(name=1111, alias_name=222, penName=333, penName2=444, pseudonym=555)
{"pseud":"555","penName":"333","name":"1111","alias_name":"222","pen_name2":"444"}

Process finished with exit code 0

 

三、@Data备注

idea安装lombok插件。

 

四、源码

类:

JavaBeanDeserializer implements ObjectDeserializer

public FieldDeserializer smartMatch(String key, int[] setFlags) {
        if (key == null) {
            return null;
        } else {
            FieldDeserializer fieldDeserializer = this.getFieldDeserializer(key, setFlags);
            if (fieldDeserializer == null) {
                long smartKeyHash = TypeUtils.fnv1a_64_lower(key);
                if (this.smartMatchHashArray == null) {
                    long[] hashArray = new long[this.sortedFieldDeserializers.length];

                    for(int i = 0; i < this.sortedFieldDeserializers.length; ++i) {
                        hashArray[i] = TypeUtils.fnv1a_64_lower(this.sortedFieldDeserializers[i].fieldInfo.name);
                    }

                    Arrays.sort(hashArray);
                    this.smartMatchHashArray = hashArray;
                }

                int pos = Arrays.binarySearch(this.smartMatchHashArray, smartKeyHash);
                boolean is = false;
                if (pos < 0 && (is = key.startsWith("is"))) {
                    smartKeyHash = TypeUtils.fnv1a_64_lower(key.substring(2));
                    pos = Arrays.binarySearch(this.smartMatchHashArray, smartKeyHash);
                }

                if (pos >= 0) {
                    if (this.smartMatchHashArrayMapping == null) {
                        short[] mapping = new short[this.smartMatchHashArray.length];
                        Arrays.fill(mapping, (short)-1);

                        for(int i = 0; i < this.sortedFieldDeserializers.length; ++i) {
                            int p = Arrays.binarySearch(this.smartMatchHashArray, TypeUtils.fnv1a_64_lower(this.sortedFieldDeserializers[i].fieldInfo.name));
                            if (p >= 0) {
                                mapping[p] = (short)i;
                            }
                        }

                        this.smartMatchHashArrayMapping = mapping;
                    }

                    int deserIndex = this.smartMatchHashArrayMapping[pos];
                    if (deserIndex != -1 && !isSetFlag(deserIndex, setFlags)) {
                        fieldDeserializer = this.sortedFieldDeserializers[deserIndex];
                    }
                }

                if (fieldDeserializer != null) {
                    FieldInfo fieldInfo = fieldDeserializer.fieldInfo;
                    if ((fieldInfo.parserFeatures & Feature.DisableFieldSmartMatch.mask) != 0) {
                        return null;
                    }

                    Class fieldClass = fieldInfo.fieldClass;
                    if (is && fieldClass != Boolean.TYPE && fieldClass != Boolean.class) {
                        fieldDeserializer = null;
                    }
                }
            }

            return fieldDeserializer;
        }
    }

 

“_”下划线及“-”短横线(减号),大小写都可以智能匹配。

类:

TypeUtils
 public static long fnv1a_64_lower(String key) {
        long hashCode = -3750763034362895579L;

        for(int i = 0; i < key.length(); ++i) {
            char ch = key.charAt(i);
            if (ch != '_' && ch != '-') {
                if (ch >= 'A' && ch <= 'Z') {
                    ch = (char)(ch + 32);
                }

                hashCode ^= (long)ch;
                hashCode *= 1099511628211L;
            }
        }

        return hashCode;
    }

 五、JackSon

@JsonProperty
这个注解提供了序列化和反序列化过程中该java属性所对应的名称。
Access.WRITE_ONLY:只在序列化时使用修改后的字段
Access.READ_ONLY:只在反序列化时使用,类似于@JsonAlias注解
Access.READ_WRITE:在序列化和反序列化都使用修改后字段
Access.AUTO:自动确定

@JsonAlias
这个注解是在JackSon 2.9版本中才有的。
作用是在反序列化的时候可以让Bean的属性接收多个json字段的名称。可以加在字段上或者getter和setter方法上

posted @ 2019-10-27 16:02  毁乐乖狂,自有诪张  阅读(2349)  评论(1编辑  收藏  举报