Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
In the previous two articles, we introduced the JSON data format and the system comes with JSON and Google's Gson project, if you can learn these things, basically can meet the needs of the job. However, programmers have the appetite for extreme efficiency, after satisfying the basic needs, we will consider can we optimize the efficiency? Of course! Today, this article is to introduce, is in the data volume is larger than the gson of the analytic efficiency of nearly 10 times times the JSON data parsing framework--jackson!
Here is a great God test results on the parsing speed of several common JSON data, please poke http://wangym.iteye.com/blog/738933
We can see that in the case of big data, Jackson's speed is faster than the resolution of Gson is not 1:30, so the most recommended to use is the Jackson frame, come, let's see how to use!
Let's start by introducing the functions of several commonly used classes:
Jsonfactory: This class is the main factory method for the Jackson project, mainly used to configure and build parsers (such as jsonparser) and generators (such as Jsongenerator), which are thread-safe and can be reused if configured.
Jsongenerator: This class is primarily used to generate JSON-formatted content, and we can use the Jsonfactory method to generate an instance.
Jsonparser: This is primarily used to read JSON-formatted content, and to complete the parsing operation, we can use the Jsonfactory method to generate an instance.
Objectmapper: This class provides conversions between Java objects and JSON, primarily through jsonparser and Jsongenerator instances to perform actual read and write operations on JSON data. This class is a subclass of Objectcodec, and the main implementation details are in Objectcodec. And this class can be reused, so it is common to create a singleton pattern for this class, such as the following code
Package Com.example.jsondemo;import org.codehaus.jackson.map.objectmapper;/** * * @ClassName: Com.example.jsondemo.JacksonMapper * @Description: Objectmapper Single-case mode * @author Zhaokaiqiang * @date 2014-11-27 4:06:52 * */public class Jacksonmapper {private static final objectmapper mapper = new Objectmapper ();p rivate jacksonmapper () {}public static Objectmapper getinstance () {return mapper;}}
We are in the following code, we often use this singleton mode, we do not confuse.
1. JSON data generation for simple objects
The Simple object mentioned here refers to a single object or a collection class of objects.
Let's look at the code implementation first and then explain some details.
There are two methods of implementation, first look at the following:
Public String getjsonstring (Object object) throws Exception {return jacksonmapper.getinstance (). Writevalueasstring ( object);}
In This approach, we use the Objectmapper class's writevalueasstring () directly to return the object's JSON format as the return value.
In addition, we can also use the second method, the code is as follows
Public String getJsonString1 (Object object) throws Exception {Objectmapper mapper = jacksonmapper.getinstance (); StringWriter SW = new StringWriter (); Jsongenerator Gen = new Jsonfactory (). Createjsongenerator (SW); Mapper.writevalue (Gen, object); Gen.flush (); Gen.close ( ); return sw.tostring ();}
This method uses the Jsongenerator and StringWriter objects, through Objectmapper, writes the converted JSON data into the StringWriter, and then ToString () We can get the JSON data we need.
Here again, from the second way, we can see that the operation is very much like the processing of IO stream inside Java, in fact, Jackson really is streaming data processing, which is the reason for its processing speed so fast.
Because the first method is more concise, we recommend that you use it.
Here are the test code and test results
/** * Collection Object * * @throws Exception */public void Objectstojson () throws Exception {person p = new person ("Zhaokaiqiang", New Birthday (1992, 1, 19)); arraylist<person> persons = new arraylist<person> ();p Ersons.add (p);p Ersons.add (p);p Ersons.add (p); LOG.D (TAG, getjsonstring (Persons));} /** * Single Object * * @throws Exception */public void Objecttojson () throws Exception {person p = new person ("Zhaokaiqiang", New Birthday (1992, 1, 19)); LOG.D (TAG, Getjsonstring (P));}
The test results are as follows
{"Name": "Zhaokaiqiang", "Birthday": {"Day": +, "month": 1, "Year": 1992}, "Age": 22}
[{"Name": "Zhaokaiqiang", "Birthday": {"Day": +, "month": 1, "Year": 1992}, "Age": 22},{"name": "Zhaokaiqiang", "Birthday ": {" Day ": +," month ": 1," Year ": 1992}," Age ": 22},{" name ":" Zhaokaiqiang "," Birthday ": {" Day ": +," month ": 1," Year ": 1992 }, "Age": 22}]
2. Generation of JSON data for complex objects
The complex object here refers to data that consists of data of many different data types, and we'll show you how to create JSON data for this complex object.
As we have described earlier, the JSON format is divided into object and array forms, and we will describe how the two formats are generated separately.
(1) Object format
Let's look directly at the code below!
/** * Generates JSON in object form * * @throws Exception */public void Createobjectjson () throws Exception {person p = new person ("en Aokaiqiang ", New Birthday (1992, 1, 19)); StringWriter StringWriter = new StringWriter ();//You must obtain jsongenerator Jsongenerator = Jacksonmapper.getinstance () in this way. Getjsonfactory (). Createjsongenerator (StringWriter); Jsongenerator.writestartobject (); Jsongenerator.writestringfield ("name", "Zhaokaiqiang"); Jsongenerator.writenumberfield ("Age", 22); Jsongenerator.writeobjectfield ("Person", p); Jsongenerator.writeendobject (); Jsongenerator.flush (); Jsongenerator.close (); LOG.D (TAG, stringwriter.tostring ());}
Jsongenerator is a generator, and if we want to create a complex JSON object, we can do it with this class. But one thing to be aware of is the way to get jsongenerator, we have to use Objectmapper's Getjsonfactory () and then create a jsongenerator, Otherwise, if we use the Writeobjectfield () method, we will report the following exception
Java.lang.IllegalStateException:No Objectcodec defined for the generator, can only serialize simple wrapper types (type P assed Com.example.jsondemo.Person)
The rest of the code above is simple, as long as the corresponding Write___field () can be called according to the different data types.
Here are the test results
{"Name": "Zhaokaiqiang", "Age": $, "person": {"name": "Zhaokaiqiang", "Birthday": {"Day": +, "month": 1, "Year": 1992}, " Age ": 22}}
(2) array format
Here we describe how to generate JSON data in array format, which is similar to the above.
Here is the code implementation:
/** * Create json in array form * * @throws Exception */public void Createarrayjson () throws Exception {person p = new person ("en Aokaiqiang ", New Birthday (1992, 1, 19)); StringWriter StringWriter = new StringWriter ();//Only this way can get jsongenerator Jsongenerator = Jacksonmapper.getinstance (). Getjsonfactory (). Createjsongenerator (StringWriter); Jsongenerator.writestartarray (); Jsongenerator.writestring (" Jsongenerator.writenumber (Zhaokaiqiang); Jsongenerator.writeobject (P); Jsongenerator.writeendarray (); Jsongenerator.flush (); Jsongenerator.close (); LOG.D (TAG, stringwriter.tostring ());}
The idea is exactly the same, the following is the result of the test:
["Zhaokaiqiang", 22,{"name": "Zhaokaiqiang", "Birthday": {"Day": +, "month": 1, "Year": 1992}, "Age": 22}]
3. Parsing simple JSON objects
Here's how to parse the JSON data into a Java object.
We look directly at the parsing code
public void Toobject () throws Exception {Objectmapper objectmapper = new Objectmapper (); Person p = new person ("Zhaokaiqiang", New Birthday (1992, 1, 19)); String jsonstring = getjsonstring (p); Person person = Objectmapper.readvalue (jsonstring, Person.class); LOG.D (TAG, person.tostring ());}
When parsing, we still need to use the Objectmapper object, call ReadValue, and then pass in the JSON data and the class of the transformation type that need to be parsed, we can complete the conversion.
The following is the code that parses a simple collection object
public void Toobjects () throws Exception {person p = new person ("Zhaokaiqiang", New Birthday (1992, 1, 19)); arraylist<person> persons = new arraylist<person> ();p Ersons.add (p);p Ersons.add (p);p Ersons.add (p); O Bjectmapper objectmapper = new Objectmapper (); String jsonstring = getjsonstring (Persons), @SuppressWarnings ("unchecked") arraylist<person> ArrayList = Objectmapper.readvalue (Jsonstring,new arraylist<person> (). GetClass ()); LOG.D (TAG, arraylist.tostring ());}
4. Parsing complex JSON objects
I'm looking into it.
"Android Development experience" is 10 times times faster than Gson parsing!—— JSON parsing artifact Jackson usage Introduction