package com.vsked.test;

import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vsked.bean.User;

public class TestJackson {
	
	private  final Logger log=LogManager.getLogger(TestJackson.class);
	
	ObjectMapper jackson = new ObjectMapper();
	Writer strWriter = new StringWriter();
	
	@Test
	public void objectToJson() throws Exception{
		String s=jackson.writeValueAsString(new User("smallvill", 18));
		log.debug(s);
		
	}
	
//	@Test
	public void jsonToObject() throws Exception{
		
		jackson.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

		//String s="{\"userName\":\"smallvillc\",\"userAge\":26}";
		String s="{'userName':'smallvillc','userAge':26}";
		User u=jackson.readValue(s, User.class);
		log.debug(u.getUserName()+"|"+u.getUserAge());
	}
	
//	@Test
	public void listToJson() throws Exception{
		List<User> userList=new ArrayList<User>();
		for(int i=0;i<10;i++)
			userList.add(new User("user"+i,20+i));
		
		String s=jackson.writeValueAsString(userList);
		log.debug(s);
	}
	
//	@Test
	public void jsonToList() throws Exception{
		String s="[{\"userName\":\"user0\",\"userAge\":20},{\"userName\":\"user1\",\"userAge\":21},{\"userName\":\"user2\",\"userAge\":22},{\"userName\":\"user3\",\"userAge\":23},{\"userName\":\"user4\",\"userAge\":24},{\"userName\":\"user5\",\"userAge\":25},{\"userName\":\"user6\",\"userAge\":26},{\"userName\":\"user7\",\"userAge\":27},{\"userName\":\"user8\",\"userAge\":28},{\"userName\":\"user9\",\"userAge\":29}]";
		List<User> userList=jackson.readValue(s, new TypeReference<List<User>>(){});
		
//		List<User> userList=jackson.readValue(s, jackson.getTypeFactory().constructCollectionType(List.class, User.class));
		
	    for(int i=0;i<userList.size();i++){
	    	User u=userList.get(i);
	    	log.debug(u.getUserName()+"|"+u.getUserAge());
	    }
	    
	}
	
//	@Test
	public void mapToJson() throws Exception{
		
		Map<String, Integer> m=new HashMap<String, Integer>();
		m.put("smallvillm1", 25);
		m.put("smallvillm2", 26);
		String s=jackson.writeValueAsString(m);
		log.debug(s);
		
	}
	
//	@Test
	public void jsonToMap() throws Exception{
		
		String s="{\"smallvillm1\":25,\"smallvillm2\":26}";
		
		Map<String, Integer> m=jackson.readValue(s, new TypeReference<Map<String, Integer>>(){});
		
		log.debug(m.get("smallvillm1")+"|"+m.get("smallvillm2"));	
		
	}

}

@@ -130,16 +130,46 @@
 shMap;%0D%0A
+import java.util.LinkedList;%0D%0A
 import j
@@ -180,24 +180,24 @@
 util.List;%0D%0A
-
 import java.
@@ -207,18 +207,16 @@
 l.Map;%0D%0A
-%0D%0A
 import o
@@ -313,26 +313,24 @@
 unit.Test;%0D%0A
-%0D%0A
 import com.f
@@ -695,24 +695,26 @@
 ();%0D%0A%09%0D%0A
+//
 %09@Test%0D%0A
 %09public 
@@ -697,32 +697,32 @@
 ;%0D%0A%09%0D%0A//%09@Test%0D%0A
-
 %09public void obj
@@ -1364,24 +1364,24 @@
 0+i));%0D%0A%09%09%0D%0A
-
 %09%09String s=j
@@ -1444,32 +1444,458 @@
 %7D%0D%0A%09%0D%0A//%09@Test%0D%0A
+%09public void listToJson1() throws Exception%7B%0D%0A%09%09List%3CMap%3CString, Object%3E%3E dataList=new LinkedList%3CMap%3CString,Object%3E%3E();%0D%0A%09%09%0D%0A%09%09Map%3CString, Object%3E data=null;%0D%0A%09%09for(int i=0;i%3C10;i++)%7B%0D%0A%09%09%09data=new HashMap%3CString, Object%3E();%0D%0A%09%09%09data.put(%22name%22, %22x%22+i);%0D%0A%09%09%09data.put(%22seq%22, i);%0D%0A%09%09%09data.put(%22isAllright%22, true);%0D%0A%09%09%09dataList.add(data);%0D%0A%09%09%7D%0D%0A%09%09String s=jackson.writeValueAsString(dataList);%0D%0A%09%09log.debug(s);%0D%0A%09%7D%0D%0A%09%0D%0A//%09@Test%0D%0A
 %09public void jso

update jacksondemo for listtojson