likes
comments
collection
share

FastJSON 首字母小写问题解决

作者站长头像
站长
· 阅读数 61
欢迎来我的博客逛逛 error0 Blog代码是直接复制过来,格式比较乱大家凑合看一下吧

需求要保留字段原样,可fastjson默认是获取get方法的字段并把首字母转成小写。

解决方案:

1、添加实例化配置

JSON.toJSONString(需要实例化的对象,new SerializeConfig(true)) //设置true通过字段作为json key,默认为flase

原理:

注:代码省略 大致在1821行

package com.alibaba.fastjson.util;
public class TypeUtils{
    
      public static SerializeBeanInfo buildBeanInfo(Class<?> beanType //
            , Map<String,String> aliasMap //
            , PropertyNamingStrategy propertyNamingStrategy //
            , boolean fieldBased //
    ){
                   List<FieldInfo> fieldInfoList = fieldBased   //通过SerializeConfig方法设置fieldBased这个字段影响了如何获取字段
                ? computeGettersWithFieldBase(beanType, aliasMap, false, propertyNamingStrategy) 
                : computeGetters(beanType, jsonType, aliasMap, fieldCacheMap, false, propertyNamingStrategy);
      
      }
    
}

computeGettersWithFieldBase 方法大概是这样 
Field[] fields = currentClass.getDeclaredFields();
computeFields(currentClass, aliasMap, propertyNamingStrategy, fieldInfoMap, fields);  //有注解优先获取注解字段

2、字段加上@JSONField(name="字段名称")

原理:

computeGetters方法是通过过去get方法作为json的key 注:代码省略 大致在2015行

 public static List<FieldInfo> computeGetters(Class<?> clazz, //
                                                 JSONType jsonType, //
                                                 Map<String,String> aliasMap, //
                                                 Map<String,Field> fieldCacheMap, //
                                                 boolean sorted, //
                                                 PropertyNamingStrategy propertyNamingStrategy //
    ){
      
         char c3 = methodName.charAt(3);
                String propertyName;
                Field field = null;
                if(Character.isUpperCase(c3) //
                        || c3 > 512 // for unicode method name
                        ){
                    if(compatibleWithJavaBean){
                        propertyName = decapitalize(methodName.substring(3));//去掉 ‘get’ 并转首字母小写
     
     
 }

原理:

但是加上@JSONField注解会优先使用注解字段为key 注:大致在2066行 同上方法

  JSONField fieldAnnotation = null;
                if(field != null){
                    fieldAnnotation = TypeUtils.getAnnotation(field, JSONField.class);
                    if(fieldAnnotation != null){
                        if(!fieldAnnotation.serialize()){
                            continue;
                        }
                        ordinal = fieldAnnotation.ordinal();
                        serialzeFeatures = SerializerFeature.of(fieldAnnotation.serialzeFeatures());
                        parserFeatures = Feature.of(fieldAnnotation.parseFeatures());
                        if(fieldAnnotation.name().length() != 0){
                            fieldAnnotationAndNameExists = true;
                            propertyName = fieldAnnotation.name(); //获取注解的name属性
                            if(aliasMap != null){
                                propertyName = aliasMap.get(propertyName);
                                if(propertyName == null){
                                    continue;
                                }
                            }
                        }

3、get方法加上@JSONField(name="字段名称") 方法和字段一样会也是优先

转载自:https://segmentfault.com/a/1190000040585981
评论
请登录