Fastjson usage and its related solutions __js

Source: Internet
Author: User
Tags serialization stream api maven central
1. How to get Fastjson?

You can download the Fastjson:maven central warehouse through the following places: http://central.maven.org/maven2/com/alibaba/fastjson/

sourceforge.net:https://sourceforge.net/projects/fastjson/files/

How to configure Fastjson dependencies in Maven Fastjson the latest version will be posted to the MAVEN central repository, which you can rely on directly. Com.alibaba Fastjson x.x.x x.x.x is not a specific version of the good, the latest version of you through the Fastjson of the homepage (https://github.com/alibaba/fastjson/wiki) obtained. 2. Fastjson the main API which.

The Fastjson entry class is Com.alibaba.fastjson.JSON, and the main API is json.tojsonstring, and parseobject.

  Package Com.alibaba.fastjson;
  Public abstract class JSON {public
        static final String tojsonstring (object);
        public static final <T> T parseobject (String text, class<t> clazz, Feature ... features);
  

Serialization:

  String jsonstring = json.tojsonstring (obj);

Deserialization:

  Vo vo = Json.parseobject ("...", vo.class);

Generic deserialization:

  Import com.alibaba.fastjson.TypeReference;

  list<vo> list = Json.parseobject ("...", new Typereference<list<vo>> () {});
3. Where to find the use of Fastjson example

Fastjson use examples to see here: Https://github.com/alibaba/fastjson/wiki/Samples-DataBind 4 Fastjson performance.

Fastjson is currently the fastest JSON library in the Java language, faster than claiming to be the fastest Jackson, and a third-party independent test results look here: https://github.com/eishay/jvm-serializers/wiki/Staging-Results.

Turn off circular reference detection when doing performance testing on your own.

  Json.tojsonstring (obj, serializerfeature.disablecircularreferencedetect)
  vo vo = Json.parseobject ("..."), Vo.class, Feature.disablecircularreferencedetect)

There's Jackson author Cowtowncoder and others. Fastjson Performance Evaluation: https://groups.google.com/forum/#!topic/ JAVA-SERIALIZATION-BENCHMARKING/8ES1KOQUAHW 5 Fastjson performance than Gson.

Fastjson is about 6 times times faster than Gson, and the test results here: https://github.com/eishay/jvm-serializers/wiki/Staging-Results. Gson's g may be the abbreviation of "turtle" Pinyin, the JSON library of turtle speed. 6. Fastjson can run on Android.

Fastjson has a dedicated for Android version that removes features that are not commonly used. The jar occupies a smaller number of bytes. Git branch address is: https://github.com/alibaba/fastjson/tree/android. 7. Fastjson serialization requires the serialization of Java beans to be configured like Json-lib.

No, Fastjson serialization and deserialization do not require a special configuration, the only requirement is that your serialized class conforms to the Java Bean specification. 8. Fastjson How to deal with the date

The API for Fastjson processing dates is simple, for example:

  Json.tojsonstringwithdateformat (date, "Yyyy-mm-dd HH:mm:ss.") SSS ")

Use ISO-8601 date format

  Json.tojsonstring (obj, Serializerfeature.useiso8601dateformat);

Global Modify date format

  Json. Deffault_date_format = "Yyyy-mm-dd";
  Json.tojsonstring (obj, Serializerfeature.writedateusedateformat);

Deserialization can automatically identify the following date formats: ISO-8601 date format yyyy-mm-dd yyyy-mm-dd HH:mm:ss yyyy-mm-dd HH:mm:ss. SSS millisecond numeric millisecond numeric string. NET JSON date format new Date (198293238) 9. How to customize Serialization.

You can use the Simpleprepropertyfilter filter field, look at this in detail: https://github.com/alibaba/fastjson/wiki/%E4%BD%BF%E7%94% A8simplepropertyprefilter%e8%bf%87%e6%bb%a4%e5%b1%9e%e6%80%a7

About customizing serialization, a detailed introduction to see here: https://github.com/alibaba/fastjson/wiki/%E5%AE%9A%E5%88%B6%E5%BA%8F%E5%88%97%E5%8C%96 10. When the object has a reference, the serialized results browser does not support, what to do.

Use the Serializerfeature.disablecircularreferencedetect attribute to turn off reference detection and generation. For example:

  String  jsonstring = json.tojsonstring (obj, 
          serializerfeature.disablecircularreferencedetect
          );
IE 6 does not support JSON with Chinese strings, what to do with.

Fastjson provides browsercompatible this configuration, after opening, all Chinese will be serialized into \UXXXX This format, the number of bytes will be more, but can be compatible with IE 6.

  String  jsonstring = json.tojsonstring (obj, 
          serializerfeature.browsercompatible
          );
how Fastjson handles oversized objects and oversized JSON text

Fastjson provides the Stream API, which is detailed here Https://github.com/alibaba/fastjson/wiki/Stream-api 13. Customizing serialization with @jsonfield

Fastjson provides the ability to customize serialization and deserialization using annotation. Https://github.com/alibaba/fastjson/wiki/JSONField


Instance code:

Encode

Import Com.alibaba.fastjson.JSON;

Group Group = new Group ();
Group.setid (0L);
Group.setname ("admin");

User Guestuser = new user ();
Guestuser.setid (2L);
Guestuser.setname ("Guest");

User Rootuser = new user ();
Rootuser.setid (3L);
Rootuser.setname ("root");

Group.getusers (). Add (guestuser);
Group.getusers (). Add (rootuser);

String jsonstring = json.tojsonstring (group);

System.out.println (jsonstring);
Output
{"id": 0, "name": "Admin", "users": [{"id": 2, "name": "Guest"},{"id": 3, "name": "Root"}]}
Decode
String jsonstring = ...;
Group Group = Json.parseobject (jsonstring, Group.class);
Group.java
public class Group {

    private Long       ID;
    Private String     name;
    Private list<user> users = new arraylist<user> ();

    Public Long GetId () {return
        ID;
    }

    public void SetId (Long id) {
        this.id = ID;
    }

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    Public list<user> Getusers () {return
        users;
    }

    public void Setusers (list<user> users) {
        this.users = users;
    }
}
User.java
public class User {

    private Long   ID;
    private String name;

    Public Long GetId () {return
        ID;
    }

    public void SetId (Long id) {
        this.id = ID;
    }

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }
}
This is the Fastjson instance code, if you still don't know how to use it. Well, look at my next article. Parse local text with Fastjson http://blog.csdn.net/kluing/article/details/41007675

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.