public class JsonMapper extends Object
参考文档地址:http://wiki.fasterxml.com/JacksonInFiveMinutes
限定符和类型 | 字段和说明 |
---|---|
private static org.slf4j.Logger |
LOGGER |
private static com.fasterxml.jackson.databind.ObjectMapper |
mapper |
限定符 | 构造器和说明 |
---|---|
private |
JsonMapper() |
限定符和类型 | 方法和说明 |
---|---|
static JsonMapper |
create()
初始化
|
com.fasterxml.jackson.databind.JavaType |
createCollectionType(Class<?> collectionClass,
Class<?>... elementClasses)
构造的Collection Type如:
ArrayList<Bean> , 则调用constructCollectionType(ArrayList.class,Bean.class)
HashMap<String,Bean> , 则调用(HashMap.class,String.class, Bean.class) |
void |
enableEnumUseToString()
设置是否使用Enun的toString函数来读写Enum,
为false时候使用Enum的name()函数来读写Enum,默认为false.
|
<T> T |
fromJson(byte[] buffer,
Class<T> clazz)
反序列化POJO或简单
Collection如List<String> . |
<T> T |
fromJson(byte[] buffer,
com.fasterxml.jackson.databind.JavaType javaType)
反序列化复杂Collection如
List<Bean> , 先使用createCollectionType构造类型,然后调用本函数. |
<T> T |
fromJson(byte[] buffer,
com.fasterxml.jackson.core.type.TypeReference<T> tr)
可以通过
new TypeReference<Map<String,List<Bean>>>(){} 来调用 |
<T> T |
fromJson(String content,
Class<T> clazz)
反序列化POJO或简单
Collection如List<String> . |
<T> T |
fromJson(String content,
com.fasterxml.jackson.databind.JavaType javaType)
反序列化复杂Collection如
List<Bean> , 先使用createCollectionType构造类型,然后调用本函数. |
<T> T |
fromJson(String content,
com.fasterxml.jackson.core.type.TypeReference<T> tr)
可以通过
new TypeReference<Map<String,List<Bean>>>(){} 来调用 |
com.fasterxml.jackson.databind.ObjectMapper |
getMapper()
取出Mapper做进一步的设置或使用其他序列化API.
|
<T> String |
toJson(T data)
如果对象为Null, 返回"null".
|
<T> String |
toJsonP(String functionName,
T data)
用于界面JsonP callback使用
|
<T> String |
toJsonPretty(T data)
格式化生成的json
|
<T> T |
update(String content,
T object)
当JSON里只含有Bean的部分属性时候,更新一个已经存在的Bean,只覆盖该部分属性.
|
<T> void |
write(Writer writer,
T data) |
private static final org.slf4j.Logger LOGGER
private static com.fasterxml.jackson.databind.ObjectMapper mapper
public static JsonMapper create()
JsonMapper
public <T> String toJson(T data)
T
- the data typedata
- datapublic <T> String toJsonPretty(T data)
T
- the data typedata
- the object to jsonpublic <T> void write(Writer writer, T data) throws IOException
T
- the data typewriter
- writerdata
- dataIOException
- 抛出IO异常public <T> T fromJson(String content, Class<T> clazz)
Collection如List<String>
.
如果JSON字符串为Null或"null"字符串, 返回Null. 如果JSON字符串为"[]", 返回空集合.
如需反序列化复杂Collection如List<Bean>
, 请使用
T
- the object type will be returncontent
- the contentclazz
- the classT
fromJson(String, JavaType)
public <T> T fromJson(byte[] buffer, Class<T> clazz)
Collection如List<String>
.
如果JSON字符串为Null或"null"字符串, 返回Null. 如果JSON字符串为"[]", 返回空集合.
如需反序列化复杂Collection如List<Bean>
, 请使用
T
- the object type will be returnbuffer
- the bytes contentclazz
- the classT
fromJson(String, JavaType)
public <T> T fromJson(String content, com.fasterxml.jackson.databind.JavaType javaType)
List<Bean>
, 先使用createCollectionType构造类型,然后调用本函数.T
- the object type will be returncontent
- the contentjavaType
- java typecreateCollectionType(Class, Class...)
public <T> T fromJson(byte[] buffer, com.fasterxml.jackson.databind.JavaType javaType)
List<Bean>
, 先使用createCollectionType构造类型,然后调用本函数.T
- the object type will be returnbuffer
- the json bytesjavaType
- java typecreateCollectionType(Class, Class...)
public com.fasterxml.jackson.databind.JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses)
ArrayList<Bean>
, 则调用constructCollectionType(ArrayList.class,Bean.class)
HashMap<String,Bean>
, 则调用(HashMap.class,String.class, Bean.class)collectionClass
- collection classelementClasses
- the elements classpublic <T> T fromJson(String content, com.fasterxml.jackson.core.type.TypeReference<T> tr)
new TypeReference<Map<String,List<Bean>>>(){}
来调用T
- the object type will be returncontent
- the contenttr
- type referenceT
public <T> T fromJson(byte[] buffer, com.fasterxml.jackson.core.type.TypeReference<T> tr)
new TypeReference<Map<String,List<Bean>>>(){}
来调用T
- the object type will be returnbuffer
- the byte contenttr
- type referenceT
public <T> T update(String content, T object)
T
- the object type will be returncontent
- contentobject
- objectT
public <T> String toJsonP(String functionName, T data)
T
- the data typefunctionName
- the function namedata
- objectpublic void enableEnumUseToString()
public com.fasterxml.jackson.databind.ObjectMapper getMapper()
ObjectMapper
Copyright © 2016. All rights reserved.