接口 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> T
    convertValue(Object from, com.fasterxml.jackson.core.type.TypeReference<T> toTypeRef)
    Convert an object to another generic type.
    <T> T
    convertValue(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> T
    fromJson(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
    Deserialize a JSON string to an object with generic type.
    <T> T
    fromJson(String json, Class<T> type)
    Deserialize a JSON string to an object of the specified type.
    Serialize an object to JSON string.
    Serialize an object to pretty-printed JSON string.
  • 方法详细资料

    • toJson

      String toJson(Object obj)
      Serialize an object to JSON string.
      参数:
      obj - the object to serialize
      返回:
      JSON string representation
      抛出:
      JsonException - if serialization fails
    • toPrettyJson

      String toPrettyJson(Object obj)
      Serialize an object to pretty-printed JSON string.
      参数:
      obj - the object to serialize
      返回:
      pretty-printed JSON string representation
      抛出:
      JsonException - if serialization fails
    • fromJson

      <T> T fromJson(String json, Class<T> type)
      Deserialize a JSON string to an object of the specified type.
      类型参数:
      T - the type parameter
      参数:
      json - the JSON string to deserialize
      type - the target class type
      返回:
      deserialized object
      抛出:
      JsonException - if deserialization fails
    • fromJson

      <T> T fromJson(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      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 deserialize
      typeRef - the type reference for generic types
      返回:
      deserialized object
      抛出:
      JsonException - if deserialization fails
    • convertValue

      <T> T convertValue(Object from, Class<T> toType)
      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 object
      toType - the target class type
      返回:
      converted object
      抛出:
      JsonException - if conversion fails
    • convertValue

      <T> T convertValue(Object from, com.fasterxml.jackson.core.type.TypeReference<T> toTypeRef)
      Convert an object to another generic type.
      类型参数:
      T - the type parameter
      参数:
      from - the source object
      toTypeRef - the type reference for generic types
      返回:
      converted object
      抛出:
      JsonException - if conversion fails
    • convertValue

      Object convertValue(Object from, Type toType)
      Convert an object to another type using Java reflection Type.

      This method preserves generic type information from parameterized types such as List<MyClass> or Map<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 object
      toType - the target type (can be Class, ParameterizedType, etc.)
      返回:
      converted object
      抛出:
      JsonException - if conversion fails