Jackson high-performance JSON processing ObjectMapper

I studied json by myself today, and it feels very easy to use. After testing, it is much faster than google's GSON

      At the same time, Jackson can easily convert Java objects into json objects and xml documents, and can also convert json and xml into Java objects. The function is very powerful!

       As we all know, json is very widely used in today's Internet era. Because everyone pays so much attention, the parsing performance requirements of json are also very high.

 

1. Preparation

1. Download the dependency library jar package

Jackson's jar all download address: http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar

Then import the jar package in the project to start working

Official example: http://wiki.fasterxml.com/JacksonInFiveMinutes

Because the following program is run with the junit test case, it is necessary to add the junit jar package. The version is junit-4.2.8

If you need to convert xml, then stax2-api.jar is also required

2. The basic code of the test class is as follows

 

[java] view plaincopy

  1. /* 
  2.  * @project java 
  3.  * @package  
  4.  * @file Jackson.java 
  5.  * @version  1.0 
  6.  *  @author    Liao Yiping 
  7.  * @time 2011-11-8 02:59:37 AM 
  8.  */  
  9.   
  10. public class Jackson {  
  11.     /* 
  12.      * 
  13.      * Class Descripton goes here. 
  14.      * 
  15.      * @class Jackson 
  16.      * @version  1.0 
  17.      * @author Liao Yiping 
  18.      * @time 2011-11-8 02:59:37 AM 
  19.      */  
  20.     public  static JsonGenerator jsonGenerator = null;  
  21.     private static ObjectMapper mapper = new ObjectMapper();  
  22.     public static void main(String[] args) {  
  23.         Student student = new Student();  
  24.         student.setIsstudent (true);  
  25.         student.setUid(1000);  
  26.         student.setUname("xiao liao");  
  27.         student.setUpwd("123");  
  28.         student.setNumber (12);  
  29.           
  30.         Map<String, Student> stuMap = new HashMap<String, Student>();  
  31.         stuMap.put("1", student);  
  32.         stuMap.put("2", student);  
  33.           
  34.         List<Object> stuList = new ArrayList<Object>();  
  35.         List<Student> stuList1 = new ArrayList<Student>();  
  36.         stuList1.add(student);  
  37.         student=  new  Student();  
  38.         student.setIsstudent (false);  
  39.         student.setUid(200);  
  40.         student.setUname("xiao mi");  
  41.         stuList1.add(student);  
  42.           
  43.         stuList.add(student);  
  44.         stuList.add("xiao xin");  
  45.         stuList.add("xiao er");  
  46.         stuList.add(stuMap);  
  47.           
  48.         //readJson2List();  
  49.         try {  
  50.             //readJson2Array();  
  51.             //writeArray2Json(array);  
  52.             //writeJson2List();  
  53.             //writeEntity2Json(student);  
  54.             writeJson2Entity();  
  55.             // writeMap2Json (stuMap);  
  56.             //writeList2Json(stuList1);  
  57.               
  58.         } catch (IOException e) {  
  59.             e.printStackTrace ();  
  60.         }  
  61.     }  
  62.      /** 
  63.       *  
  64.       * <code>writeEntity2Json</code> 
  65.       * @description: TODO (entity class converted to json)  
  66.       * @param object 
  67.       * @throws IOException 
  68.       * @since 2011-11-8 Liao Yiping 
  69.       */  
  70.      public static void writeEntity2Json(Object object) throws IOException {  
  71.            mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );  
  72.            mapper.writeValue( System.out,object );  
  73.            
  74.      }  
  75.      /** 
  76.       *  
  77.       * <code>writeArray2Json</code> 
  78.       * @description: TODO (array converted to json array)  
  79.       * @param object 
  80.       * @throws IOException 
  81.       * @since 2011-11-8 Liao Yiping 
  82.       */  
  83.      public static void writeArray2Json(Object object) throws IOException {  
  84.            
  85.          // writeValue has the same function as writeObject  
  86.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );  
  87.          mapper.writeValue(System.out,object );  
  88.            
  89.      }  
  90.      /** 
  91.       *  
  92.       * <code>writeMap2Json</code> 
  93.       * @description: TODO (map object converted to json object)  
  94.       * @param object 
  95.       * @throws IOException 
  96.       * @since 2011-11-8 Liao Yiping 
  97.       */  
  98.      public static void writeMap2Json(Object object) throws IOException {  
  99.            
  100.          System.out.println("使用ObjectMapper-----------");  
  101.          // writeValue has the same function as writeObject  
  102.          System.out.println("==>"+mapper.writeValueAsString(object));  
  103.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );  
  104.          mapper.writeValue( System.out , object );  
  105.      }  
  106.      /** 
  107.       *  
  108.       * <code>writeList2Json</code> 
  109.       * @description: TODO (list converted to json)  
  110.       * @param object 
  111.       * @throws IOException 
  112.       * @since 2011-11-8 Liao Yiping 
  113.       */  
  114.      public static void writeList2Json(Object object) throws IOException {  
  115.          System.out.println("==>"+mapper.writeValueAsString(object));  
  116.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );  
  117.          mapper.writeValue( System.out , object );  
  118.      }  
  119.      /** 
  120.       *  
  121.       * <code>writeJson2Entity</code> 
  122.       * @description: TODO (json converted to entity)  
  123.       * @throws IOException 
  124.       * @since 2011-11-8 Liao Yiping 
  125.       */  
  126.      public static void writeJson2Entity() throws IOException {  
  127.          System.out.println("json string is converted to entity-------------");  
  128. //       File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");  
  129. //       FileInputStream inputStream = new FileInputStream(file);  
  130. //       Student student = mapper.readValue(inputStream,Student.class);  
  131. //       System.out.println(student.toString());  
  132.          // pretty output  
  133.          //mapper.defaultPrettyPrintingWriter().writeValueAsString(value);  
  134.       
  135.          String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}";  
  136.          Student student1 = mapper.readValue(json,Student.class);  
  137.          System.out.println("json2:"+student1.toString());  
  138.      }  
  139.      /** 
  140.       *  
  141.       * <code>writeJson2List</code> 
  142.       * @description: TODO (json special trip list object)  
  143.       * @throws IOException 
  144.       * @since 2011-11-8 Liao Yiping 
  145.       */  
  146.      public static void writeJson2List() throws IOException {  
  147.          System.out.println("json string is converted to entity-------------");  
  148.          File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");  
  149.          FileInputStream inputStream = new FileInputStream(file);  
  150.          Student student = mapper.readValue(inputStream,Student.class);  
  151.          System.out.println(student.toString());  
  152.            
  153.          String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";  
  154.          List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class);  
  155.          for (int i = 0; i < s.size(); i++) {  
  156.              LinkedHashMap<String, Object> link = s.get(i);  
  157.              Set<String>  key = link.keySet();  
  158.              for (Iterator iterator = key.iterator(); iterator.hasNext();) {  
  159.                 String string = (String) iterator.next();  
  160.                 System.out.println(string+"==>"+link.get(string));  
  161.                   
  162.             }  
  163.              System.out.println("json:"+i+""+s.get(i).toString());  
  164.               
  165.         }  
  166.      }  
  167.      /** 
  168.        * Convert JSON to List object 
  169.        */  
  170.       public static void readJson2List() {  
  171.        String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"  
  172.          + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";  
  173.        try {  
  174.         List<LinkedHashMap<String, Object>> list = mapper.readValue(  
  175.           json, List.class);  
  176.         System.out.println(list.size());  
  177.         for (int i = 0; i < list.size(); i++) {  
  178.          Map<String, Object> map = list.get(i);  
  179.          Set<String> set = map.keySet();  
  180.          for (Iterator<String> it = set.iterator(); it.hasNext();) {  
  181.           String key = it.next();  
  182.           System.out.println(key + ":" + map.get(key));  
  183.          }  
  184.         }  
  185.        } catch (JsonParseException e) {  
  186.         e.printStackTrace ();  
  187.        } catch (JsonMappingException e) {  
  188.         e.printStackTrace ();  
  189.        } catch (IOException e) {  
  190.         e.printStackTrace ();  
  191.        }  
  192.       }  
  193.       /** 
  194.        * Convert JSON to List object 
  195.        */  
  196.       public static void readJson2Array() {  
  197.           String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"  
  198.               + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";  
  199.           try {  
  200.               Student[] students = mapper.readValue(json, Student[].class);  
  201.               for (Student student : students) {  
  202.                 System.out.println(">"+student.toString());  
  203.             }  
  204.           } catch (JsonParseException e) {  
  205.               e.printStackTrace ();  
  206.           } catch (JsonMappingException e) {  
  207.               e.printStackTrace ();  
  208.           } catch (IOException e) {  
  209.               e.printStackTrace ();  
  210.           }  
  211.       }  
  212.   
  213. }  

print result:

Convert the string to entity-------------
json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isStudent=true

writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

readJson2Array------------------
>uid=1,name=www,upwd=456,number=234.0,isStudent=false
>uid=5,name=tom,upwd=123,number=3.44,isStudent=false
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

Let's try one by one, the above is also my test code

 

[java] view plaincopy

  1. Entity class:  
  2. /* 
  3.  * @project java 
  4.  * @package  
  5.  * @file Student.java 
  6.  * @version  1.0 
  7.  * @author Liao Yiping 
  8.  * @time 2011-11-8 03:01:08 AM 
  9.  */  
  10.   
  11. public class Student {  
  12.     /* 
  13.      * 
  14.      * Class Descripton goes here. 
  15.      * 
  16.      * @class Student 
  17.      * @version  1.0 
  18.      * @author Liao Yiping 
  19.      * @time 2011-11-8 03:01:08 AM 
  20.      */  
  21.       private int uid;  
  22.       private String uname;  
  23.       private String upwd;  
  24.       private double number;  
  25.       private boolean isstudent;  
  26.     public int getUid() {  
  27.         return uid;  
  28.     }  
  29.     public void setUid(int uid) {  
  30.         this.uid = uid;  
  31.     }  
  32.     public String getUname () {  
  33.         return uname;  
  34.     }  
  35.     public void setUname(String uname) {  
  36.         this.uname = uname;  
  37.     }  
  38.     public String getUpwd() {  
  39.         return upwd;  
  40.     }  
  41.     public void setUpwd(String upwd) {  
  42.         this.upwd = upwd;  
  43.     }  
  44.     public double getNumber() {  
  45.         return number;  
  46.     }  
  47.     public void setNumber(double number) {  
  48.         this.number = number;  
  49.     }  
  50.     public boolean isIsstudent() {  
  51.         return isstudent;  
  52.     }  
  53.     public void setIsstudent (boolean isstudent) {  
  54.         this.isstudent = isstudent;  
  55.     }  
  56.     @Override  
  57.     public String toString() {  
  58.       
  59.         return "uid="+uid+",name="+uname+",upwd="+upwd+",number="+number+",isStudent="+isstudent;  
  60.     }  
  61.       
  62.         
  63. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325578165&siteId=291194637