All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gu.simplemq.json.BaseJsonEncoder Maven / Gradle / Ivy

There is a newer version: 2.3.17
Show newest version
package gu.simplemq.json;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Map.Entry;

import gu.simplemq.exceptions.SmqNotBeanException;

/**
 * json实现对象序列化反序列化的抽象类
 * @author guyadong
 *
 */
public abstract class BaseJsonEncoder {
	private static final FastJsonInitializer initizlizer = getConfigProvider();
	static{
		try{
			initizlizer.init();
		}catch (Throwable e) {
			System.out.println(String.format("%s INITIALIZE ERROR:%s ",initizlizer.getClass().getName(),e.getMessage()));
		}
	}
	public BaseJsonEncoder() {
	}
	/**
	 * SPI(Service Provider Interface)机制加载 {@link GfLicenseProvider}实例,
	 * 没有找到返回{@link DefaultCasAndroidConfig}实例
	 * @return
	 */
	private static FastJsonInitializer getConfigProvider() {		
		ServiceLoader initizlizer = ServiceLoader.load(FastJsonInitializer.class);
		Iterator itor = initizlizer.iterator();
		if(!itor.hasNext()){
			return new FastJsonInitializer(){
				@Override
				public void init() {
				}};
		}
		return itor.next();
	}
	/**
	 * serializes model to Json
	 * @param obj
	 * @return
	 */
	public abstract String toJsonString(Object obj);
	
	/**
	 * 按字段(field)序列化为Map对象
	 * @param obj
	 * @return
	 * @throws SmqNotBeanException
	 */
	public abstract Map toJsonMap(Object obj)throws SmqNotBeanException;

	/**
	 * deserialize json into T
	 * @param json
	 * @param type
	 * @return instance of {@code type } or {@code null} if {@code json} is {@code null}
	 */
	public abstract  T fromJson(String json, Type type);
	
	/**
	 * deserialize json field map  into T
	 * @param fieldHash
	 * @param type
	 * @return instance of {@code type } or {@code null} if {@code fieldHash} is {@code null} or empty
	 * @throws SmqNotBeanException
	 */
	public abstract  T fromJson(Map fieldHash, Type type)throws SmqNotBeanException ;
	
	public T fromJson(String json, Class clazz) {		
		return fromJson(json,(Type)clazz);
	}
	
	public  T fromJson(Map fieldHash, Class clazz)throws SmqNotBeanException {
		return fromJson(fieldHash,(Type)clazz);
	}
	
	public Map fromJson(Map fieldHash,Map types){
		if(null == fieldHash) {
			return null;
		}
		LinkedHashMap fields = new LinkedHashMap();
		for(Entry entry:fieldHash.entrySet()){
			String field = entry.getKey();
			fields.put(field, this.fromJson(entry.getValue(), null == types ? null : types.get(field)));
		}
		return fields;
	}
	
	public List toJsonArray(@SuppressWarnings("unchecked") T...array){
		return null == array ? null : toJsonArray(Arrays.asList(array));
	}
	
	public List toJsonArray(Collection c){
		if(null == c){
			return null;
		}
		ArrayList list = new ArrayList(16);
		for( Object element:c){
			try{
				list.add(this.toJsonMap(element));
			}catch(SmqNotBeanException e){
				list.add(this.toJsonString(element));
			}
		}
		return list;
	}
	
	public static final BaseJsonEncoder getEncoder(){
		return FastjsonEncoder.getInstance(); 
	}
}