public class JacksonMapper
extends com.fasterxml.jackson.databind.ObjectMapper
Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解。 // @ JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。 // @ JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。 // @ JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。 // http://blog.csdn.net/jesse621/article/details/22698565
| 限定符和类型 | 字段和说明 |
|---|---|
private static org.slf4j.Logger |
logger |
private static JacksonMapper |
mapper |
_configOverrides, _deserializationConfig, _deserializationContext, _injectableValues, _jsonFactory, _mixIns, _registeredModuleTypes, _rootDeserializers, _serializationConfig, _serializerFactory, _serializerProvider, _subtypeResolver, _typeFactory, DEFAULT_ANNOTATION_INTROSPECTOR, DEFAULT_BASE| 构造器和说明 |
|---|
JacksonMapper() |
JacksonMapper(com.fasterxml.jackson.annotation.JsonInclude.Include include) |
| 限定符和类型 | 方法和说明 |
|---|---|
static com.fasterxml.jackson.databind.node.ObjectNode |
createObjectNode(Map<String,String> map)
创建 节点 对象
|
JacksonMapper |
enableSimple()
允许单引号
允许不带引号的字段名称
|
static <T> T |
fromJson(byte[] json,
Class<T> clazz)
读取JSON数据: 从 byte[]
|
static <T> T |
fromJson(File json,
Class<T> valueType)
读取JSON数据: 从文件
|
static <T> T |
fromJson(InputStream json,
Class<T> valueType)
读取JSON数据: 从 InputStream
|
static <T> T |
fromJson(String json,
Class<T> valueType)
读取JSON数据: 从字符串
|
static <T> T |
fromJson(String json,
com.fasterxml.jackson.databind.JavaType javaType)
该方法有时有问题
|
static com.fasterxml.jackson.databind.JavaType |
getCollectionType(Class<?> collectionClass,
Class<?>... elementClasses)
获取泛型的Collection Type
|
static JacksonMapper |
getMapper()
返回mapper,忽略未知参数、使用long解析时间
|
static <T> T |
jsonToList(String json,
Class<T> valueType)
将 json 转为 List, 该方法有时有问题
|
static void |
main(String[] args) |
static JacksonMapper |
nonDefaultMapper()
创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。
|
static String |
toJson(Object obj)
java对象转换成JSON字符串
java对象、List、Map |
static com.fasterxml.jackson.databind.JsonNode |
toNode(String content)
JSON 字符串解析为 JsonNode
|
static <T> void |
writeJson(File file,
T value)
写入JSON数据:到 文件
|
_checkInvalidCopy, _configAndWriteValue, _convert, _findRootDeserializer, _initForReading, _initForReading, _newReader, _newReader, _newWriter, _newWriter, _newWriter, _readMapAndClose, _readTreeAndClose, _readValue, _serializerProvider, _unwrapAndDeserialize, _verifyNoTrailingTokens, _verifySchemaType, acceptJsonFormatVisitor, acceptJsonFormatVisitor, addHandler, addMixIn, addMixInAnnotations, canDeserialize, canDeserialize, canSerialize, canSerialize, clearProblemHandlers, configOverride, configure, configure, configure, configure, configure, constructType, convertValue, convertValue, convertValue, copy, createArrayNode, createDeserializationContext, createObjectNode, defaultClassIntrospector, disable, disable, disable, disable, disable, disable, disable, disableDefaultTyping, enable, enable, enable, enable, enable, enable, enable, enableDefaultTyping, enableDefaultTyping, enableDefaultTyping, enableDefaultTypingAsProperty, findAndRegisterModules, findMixInClassFor, findModules, findModules, generateJsonSchema, getDateFormat, getDeserializationConfig, getDeserializationContext, getFactory, getInjectableValues, getJsonFactory, getNodeFactory, getPropertyNamingStrategy, getRegisteredModuleIds, getSerializationConfig, getSerializerFactory, getSerializerProvider, getSerializerProviderInstance, getSubtypeResolver, getTypeFactory, getVisibilityChecker, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, mixInCount, reader, reader, reader, reader, reader, reader, reader, reader, reader, reader, reader, readerFor, readerFor, readerFor, readerForUpdating, readerWithView, readTree, readTree, readTree, readTree, readTree, readTree, readTree, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValue, readValues, readValues, readValues, readValues, registerModule, registerModules, registerModules, registerSubtypes, registerSubtypes, registerSubtypes, setAnnotationIntrospector, setAnnotationIntrospectors, setBase64Variant, setConfig, setConfig, setDateFormat, setDefaultMergeable, setDefaultPrettyPrinter, setDefaultPropertyInclusion, setDefaultPropertyInclusion, setDefaultSetterInfo, setDefaultTyping, setDefaultVisibility, setFilterProvider, setFilters, setHandlerInstantiator, setInjectableValues, setLocale, setMixInAnnotations, setMixInResolver, setMixIns, setNodeFactory, setPropertyInclusion, setPropertyNamingStrategy, setSerializationInclusion, setSerializerFactory, setSerializerProvider, setSubtypeResolver, setTimeZone, setTypeFactory, setVisibility, setVisibility, setVisibilityChecker, treeAsTokens, treeToValue, updateValue, valueToTree, version, writer, writer, writer, writer, writer, writer, writer, writer, writer, writer, writerFor, writerFor, writerFor, writerWithDefaultPrettyPrinter, writerWithType, writerWithType, writerWithType, writerWithView, writeTree, writeTree, writeValue, writeValue, writeValue, writeValue, writeValue, writeValueAsBytes, writeValueAsStringprivate static org.slf4j.Logger logger
private static JacksonMapper mapper
public JacksonMapper()
public JacksonMapper(com.fasterxml.jackson.annotation.JsonInclude.Include include)
public JacksonMapper enableSimple()
public static JacksonMapper getMapper()
public static JacksonMapper nonDefaultMapper()
public static <T> T fromJson(byte[] json,
Class<T> clazz)
json - clazz - public static <T> T fromJson(InputStream json, Class<T> valueType)
json - valueType - public static <T> T fromJson(String json, Class<T> valueType)
json - valueType - public static <T> T fromJson(File json, Class<T> valueType)
json - valueType - public static <T> T fromJson(String json, com.fasterxml.jackson.databind.JavaType javaType) throws Exception
json - javaType - Exceptionpublic static <T> T jsonToList(String json, Class<T> valueType) throws Exception
json - valueType - 对象类型Exceptionpublic static com.fasterxml.jackson.databind.JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses)
collectionClass - 泛型的CollectionelementClasses - 元素类public static <T> void writeJson(File file, T value)
file - value - public static com.fasterxml.jackson.databind.JsonNode toNode(String content) throws IOException
content - IOExceptionpublic static com.fasterxml.jackson.databind.node.ObjectNode createObjectNode(Map<String,String> map)
map - public static void main(String[] args)
Copyright © 2024. All rights reserved.