• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于java:FastJSON-对象转JSON字符串自动转换首字母小写

java 搞代码 4年前 (2022-01-27) 46次浏览 已收录 0个评论
文章目录[隐藏]

欢送来我的博客逛逛 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=”字段名称”) 办法和字段一样会也是优先


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于java:FastJSON-对象转JSON字符串自动转换首字母小写
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址