ジャックソンファクトリー


参考資料
1 javaのいくつかのjsonツール分析テスト【元】
http://blog.csdn.net/weinianjie1/article/details/6561788
2つのJSONクラスJacksonとJSON-libの性能比較(第3試験追加)
http://wangym.iteye.com/blog/738933
上記の文章に基づいて、jacksonの性能は他の2つのjsonより高いですが、gsonの使い方は私達にとって一番良い理解です.gsonの提供する2つの方法をまねて、以下はjacksonに包装しました.他にorg.json.jarのカバンがあります.ソースコードを梱包したものです.アップロードしてください.後で使えるかもしれません.

環境:XP+JDK 1.6バージョン:jackson-all-1.8.1.jar
工事の画像は以下の通りです
JacksonFactory(仿gson)
ジャックソンファクトリー.java

package net.liuzd.json;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFactory {	
	
	private static final  ObjectMapper mapper = new ObjectMapper();	
	
	private JacksonFactory(){
		
	}
	
	
	/**
	 * @param <T>      
	 * @param bean     
	 * @return     JSON   
	 */
	public static <T> String toJson(T bean){		
		StringWriter sw = new StringWriter(); 
		try {					
			JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
			mapper.writeValue(gen, bean);
			gen.close(); 
			return sw.toString();
		} catch (JsonGenerationException e) {
			throw new RuntimeException(e.getMessage(),e);
		} catch (JsonMappingException e) {
			throw new RuntimeException(e.getMessage(),e);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(),e);
		}  			
	}
	
	/**
	 * @param <T>       
	 * @param json  JSON   
	 * @param clzz          
	 * @return
	 */
	public static <T> T fromJson(String json,Class<T> clzz){		
		
		T t = null;
		try {			
			t =  mapper.readValue(json, clzz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return t;
	}
	
	/**
	 * @param json  JSON   ,   key       
	 * @return      Map  ,   HashMap
	 */
	public static Map<?,?> fromJson(String json){
		try {
			return  mapper.readValue(json, HashMap.class);
		} catch (JsonParseException e) {
			throw new RuntimeException(e.getMessage(),e);
		} catch (JsonMappingException e) {
			throw new RuntimeException(e.getMessage(),e);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(),e);
		}			
	}
}
TestJson.java(テストクラス)

package net.liuzd.json;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class TestJson {

	
	public static void main(String[] args) {	
		
		String json;
		List<Book> books = new ArrayList<Book>();  
        Book b1 = new Book();  
        b1.setId(1);  
        b1.setName("book1");  
        Book b2 = new Book();  
        b2.setId(2);  
        b2.setName("book2");  
        books.add(b1);  
        books.add(b2);  
        Order order = new Order();  
        order.setBooks(books);  
        order.setCost(100.5f);  
        order.setName("orderName"); order.setDate(new Date(System.currentTimeMillis()));  
        
        json = JacksonFactory.toJson(order);
        System.out.println("order: " + json);
        
        order = JacksonFactory.fromJson(json, Order.class);  
        System.out.println(order.getName()+","+order.getCost() + "," + order.getDate());
        
        for(Book b : order.getBooks()){
        	System.out.println(b.getId() + "," + b.getName());
        }
        Users bean = new Users("1","  ",23);
		json = JacksonFactory.toJson(bean);
		System.out.println(json);
		
		Users bean2 = JacksonFactory.fromJson(json, Users.class);  
		System.out.println(bean2.getId() + "," + bean2.getUsername() + "," + bean2.getAge());    	
		
	    Map<?,?> map = JacksonFactory.fromJson("{\"a\":1, \"b\":2}");	    
	    System.out.println(map.get("a"));
	    System.out.println(map.get("b"));
	    
	    json = JacksonFactory.toJson(map);
	    System.out.println(json);    

	}
}
出力結果は以下の通りです.

order: {"name":"orderName","date":1318986605676,"cost":100.5,"books":[{"name":"book1","id":1},{"name":"book2","id":2}]}
orderName,100.5,Wed Oct 19 09:10:05 CST 2011
1,book1
2,book2
{"id":"1","username":"  ","age":23}
1,  ,23
1
2
{"b":2,"a":1}
他の種類は書かないようにしましょう.
プロジェクトにはこの二つのjarが含まれていません.以下の通りです.