Converting a long type to a string type by Fastjson

Source: Internet
Author: User

The back end transmits long data to the front end, which can cause loss of precision. For example:201511200001725439 Such a long integer, passed to the front end will become 201511200001725440.

Workaround:

Method One: In the background to convert the long field to string type, the risk is relatively large.

Method Two: Use the provided annotations of Fastjson,@JSONField (serializeusing= tostringserializer.class).

Note:

Fastjson provides a variety of annotation for data type conversions under the Com.alibaba.fastjson.serializer package.

You can also expand these annotations by implementing the Objectserializer interface.

  Tostringserializer 's code:

 Public classTostringserializerImplementsObjectserializer { Public Static FinalTostringserializer instance =NewTostringserializer (); @Override Public voidWrite (Jsonserializer Serializer, Object object, Object FieldName, Type FieldType,intFeaturesthrowsIOException {serializewriter out=serializer.out; if(Object = =NULL) {out.writenull (); return; } String strval=object.tostring ();    Out.writestring (Strval); }}

Converting a long type to a string type by Fastjson

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.