类 JacksonJsonCodec

java.lang.Object
io.agentscope.core.util.JacksonJsonCodec
所有已实现的接口:
JsonCodec

public class JacksonJsonCodec extends Object implements JsonCodec
Jackson-based implementation of JsonCodec.

This is the default implementation used by JsonUtils. It uses Jackson's ObjectMapper with the following configuration:

  • FAIL_ON_UNKNOWN_PROPERTIES = false - allows unknown fields in JSON

Users can access the underlying ObjectMapper via getObjectMapper() for advanced operations not covered by the JsonCodec interface.

另请参阅:
  • 构造器概要

    构造器
    构造器
    说明
    Creates a new JacksonJsonCodec with default ObjectMapper configuration.
    JacksonJsonCodec(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Creates a new JacksonJsonCodec with a custom ObjectMapper.
  • 方法概要

    修饰符和类型
    方法
    说明
    <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.
    com.fasterxml.jackson.databind.ObjectMapper
    Get the underlying ObjectMapper for advanced operations.
    Serialize an object to JSON string.
    Serialize an object to pretty-printed JSON string.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

    • JacksonJsonCodec

      public JacksonJsonCodec()
      Creates a new JacksonJsonCodec with default ObjectMapper configuration.
    • JacksonJsonCodec

      public JacksonJsonCodec(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Creates a new JacksonJsonCodec with a custom ObjectMapper.
      参数:
      objectMapper - the ObjectMapper to use
  • 方法详细资料

    • getObjectMapper

      public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Get the underlying ObjectMapper for advanced operations.
      返回:
      the ObjectMapper instance
    • toJson

      public String toJson(Object obj)
      从接口复制的说明: JsonCodec
      Serialize an object to JSON string.
      指定者:
      toJson 在接口中 JsonCodec
      参数:
      obj - the object to serialize
      返回:
      JSON string representation
    • toPrettyJson

      public String toPrettyJson(Object obj)
      从接口复制的说明: JsonCodec
      Serialize an object to pretty-printed JSON string.
      指定者:
      toPrettyJson 在接口中 JsonCodec
      参数:
      obj - the object to serialize
      返回:
      pretty-printed JSON string representation
    • fromJson

      public <T> T fromJson(String json, Class<T> type)
      从接口复制的说明: JsonCodec
      Deserialize a JSON string to an object of the specified type.
      指定者:
      fromJson 在接口中 JsonCodec
      类型参数:
      T - the type parameter
      参数:
      json - the JSON string to deserialize
      type - the target class type
      返回:
      deserialized object
    • fromJson

      public <T> T fromJson(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      从接口复制的说明: JsonCodec
      Deserialize a JSON string to an object with generic type.

      Use this method when deserializing to generic types like List<MyClass>.

      指定者:
      fromJson 在接口中 JsonCodec
      类型参数:
      T - the type parameter
      参数:
      json - the JSON string to deserialize
      typeRef - the type reference for generic types
      返回:
      deserialized object
    • convertValue

      public <T> T convertValue(Object from, Class<T> toType)
      从接口复制的说明: JsonCodec
      Convert an object to another type.

      This is useful for converting Map to typed objects or vice versa.

      指定者:
      convertValue 在接口中 JsonCodec
      类型参数:
      T - the type parameter
      参数:
      from - the source object
      toType - the target class type
      返回:
      converted object
    • convertValue

      public <T> T convertValue(Object from, com.fasterxml.jackson.core.type.TypeReference<T> toTypeRef)
      从接口复制的说明: JsonCodec
      Convert an object to another generic type.
      指定者:
      convertValue 在接口中 JsonCodec
      类型参数:
      T - the type parameter
      参数:
      from - the source object
      toTypeRef - the type reference for generic types
      返回:
      converted object
    • convertValue

      public Object convertValue(Object from, Type toType)
      从接口复制的说明: JsonCodec
      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);
       
      指定者:
      convertValue 在接口中 JsonCodec
      参数:
      from - the source object
      toType - the target type (can be Class, ParameterizedType, etc.)
      返回:
      converted object