com.aiwiown.face.internal.parser.json.JsonConverter Maven / Gradle / Ivy
package com.aiwiown.face.internal.parser.json;
import com.aiwiown.face.*;
import com.aiwiown.face.internal.mapping.Converter;
import com.aiwiown.face.internal.mapping.Converters;
import com.aiwiown.face.internal.mapping.Reader;
import com.aiwiown.face.internal.util.json.ExceptionErrorListener;
import com.aiwiown.face.internal.util.json.JSONReader;
import com.aiwiown.face.internal.util.json.JSONValidatingReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* JSON格式转换器。
*
* @author carver.gu
* @since 1.0, Apr 11, 2010
*/
public class JsonConverter implements Converter {
@Override
public T toResponse(String rsp, Class clazz) throws ApiException {
JSONReader reader = new JSONValidatingReader(new ExceptionErrorListener());
Object rootObj = reader.read(rsp);
if (rootObj instanceof Map, ?>) {
Map, ?> rspJson = (Map, ?>) rootObj;
return fromJson(rspJson, clazz);
}
return null;
}
/**
* 把JSON格式的数据转换为对象。
*
* @param 泛型领域对象
* @param json JSON格式的数据
* @param clazz 泛型领域类型
* @return 领域对象
* @throws ApiException
*/
public T fromJson(final Map, ?> json, Class clazz) throws ApiException {
return Converters.convert(clazz, new Reader() {
@Override
public boolean hasReturnField(Object name) {
return json.containsKey(name);
}
@Override
public Object getPrimitiveObject(Object name) {
return json.get(name);
}
@Override
public Object getObject(Object name, Class> type) throws ApiException {
Object tmp = json.get(name);
if (tmp instanceof Map, ?>) {
Map, ?> map = (Map, ?>) tmp;
return fromJson(map, type);
} else {
return null;
}
}
@Override
public List> getListObjects(Object listName, Object itemName, Class> subType) throws ApiException {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy