Fastjson the long type to JSON precision loss problem resolution

Source: Internet
Author: User
Tags dateformat object object

This isJackson

public void Extendmessageconverters (list

for (httpmessageconverter<?> c:converters) {

if (c instanceof mappingjackson2httpmessageconverter) {

Mappingjackson2httpmessageconverter Mappingjackson2httpmessageconverter = (mappingjackson2httpmessageconverter) C;

Objectmapper objectmapper = Mappingjackson2httpmessageconverter.getobjectmapper ();

Objectmapper.configure (Serializationfeature.write_dates_as_timestamps, false);

if (This.dateformat! = null) {

DateFormat Mydateformat = new SimpleDateFormat (DateFormat);

Objectmapper.setdateformat (Mydateformat);

}

Registering a long dedicated JSON converter

Simplemodule module = new Simplemodule ();

Fixed long, missing precision problem

Module.addserializer (Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);

Module.addserializer (Long.type, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);

Module.addserializer (Bigdecimal.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);

Module.addserializer (Biginteger.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);

Objectmapper.registermodule (module);

}

}

}

Change to Fastjson below.

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<!--JSON Library unified use Fastjson--

<exclusion>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

</exclusion>

</exclusions>

</dependency>


Generates a serialized transformation class Tostringserializer

public class Tostringserializer implements Objectserializer {

public static final Tostringserializer instance = new Tostringserializer ();

@Override

public void Write (Jsonserializer Serializer, Object object, Object FieldName, Type FieldType,

int features) throws IOException {

Serializewriter out = serializer.out;

if (object = = null) {

Out.writenull ();

Return

}

String strval = object.tostring ();

Out.writestring (Strval);

}

}


Web configuration for Tostringserializer

@Bean

Public Httpmessageconverters getconverters () {

Fastjsonhttpmessageconverter converter = new Fastjsonhttpmessageconverter ();

Fastjsonconfig config = new Fastjsonconfig ();

Config.setserializerfeatures (

Serializerfeature.writemapnullvalue,

Serializerfeature.quotefieldnames,

Serializerfeature.ignorenonfieldgetter

);

Serializeconfig serializeconfig = new Customserializeconfig ();

Serializeconfig.put (Biginteger.class, tostringserializer.instance);

Serializeconfig.put (Long.class, tostringserializer.instance);

Serializeconfig.put (Long.type, tostringserializer.instance);

Config.setserializeconfig (Serializeconfig);

Converter.setfastjsonconfig (config);

list<mediatype> mediatypes = Arrays.aslist (

Mediatype.application_json_utf8,

Mediatype.text_plain,

Mediatype.text_html,

Mediatype.text_xml,

Mediatype.application_octet_stream);

Converter.setsupportedmediatypes (mediatypes);

return new Httpmessageconverters (converter);

}

As shown above, registered serialization type, tested, function OK!


@Override

public void Configuremessageconverters (list

Initializing the converter

Fastjsonhttpmessageconverter Fastconvert = new Fastjsonhttpmessageconverter ();

Initializing a converter configuration

Fastjsonconfig fastjsonconfig = new Fastjsonconfig ();

Json. Deffault_date_format = "Yyyy-mm-dd hh:mm";

Fastjsonconfig.setserializerfeatures (Serializerfeature.browsercompatible,

Serializerfeature.writenulllistasempty,

Serializerfeature.prettyformat,

Serializerfeature.writedateusedateformat,

Serializerfeature.writenullstringasempty,

Serializerfeature.writemapnullvalue,

Serializerfeature.disablecircularreferencedetect);

Resolving the problem of long-to-JSON precision loss

Serializeconfig serializeconfig = serializeconfig.globalinstance;

Serializeconfig.put (Biginteger.class, tostringserializer.instance);

Serializeconfig.put (Long.class, tostringserializer.instance);

Serializeconfig.put (Long.type, tostringserializer.instance);

Fastjsonconfig.setserializeconfig (Serializeconfig);

To set the configuration to the converter and add it to the Httpmessageconverter converter list

Fastconvert.setfastjsonconfig (Fastjsonconfig);

Converters.add (Fastconvert);

}


Package com.chz.component.basic.configure;


Import Com.alibaba.fastjson.serializer.SerializeConfig;

Import Com.alibaba.fastjson.serializer.ToStringSerializer;

Import Com.alibaba.fastjson.support.config.FastJsonConfig;


Import Java.math.BigInteger;


public class Fastjsonconfigext extends Fastjsonconfig {


Public Fastjsonconfigext () {

Super ();

Serializeconfig serializeconfig = serializeconfig.globalinstance;

Serializeconfig.put (Biginteger.class, tostringserializer.instance);

Serializeconfig.put (long.class,tostringserializer.instance);

Serializeconfig.put (long.type,tostringserializer.instance);

This.setserializeconfig (Serializeconfig);

}


}




@Configuration

public class Webconfig extends Webmvcconfigureradapter {


@Override

public void Configuremessageconverters (list

Fastjsonhttpmessageconverter Fastconverter =

New Fastjsonhttpmessageconverter ();


Fastjsonconfig fastjsonconfig = new Fastjsonconfig ();

Serializeconfig serializeconfig = serializeconfig.globalinstance;

Serializeconfig.put (Biginteger.class, tostringserializer.instance);

Serializeconfig.put (Long.class, tostringserializer.instance);

Serializeconfig.put (Long.type, tostringserializer.instance);

Fastjsonconfig.setserializeconfig (Serializeconfig);

Fastconverter.setfastjsonconfig (Fastjsonconfig);

Converters.add (Fastconverter);

}

}


Fastjson the long type to JSON precision loss problem resolution

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.