接口 JsonCodec
- 所有已知实现类:
JacksonJsonCodec
public interface JsonCodec
Interface for JSON serialization and deserialization operations.
This interface provides a unified abstraction for JSON processing, allowing users to replace the default Jackson-based implementation with custom implementations if needed.
Usage:
// Get the codec instance
JsonCodec codec = JsonUtils.getJsonCodec();
// Serialize object to JSON
String json = codec.toJson(myObject);
// Deserialize JSON to object
MyClass obj = codec.fromJson(json, MyClass.class);
// Deserialize with generic type
List<MyClass> list = codec.fromJson(json, new TypeReference<List<MyClass>>() {});
- 另请参阅:
-
方法概要
修饰符和类型方法说明<T> TconvertValue(Object from, com.fasterxml.jackson.core.type.TypeReference<T> toTypeRef) Convert an object to another generic type.<T> TconvertValue(Object from, Class<T> toType) Convert an object to another type.convertValue(Object from, Type toType) Convert an object to another type using Java reflection Type.<T> TDeserialize a JSON string to an object with generic type.<T> TDeserialize a JSON string to an object of the specified type.Serialize an object to JSON string.toPrettyJson(Object obj) Serialize an object to pretty-printed JSON string.
-
方法详细资料
-
toJson
Serialize an object to JSON string.- 参数:
obj- the object to serialize- 返回:
- JSON string representation
- 抛出:
JsonException- if serialization fails
-
toPrettyJson
Serialize an object to pretty-printed JSON string.- 参数:
obj- the object to serialize- 返回:
- pretty-printed JSON string representation
- 抛出:
JsonException- if serialization fails
-
fromJson
Deserialize a JSON string to an object of the specified type.- 类型参数:
T- the type parameter- 参数:
json- the JSON string to deserializetype- the target class type- 返回:
- deserialized object
- 抛出:
JsonException- if deserialization fails
-
fromJson
Deserialize a JSON string to an object with generic type.Use this method when deserializing to generic types like
List<MyClass>.- 类型参数:
T- the type parameter- 参数:
json- the JSON string to deserializetypeRef- the type reference for generic types- 返回:
- deserialized object
- 抛出:
JsonException- if deserialization fails
-
convertValue
Convert an object to another type.This is useful for converting Map to typed objects or vice versa.
- 类型参数:
T- the type parameter- 参数:
from- the source objecttoType- the target class type- 返回:
- converted object
- 抛出:
JsonException- if conversion fails
-
convertValue
Convert an object to another generic type.- 类型参数:
T- the type parameter- 参数:
from- the source objecttoTypeRef- the type reference for generic types- 返回:
- converted object
- 抛出:
JsonException- if conversion fails
-
convertValue
Convert an object to another type using Java reflection Type.This method preserves generic type information from parameterized types such as
List<MyClass>orMap<String, MyClass>.Example usage:
// For a method parameter declared as List<OrderItem> Type paramType = parameter.getParameterizedType(); List<OrderItem> items = (List<OrderItem>) codec.convertValue(value, paramType);- 参数:
from- the source objecttoType- the target type (can be Class, ParameterizedType, etc.)- 返回:
- converted object
- 抛出:
JsonException- if conversion fails
-