`

Java生成json技术比较

 
阅读更多

一切源于尘土,一切归于尘土,事事有原因就有结果,不要在乎结果是什么,享受其中的过程最重要

                                                                                                                             -----chenhailong

 

JavaJson的工具很多,现在主要流行的有Jackson,Gson,JsonSimple

 

我这里主要比较这三种。

 

Jackson发展的还算顺利,官网:http://jackson.codehaus.org/ 

官网上的信息不是太多,但也够用了。

Jackson官网很大胆的说了高性能Json转换工具,嘿嘿,我感觉性能上确实不错,大量测试中Jackson给的状态还算不错的哦。不过没有JsonSimple快,我们一会再说

官网上给出了

 

  • Streaming (reading, writing)
  • FAST (measured to be faster than any other Java json parser and data binder)
  • Powerful (full data binding for common JDK classes as well as any Java bean class, Collection, Map or Enum)
  • Zero-dependency (does not rely on other packages beyond JDK)
  • Open Source (LGPL or AL)
  • Fully conformant
  • Extremely configurable


1.流操作
2.快捷
3.强大
4.零依赖(还算可以,不过接下来我介绍的都是零依赖)
5.开源(我喜欢开源)
6.简单
7.极限配置

   
Gson 是google出的Json 解析工具

官网:http://code.google.com/p/google-gson/

不能说是官网,现在这个工具,还没有成为系统,可以说维护上有差距。至于性能上,可以说是非常的低了。

User Guide 
https://sites.google.com/site/gson/gson-user-guide


Jsonsimple 
官网:http://code.google.com/p/json-simple/
也是没有正规的组织者维护的项目,不过性能上,可以说是非常的棒,我比较推荐这个

下面看看比较代码的实现

package com.bean;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonProperty;

@JsonAutoDetect
public class User {

    @JsonProperty
    private int age = 29;

    @JsonProperty
    private String name = "chenhailong";

    @JsonProperty
    private List<String> messages = new ArrayList<String>();

    public User() {
        for (int i = 0; i < 10000; i++) {
            messages.add("msg");
        }

    }

    @Override
    public String toString() {
        return "User [age=" + age + ", name=" + name + ", " + "messages=" + messages + "]";
    }
}
 

Gson 代码

package com.gson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import com.bean.User;
import com.google.gson.Gson;

public class GsonRead {

    public static void main(String[] args) {

        Gson gson = new Gson();

        try {
            Date startDate = new Date();
            BufferedReader br = new BufferedReader(new FileReader("user.json"));

            User obj = gson.fromJson(br, User.class);

            Date endDate = new Date();

            // display to console
            System.out.println(endDate.getTime() - startDate.getTime());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
 

 
package com.gson;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import com.bean.User;
import com.google.gson.Gson;

public class GsonWirte {
    public static void main(String[] args) {

        Date startDate = new Date();
        User obj = new User();
        Gson gson = new Gson();
        String json = gson.toJson(obj);

        try {
            FileWriter writer = new FileWriter("user.json");
            writer.write(json);
            writer.close();
            Date endDate = new Date();

            // display to console
            System.out.println(endDate.getTime() - startDate.getTime());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
 
  

Jackson的代码

 

 

package com.jackson;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.bean.User;

public class JSonRead {
    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();

        try {

            Date startDate = new Date();

            // read from file, convert it to user class
            User user = mapper.readValue(new File("user.json"), User.class);
            Date endDate = new Date();

            // display to console
            System.out.println(endDate.getTime() - startDate.getTime());

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }
}
 

 

 

 

package com.jackson;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.bean.User;

public class JSonWirte {
    public static void main(String[] args) {

        User user = new User();
        ObjectMapper mapper = new ObjectMapper();

        try {

            Date startDate = new Date();
            mapper.writeValue(new File("user.json"), user);

            // System.out.println(mapper.writeValueAsString(user));
            Date endDate = new Date();

            System.out.println(endDate.getTime() - startDate.getTime());

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

 

Jsonsimple的代码

 

 

package com.jsonsimple;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSampleRead {
    public static void main(String[] args) {

        JSONParser parser = new JSONParser();

        try {

            Date startDate = new Date();
            Object obj = parser.parse(new FileReader("user.json"));

            JSONObject jsonObject = (JSONObject) obj;
            Date endDate = new Date();

            System.out.println(endDate.getTime() - startDate.getTime());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

 

 

 

 

package com.jsonsimple;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonSampleWirter {
    public static void main(String[] args) {

        JSONObject obj = new JSONObject();
        obj.put("name", "mkyong.com");
        obj.put("age", new Integer(100));

        JSONArray list = new JSONArray();
        for (int i = 0; i < 10000; i++) {
            list.add("msg 1");

        }

        obj.put("messages", list);

        try {

            Date startDate = new Date();
            FileWriter file = new FileWriter("user.json");
            file.write(obj.toJSONString());
            file.flush();
            file.close();
            Date endDate = new Date();

            System.out.println(endDate.getTime() - startDate.getTime());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

 

 

比较结果

 

 

读的方面: Jsonsimple > Jackson > Gson

 

写的方面:Jsonsimple > Jackson > Gson

 

下面提供功能,大家也可以尝试一下

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics