package com.blueland.bpm.engine.util;

import java.io.StringWriter;

import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


public class JsonUtil {
	
	public static String convertToJson(Object bean) {
		StringWriter sw = new StringWriter();
		try{
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX,true);  
		JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
		mapper.writeValue(gen, bean);
		}catch(Exception e){
			e.printStackTrace();
		}
		return sw.toString();
	}
	

	public static Object convertToBean(String jsonStr, Class clas) {
		Object bean = null;
		try {
			ObjectMapper mapper = new ObjectMapper();
			bean = mapper.readValue(jsonStr, clas);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}
	public static void main(String[] args) {
		ProcessInstanceModel p=new ProcessInstanceModel();
		p.setActionType("\"1");
		System.out.println(JsonUtil.convertToJson(p));
	//	String a="{\"procDefId\":null,\"procDefName\":null,\"procInstId\":null,\"topProcInstId\":null,\"parentProcInstId\":null,\"procTitle\":null,\"startUserId\":null,\"startUserName\":null,\"starterOrgId\":null,\"starterOrgName\":null,\"createTime\":null,\"finishTime\":null,\"startActivityId\":null,\"endActivityId\":null,\"businessKey\":null,\"procState\":null,\"hasSubProcess\":false,\"actionType\":\"1\",\"processDefinition\":null,\"currentActivity\":null,\"nextActivity\":null,\"subProcInstIds\":[],\"processVariables\":{},\"processInputModel\":null,\"tenantId\":null}";
		//System.out.println(JsonUtil.convertToBean(a, ProcessInstanceModel.class));
	}
}
