package cn.springmvc.json;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
*/
public class JsonWriter {
private static ObjectMapper om = new ObjectMapper();
private static JsonFactory jf = new JsonFactory();
public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass) {
try {
return om.readValue(jsonAsString, pojoClass);
} catch (JsonParseException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public static <T> Object fromJson(FileReader fr, Class<T> pojoClass) {
try {
return om.readValue(fr, pojoClass);
} catch (JsonParseException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public static String toJson(Object pojo, boolean prettyPrint) {
try {
// String start = System.nanoTime();
StringWriter sw = new StringWriter();
JsonGenerator jg = jf.createGenerator(sw);
if (prettyPrint) {
jg.useDefaultPrettyPrinter();
}
om.writeValue(jg, pojo);
// return sw.toString().replaceAll("\r\n", "");
// System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
// + (System.nanoTime() - start));
return sw.toString();
} catch (JsonParseException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint) {
try {
JsonGenerator jg = jf.createGenerator(fw);
if (prettyPrint) {
jg.useDefaultPrettyPrinter();
}
om.writeValue(jg, pojo);
} catch (JsonParseException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public static String toJson(Object pojo) {
return toJson(pojo, false);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springmvc实例代码
共54个文件
class:13个
jar:13个
java:13个
5星 · 超过95%的资源 需积分: 12 573 下载量 11 浏览量
2016-09-21
15:58:07
上传
评论 8
收藏 5.77MB RAR 举报
温馨提示
springmvc实例代码 有关springmvc入门请看 http://blog.csdn.net/zxw136511485/article/details/52583337
资源推荐
资源详情
资源评论
收起资源包目录
springmvc_project.rar (54个子文件)
springmvc_project
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
views
result.jsp 783B
success.jsp 943B
lib
spring-beans-4.0.0.RELEASE.jar 654KB
spring-core-4.0.0.RELEASE.jar 933KB
jackson-core-2.3.0.jar 193KB
jackson-databind-2.3.0.jar 893KB
spring-context-4.0.0.RELEASE.jar 928KB
spring-webmvc-4.0.0.RELEASE.jar 645KB
spring-aop-4.0.0.RELEASE.jar 343KB
commons-logging-1.1.1.jar 59KB
mysql-connector-5.1.8.jar 707KB
spring-expression-4.0.0.RELEASE.jar 202KB
jackson-annotations-2.3.0.jar 34KB
jstl-1.2.jar 405KB
spring-web-4.0.0.RELEASE.jar 646KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.core.resources.prefs 166B
org.eclipse.wst.common.component 503B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 567B
org.eclipse.jdt.core.prefs 364B
src
springmvc.xml 1KB
cn
springmvc
business
StudentBusiness.java 2KB
DBHelper.java 1KB
action
StudentServlet.java 1KB
model
Students.java 836B
json
StatusCode.java 412B
JsonWriter.java 3KB
AbstractJsonObject.java 737B
ListObject.java 282B
SingleObject.java 244B
ResponseUtils.java 2KB
JackJsonUtils.java 1KB
service
impl
StudentServiceImpl.java 413B
StudentService.java 193B
.project 1KB
.classpath 841B
build
classes
springmvc.xml 1KB
cn
springmvc
business
StudentBusiness.class 3KB
DBHelper.class 1KB
action
StudentServlet.class 2KB
model
Students.class 2KB
json
AbstractJsonObject.class 1KB
ResponseUtils.class 2KB
SingleObject.class 573B
StatusCode.class 720B
JackJsonUtils.class 1KB
JsonWriter.class 3KB
ListObject.class 708B
service
StudentService.class 248B
impl
StudentServiceImpl.class 597B
共 54 条
- 1
程序员七哥
- 粉丝: 1071
- 资源: 70
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页