• Jackson优化使用实例


    Jackson优化使用实例

    博客分类: 
     

    JSON的三种处理方式  Jackson提供了三种可选的JSON处理方法(一种方式及其两个变型):

    • 流式 API:(也称为"增量分析/生成") 读取和写入 JSON 内容作为离散事件。

    • 树模型 :提供一个 JSON 文档可变内存树的表示形式。

      • org.codehaus.jackson.map.ObjectMapper 生成树 ;树组成 JsonNode 节点集。

      • 树模型类似于 XML DOM。
    • 数据绑定: JSON和POJO相互转换,基于属性访问器规约或注解。

      • 有 两种变体: 简单 和 完整 的数据绑定:

      • 简单数据绑定: 是指从Java Map、List、String、Numbers、Boolean和空值进行转换

      • 完整数据绑定 :是指从任何 Java bean 类型 (及上文所述的"简单"类型) 进行转换

      • org.codehaus.jackson.map.ObjectMapper 对两个变种,进行编组(marshalling )处理 (写入 JSON) 和反编组(unmarshalling ,读 JSON)。

      • JAXB激励下的基于注释的 (代码优先)变种。

    从使用的角度来看,总结这些3 种方法的用法如下:

    • 流 API: 性能最佳的方式 (最低开销、 速度最快的读/写; 其它二者基于它实现)。

    • 数据绑定 :使用最方便的方式。

    • 树模型: 最灵活的方式。

    额,直接贴出所有代码吧,该说的都在代码中了

    Java代码  收藏代码
    1. package com.wujintao.json;  
    2.   
    3. import java.io.IOException;  
    4. import java.util.Date;  
    5.   
    6. import org.codehaus.jackson.JsonGenerationException;  
    7. import org.codehaus.jackson.map.JsonMappingException;  
    8. import org.codehaus.jackson.map.ObjectMapper;  
    9. import org.codehaus.jackson.map.ObjectReader;  
    10. import org.codehaus.jackson.map.ObjectWriter;  
    11. import org.junit.Test;  
    12.   
    13. /** 
    14.  * http://wiki.fasterxml.com/JacksonDownload 
    15.  * http://wiki.fasterxml.com/JacksonInFiveMinutes 
    16.  * http://wiki.fasterxml.com/JacksonBestPracticesPerformance 
    17.  * http://jackson.codehaus.org/1.8.8/javadoc/index.html 
    18.  * http://wiki.fasterxml.com/ObjectReader 
    19.  * https://github.com/FasterXML/jackson-docs/wiki/ObjectWriter 
    20.  * jars:jackson-core-lgpl-1.8.10.jar,jackson-mapper-lgpl-1.8.10.jar 
    21.  *  
    22.  * 1.实践结果,无论是哪种形式的转换,Jackson > Gson > Json-lib。Jackson的处理能力甚至高出Json-lib有10倍左右 
    23.  * 2.JSON-lib似乎已经停止更新,最新的版本也是基于JDK15,而Jackson的社区则较为活跃 
    24.  * 3.高并发情况下jackson表现尤为优越,内存占用甚少,json-lib会占用很多内存 
    25.  *  
    26.  */  
    27. public class TestCase {  
    28.       
    29.     @Test  
    30.     public void test() throws JsonGenerationException, JsonMappingException,  
    31.             IOException {  
    32.         long start = new Date().getTime();  
    33.         ObjectMapper mm = new ObjectMapper(); // can reuse, share  
    34.         Person pp = new Person();  
    35.         pp.setAdd("beijing");  
    36.         pp.setAge(11);  
    37.         pp.setSalary(1.1);  
    38.         pp.setSex('男');  
    39.         String jj = mm.writeValueAsString(pp);  
    40.         System.out.println(jj);  
    41.   
    42.         Person pp2 = mm.readValue(jj, Person.class);  
    43.         System.out.println(pp2);  
    44.           
    45.         long end = new Date().getTime();  
    46.         System.out.println("using ObjectMapper cost:"+(end-start)+"ms");  
    47.   
    48.         System.out.println("=================================");  
    49.         // 7. Prefer ObjectReader/ObjectWriter over ObjectMapper  
    50.         // Although the main reason to prefer these objects is thread-safety  
    51.         // (and thus correctness),there may be performance benefits as well  
    52.   
    53.         // 8.When reading a root-level sequence of POJOs,  
    54.         // readValues() method of ObjectReader can be much more efficient  
    55.         // than doing explicit iteration using ObjectMapper.readValue() method.  
    56.   
    57.         long start2 = new Date().getTime();  
    58.         ObjectMapper mapper = new ObjectMapper();  
    59.         Person person = new Person();  
    60.         person.setAdd("beijing");  
    61.         person.setAge(11);  
    62.         person.setSalary(1.1);  
    63.         person.setSex('男');  
    64.   
    65.         ObjectWriter writer = mapper.viewWriter(Person.class);  
    66.         String json = writer.writeValueAsString(person);  
    67.         System.out.println(json);  
    68.         // we'll be reading instances of MyBean  
    69.         ObjectReader reader = mapper.reader(Person.class);  
    70.         // and then do other configuration, if any, and read:  
    71.         Person result = reader.readValue(json);  
    72.         System.out.println(result);  
    73.         long end2 = new Date().getTime();  
    74.         System.out.println("using ObjectReader/ObjectWriter cost:"+(end2-start2)+"ms");  
    75.           
    76.         //官网建议使用ObjectReader/ObjectWriter  
    77.     }  
    78.       
    79.     //还有fast-json、Genson 等没做研究  
    80. }  
  • 相关阅读:
    .NET分页存储过程代码及使用
    优盘内文件夹大小为几十GB,文件夹内全是乱码而且无法删除的最佳解决办法
    [原创]对于“优盘变成了RAW的文件系统,双击提示需要格式化,右键查看属性是0字节,也无法打开”的解决办法
    [转载]Ubuntu下vi编辑器方向键变成字母的解决方法
    [转载]普通人的编辑利器——Vim
    [转载]“不能打开暂存盘文件,因为该文件已锁定”解决办法
    [转载]“10倍效率”程序员/开发人员的习惯
    关于一道.NET程序员面试题的遐想
    “未能从程序集“Microsoft.VisualStudio.DataDesign.SyncDesigner.DslPackage, Version=9.0.0.0, Culture=neutral...”的解决办法
    初学正则表达式之不可忽视的空白符
  • 原文地址:https://www.cnblogs.com/wzhanke/p/4562049.html
Copyright © 2020-2023  润新知