package com.ice.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
/**
* JSON的工具类
*
* <h3>Here is an example:</h3>
*
* <pre>
* // 将json通过类型转换成对象
* {@link JsonUtil JsonUtil}.fromJson("{\"username\":\"username\", \"password\":\"password\"}", User.class);
* </pre>
* <hr />
* <pre>
* // 传入转换的引用类型
* {@link JsonUtil JsonUtil}.fromJson("[{\"username\":\"username\", \"password\":\"password\"}, {\"username\":\"username\", \"password\":\"password\"}]", new TypeReference<List<User>>);
* </pre>
* <hr />
* <pre>
* // 将对象转换成json
* {@link JsonUtil JsonUtil}.toJson(user);
* </pre>
* <hr />
* <pre>
* // 将对象转换成json, 可以设置输出属性
* {@link JsonUtil JsonUtil}.toJson(user, {@link Inclusion Inclusion.ALWAYS});
* </pre>
* <hr />
* <pre>
* // 将对象转换成json, 传入配置对象
* {@link ObjectMapper ObjectMapper} mapper = new ObjectMapper();
* mapper.setSerializationInclusion({@link Inclusion Inclusion.ALWAYS});
* mapper.configure({@link Feature Feature.FAIL_ON_UNKNOWN_PROPERTIES}, false);
* mapper.configure({@link Feature Feature.FAIL_ON_NUMBERS_FOR_ENUMS}, true);
* mapper.setDateFormat(new {@link SimpleDateFormat SimpleDateFormat}("yyyy-MM-dd HH:mm:ss"));
* {@link JsonUtil JsonUtil}.toJson(user, mapper);
* </pre>
* <hr />
* <pre>
* // 获取Mapper对象
* {@link JsonUtil JsonUtil}.mapper();
* </pre>
*
* @see JsonUtil JsonUtil
* @see Feature Feature
* @see ObjectMapper ObjectMapper
* @see Inclusion Inclusion
* @see IOException IOException
* @see SimpleDateFormat SimpleDateFormat
*
*/
@SuppressWarnings("unchecked")
public final class JsonUtils {
private static ObjectMapper MAPPER;
static {
MAPPER = generateMapper(Inclusion.NON_NULL);
}
private JsonUtils() {
}
/**
* 将json通过类型转换成对象
* <pre>
* {@link JsonUtil JsonUtil}.fromJson("{\"username\":\"username\", \"password\":\"password\"}", User.class);
* </pre>
*
* @param json json字符串
* @param clazz 泛型类型
* @return 返回对象
* @throws IOException
*/
public static <T> T fromJson(String json, Class<T> clazz){
try {
return clazz.equals(String.class) ? (T) json : MAPPER.readValue(json, clazz);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json通过类型转换成对象
*
* <pre>
* {@link JsonUtil JsonUtil}.fromJson("[{\"username\":\"username\", \"password\":\"password\"}, {\"username\":\"username\", \"password\":\"password\"}]", new TypeReference<List<User>>);
* </pre>
*
* @param json json字符串
* @param typeReference 引用类型
* @return 返回对象
* @throws IOException
*/
public static <T> T fromJson(String json, TypeReference<?> typeReference) {
try {
return (T) (typeReference.getType().equals(String.class) ? json : MAPPER.readValue(json, typeReference));
} catch ( IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将对象转换成json
*
* <pre>
* {@link JsonUtil JsonUtil}.toJson(user);
* </pre>
*
* @param src 对象
* @return 返回json字符串
* @throws IOException
*/
public static <T> String toJson(T src){
try {
return src instanceof String ? (String) src : MAPPER.writeValueAsString(src);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将对象转换成json, 可以设置输出属性
*
* <pre>
* {@link JsonUtil JsonUtil}.toJson(user, {@link Inclusion Inclusion.ALWAYS});
* </pre>
*
* {@link Inclusion Inclusion 对象枚举}
* <ul>
* <li>{@link Inclusion Inclusion.ALWAYS 全部列入}</li>
* <li>{@link Inclusion Inclusion.NON_DEFAULT 字段和对象默认值相同的时候不会列入}</li>
* <li>{@link Inclusion Inclusion.NON_EMPTY 字段为NULL或者""的时候不会列入}</li>
* <li>{@link Inclusion Inclusion.NON_NULL 字段为NULL时候不会列入}</li>
* </ul>
*
* @param src 对象
* @param inclusion 传入一个枚举值, 设置输出属性
* @return 返回json字符串
* @throws IOException
*/
public static <T> String toJson(T src, Inclusion inclusion) throws IOException {
if (src instanceof String) {
return (String) src;
} else {
ObjectMapper customMapper = generateMapper(inclusion);
return customMapper.writeValueAsString(src);
}
}
/**
* 将对象转换成json, 传入配置对象
*
* <pre>
* {@link ObjectMapper ObjectMapper} mapper = new ObjectMapper();
* mapper.setSerializationInclusion({@link Inclusion Inclusion.ALWAYS});
* mapper.configure({@link Feature Feature.FAIL_ON_UNKNOWN_PROPERTIES}, false);
* mapper.configure({@link Feature Feature.FAIL_ON_NUMBERS_FOR_ENUMS}, true);
* mapper.setDateFormat(new {@link SimpleDateFormat SimpleDateFormat}("yyyy-MM-dd HH:mm:ss"));
* {@link JsonUtil JsonUtil}.toJson(user, mapper);
* </pre>
*
* {@link ObjectMapper ObjectMapper}
*
* @see ObjectMapper
*
* @param src 对象
* @param mapper 配置对象
* @return 返回json字符串
* @throws IOException
*/
public static <T> String toJson(T src, ObjectMapper mapper) throws IOException {
if (null != mapper) {
if (src instanceof String) {
return (String) src;
} else {
return mapper.writeValueAsString(src);
}
} else {
return null;
}
}
/**
* 将对象转换成map, 传入配置对象
*
* <pre>
* {@link ObjectMapper ObjectMapper} mapper = new ObjectMapper();
* mapper.setSerializationInclusion({@link Inclusion Inclusion.ALWAYS});
* mapper.configure({@link Feature Feature.FAIL_ON_UNKNOWN_PROPERTIES}, false);
* mapper.configure({@link Feature Feature.FAIL_ON_NUMBERS_FOR_ENUMS}, true);
* mapper.setDateFormat(new {@link SimpleDateFormat SimpleDateFormat}("yyyy-MM-dd HH:mm:ss"));
* {@link JsonUtil JsonUtil}.toJson(user, mapper);
* </pre>
*
* {@link ObjectMapper ObjectMapper}
* @param <T>
*
* @see ObjectMapper
*
* @param src 对象
* @param mapper 配置对象
* @return 返回map
* @throws IOException
*/
public static Map<String, Object> toMap(String src) throws IOException {
Map<String, Object> map = mapper().readValue(src, Map.class);
return map;
}
/**
* 返回{@link ObjectMapper ObjectMapper}对象, 用于定制性的操作
*
* @return {@link ObjectMapper ObjectMapper}对象
*/
public static ObjectMapper mapper() {
没有合适的资源?快使用搜索试试~ 我知道了~
icewebservice框架
共157个文件
class:58个
java:53个
xml:14个
5星 · 超过95%的资源 需积分: 9 41 下载量 129 浏览量
2014-09-29
14:13:57
上传
评论 1
收藏 192KB ZIP 举报
温馨提示
基于spring开发自己的webservice框架,会话,appKey,密钥, 签名,时间戳,超时等功能,类似淘宝的top平台。
资源推荐
资源详情
资源评论
收起资源包目录
icewebservice框架 (157个子文件)
IceBuilder.class 8KB
Dispatcher.class 7KB
DispatchRouter.class 6KB
IceRequestContext.class 6KB
JsonUtils.class 5KB
IceContext$1.class 4KB
SignUtils.class 4KB
RequestUtil.class 3KB
IceContext.class 3KB
UserWebService.class 3KB
ServiceMethodHandler.class 3KB
DispatchRouter$ServiceRunnable.class 3KB
UserTest.class 2KB
IceServlet.class 2KB
ParametersError.class 2KB
IceSession.class 2KB
XmlUitls.class 2KB
ServiceMethodValue.class 2KB
ResponseUtil.class 2KB
InvokeServiceImpl.class 2KB
IceErrorResponse.class 2KB
ActionError.class 2KB
MaxSourcesError.class 1KB
TimestampError.class 1KB
AppKeyError.class 1KB
MehodError.class 1KB
SessionError.class 1KB
ObsoletedError.class 1KB
ExecuteError.class 1KB
SignError.class 1KB
TimeoutError.class 1KB
IceBuilder$2.class 1KB
AppKeyServiceImpl.class 1KB
HttpAction.class 1KB
ObsoletedType.class 1KB
SessionType.class 1KB
SessionServiceImpl.class 1KB
SignType.class 1KB
IceContext$2.class 1KB
IceResponse.class 1KB
IceBuilder$1.class 1KB
ServiceMethod.class 1010B
AbstracetIceRequest.class 893B
LoginRequest.class 880B
SystemParameterNames.class 852B
SystemValue.class 843B
IceException.class 803B
ErrorType.class 748B
WebService.class 477B
Session.class 443B
NoSign.class 435B
Temporary.class 429B
InvokeService.class 396B
Router.class 333B
BaseRequest.class 319B
SessionService.class 267B
AppKeyService.class 265B
IceRequest.class 260B
.classpath 1KB
.classpath 1KB
.classpath 123B
org.eclipse.wst.common.component 897B
org.eclipse.wst.common.component 246B
org.eclipse.wst.jsdt.ui.superType.container 49B
file.html 586B
file.html 586B
ice.jar 55KB
JsonUtils.java 9KB
IceBuilder.java 8KB
Dispatcher.java 7KB
DispatchRouter.java 6KB
IceContext.java 5KB
IceRequestContext.java 5KB
RequestUtil.java 2KB
SignUtils.java 2KB
UserWebService.java 2KB
ServiceMethodHandler.java 2KB
UserTest.java 2KB
ServiceMethodValue.java 2KB
IceServlet.java 2KB
ServiceMethod.java 1KB
ResponseUtil.java 1KB
ParametersError.java 1KB
XmlUitls.java 1KB
IceErrorResponse.java 1KB
SystemValue.java 1KB
MaxSourcesError.java 1022B
ErrorType.java 1001B
ActionError.java 996B
InvokeServiceImpl.java 996B
IceSession.java 933B
ExecuteError.java 927B
TimeoutError.java 911B
TimestampError.java 911B
AppKeyError.java 906B
SystemParameterNames.java 906B
MehodError.java 895B
SessionError.java 887B
ObsoletedError.java 885B
SignError.java 874B
共 157 条
- 1
- 2
资源评论
- liu2010dn2017-01-23资源可用,感谢!!!!
- fei02152018-01-23有帮助,非常感谢
kluyuyu
- 粉丝: 4
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功