Fastjson initial lowercase problem solving

Time:2021-9-16

Welcome to my blogerror0 Blog
The code is copied directly. The format is messy. Let’s make do and have a look

If you want to keep the fields as they are, fastjson defaults to getting the fields of the get method and converting the initial letter to lowercase.

Solution:

1. Add instantiation configuration

Json.tojsonstring (the object to be instantiated, new serializeconfig (true)) // set the true field as the JSON key, and the default is false

Principle:

Note: the code is omitted in line 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 // set fieldbased through the serializeconfig method. This field affects how to get the field
                ? computeGettersWithFieldBase(beanType, aliasMap, false, propertyNamingStrategy) 
                : computeGetters(beanType, jsonType, aliasMap, fieldCacheMap, false, propertyNamingStrategy);
      
      }
    
}

The computegeterswithfieldbase method is roughly like this 
Field[] fields = currentClass.getDeclaredFields();
computeFields(currentClass, aliasMap, propertyNamingStrategy, fieldInfoMap, fields);  // If there is annotation, get the annotation field first

2. Field plus @ jsonfield (name = “field name”)

Principle:

The computegeters method uses the get method as the key of JSON in the past
Note: the code is omitted in line 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));// Remove 'get' and turn to lowercase
     
     
 }

Principle:

But plus@JSONFieldThe annotation will be used preferentially. The annotation field is key
Note: roughly in line 2066, the method is the same as above

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(); // Gets the name attribute of the annotation
                            if(aliasMap != null){
                                propertyName = aliasMap.get(propertyName);
                                if(propertyName == null){
                                    continue;
                                }
                            }
                        }

3. The get method plus the @ jsonfield (name = “field name”) method will take precedence as well as the field