fastjson反序列化机制
fastjson提供了智能匹配的规则,下面写法会自动映射
op_id->opid->ipId
也就是说就算json字符串是’op_id’,那java变量也可以用opid或者opId,然后也可以获取相应的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Runme { static int ONE_DAY_SECONDS = 24 * 60 * 60 * 1000; public static void main(String[] args) { String json = "{\"op-id\":1000}"; Mo mo = JSON.parseObject(json, Mo.class); System.out.println(mo.getOpId()); } public static class Mo { private String opId; public String getOpId() { return opId; } public void setOpId(String opId) { this.opId = opId; } } }
|
原理分析
那fastjson是怎么做到的呢?
看了下源代码
https://github.com/alibaba/fastjson
发现它的逻辑如下:
文件:src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java
方法: smartMatch(String key, int[] setFlags)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public FieldDeserializer smartMatch(String key, int[] setFlags) { if (key == null) { return null; } FieldDeserializer fieldDeserializer = getFieldDeserializer(key, setFlags); if (fieldDeserializer == null) { long smartKeyHash = TypeUtils.fnv1a_64_lower(key); if (this.smartMatchHashArray == null) { long[] hashArray = new long[sortedFieldDeserializers.length]; for (int i = 0; i < sortedFieldDeserializers.length; i++) { hashArray[i] = TypeUtils.fnv1a_64_lower(sortedFieldDeserializers[i].fieldInfo.name); } Arrays.sort(hashArray); this.smartMatchHashArray = hashArray; } int pos = Arrays.binarySearch(smartMatchHashArray, smartKeyHash); boolean is = false; if (pos < 0 && (is = key.startsWith("is"))) { smartKeyHash = TypeUtils.fnv1a_64_lower(key.substring(2)); pos = Arrays.binarySearch(smartMatchHashArray, smartKeyHash); } if (pos >= 0) { if (smartMatchHashArrayMapping == null) { short[] mapping = new short[smartMatchHashArray.length]; Arrays.fill(mapping, (short) -1); for (int i = 0; i < sortedFieldDeserializers.length; i++) { int p = Arrays.binarySearch(smartMatchHashArray, TypeUtils.fnv1a_64_lower(sortedFieldDeserializers[i].fieldInfo.name)); if (p >= 0) { mapping[p] = (short) i; } } smartMatchHashArrayMapping = mapping; } int deserIndex = smartMatchHashArrayMapping[pos]; if (deserIndex != -1) { if (!isSetFlag(deserIndex, setFlags)) { fieldDeserializer = 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.class && fieldClass != Boolean.class)) { fieldDeserializer = null; } } } return fieldDeserializer; }
|
它里面有个重要的地方就是调用TypeUtils.fnv1a_64_lower(String)方法,分别计算传入的key(op_id)和定义的java成员变量opId,然后计算他们是否匹配。
如果匹配的话,就会覆盖。
所以,关键就在于TypeUtils.fnv1a_64_lower(String)方法实现,如下:
这个方法就是如果是’_’或者’-‘,那么就忽略,也就是如果是op_id,那么就会变成opid。
如果是大写,那么就转换成小写,也就是opId,就会变成opid。
所以op-id或者op_id,都可以匹配opId或者opid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static long fnv1a_64_lower(String key){ long hashCode = 0xcbf29ce484222325L; for(int i = 0; i < key.length(); ++i){ char ch = key.charAt(i); if(ch == '_' || ch == '-'){ continue; } if(ch >= 'A' && ch <= 'Z'){ ch = (char) (ch + 32); } hashCode ^= ch; hashCode *= 0x100000001b3L; } return hashCode; }
|
去除json中不规范的引号和空格
1 2 3 4
| s = StringEscapeUtils.unescapeJson(s); \\反转义 jsonarray.getObject(0, String.class); \\取json数组的第一个元素,并且会反转义字符串 s = s.replaceAll("\"[\\s]+", "\""); \\ 去除引号后的多余空白字符 s = s.replaceAll("[\\s]+\"", "\""); \\ 去除引号前的多余空白字符
|