Fastjson the first letter to lowercase when the bean is turned into a string

Source: Internet
Author: User
Tags naming convention

A project requirement requires the return value to be in JSON format, and most of the fields are capitalized, and some are similar to the N_TX format, in the output of such a result encountered a problem, due to the time is tight, directly copy the desired result field to establish the JavaBean class, It was thought that the final call to Json.tostring (obj) would return the result, and it was not expected that the first letter would be automatically lowercase in the return value. View Fastjson source Discovery key in the following paragraph

public static list<fieldinfo> Computegetters (class<?> clazz, map<string, String> AliasMap, Boolean Sorted) {String propertyname; if (Character.isuppercase (C3)) { if (compatiblewithjavabean) { propertyname = Introspector.decapitalize (methodname.substring (3)); } else { propertyname = Character.tolowercase (Methodname.charat (3)) + methodname.substring (4); }} else if (C3 = = ' _ ') {propertyname = Methodname.substring (4);} else if (C3 = = ' F ') {propertyname = Methodname.substrin g (3); } else {continue;}} In Compatiblewithjavabean is false when the direct get property has the first letter lowercase, consider Compatiblewithjavabean initialized to True to discover Public static string decapitalize (string name) { if (name = = Null | | name.length () = = 0) { return name;    } If (Name.length () > 1 && Character. Isuppercase(Name.charat (1)) &&Character. Isuppercase (Name.charat (0))) { return name;    } char chars[] = Name.tochararray ();Chars[0] = Character. toLowerCase (Chars[0]); return new String (chars);    }The first-letter lowercase operation is only allowed in consecutive uppercase letters. Therefore, the method also does not meet the requirements.

Finally, find the following workaround:

1. Change the properties of the bean object directly to public, and the property name to be capitalized, for example {"name": "Nomouse", "Age": 12}, the corresponding Bean is: familiar as public, do not need to define the Get method

public class User {

public String Name;

public int age;

}



2, the first method does not conform to the Java naming convention, you can use the second method to add annotations on attributes:

public class User {

@JSONField (value = "Name")

private String name;

@JSONField (Value = "Age")

private int age;

}

3, their own use of reflection to write Object2json and List2json method, as long as the simple JavaBean class can be processed.


At first, I want to see if the Fastjson serializerfeature serialization property is available, and finally it doesn't fit:

Disablecheckspecialchar: If there is a special word such as double quotation mark in the string property of an object, it will have a backslash transfer character when it is turned into JSON. If you do not need to escape, you can use this property. The default is false quotefieldnames whether double quotation marks are used when the key is ———-output, and the default is true writemapnullvalue--– whether to output a field with a null value, false by default writenullnumberaszero--numeric field if NULL, output is 0, not null writenulllistasempty-–list field if NULL, the output is [], NOT NULL writenullstringasempty-character Type field if NULL, output is "" Instead of NULL Writenullbooleanasfalse–boolean field if NULL, the output is false, not NULL





From for notes (Wiz)

Fastjson The first letter to lowercase when the bean is converted to a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.