Module bus.extra

Class JacksonProvider

java.lang.Object
org.miaixz.bus.extra.json.provider.AbstractJsonProvider
org.miaixz.bus.extra.json.provider.JacksonProvider
All Implemented Interfaces:
Serializable, org.miaixz.bus.core.Provider, JsonProvider

public class JacksonProvider extends AbstractJsonProvider
A JsonProvider implementation based on the Jackson library. This class provides JSON serialization and deserialization functionalities using Jackson's ObjectMapper.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • objectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper objectMapper
      The underlying Jackson ObjectMapper used for JSON operations.
  • Constructor Details

    • JacksonProvider

      public JacksonProvider()
      Constructs a new JacksonProvider instance. Initializes an ObjectMapper with module auto-detection and disables writing dates as timestamps.
  • Method Details

    • toJsonString

      public String toJsonString(Object object)
      Description copied from interface: JsonProvider
      Converts an object into its JSON string representation.
      Parameters:
      object - The object to be serialized.
      Returns:
      The JSON string representation of the object.
    • toJsonString

      public String toJsonString(Object object, String format)
      Description copied from interface: JsonProvider
      Converts an object into its JSON string representation, with a specified date format.
      Parameters:
      object - The object to be serialized.
      format - The date format string to use for date/time objects, e.g., "yyyy-MM-dd HH:mm:ss".
      Returns:
      The JSON string representation of the object.
    • toPojo

      public <T> T toPojo(String json, Class<T> valueType)
      Description copied from interface: JsonProvider
      Parses a JSON string into an object of the specified class.
      Type Parameters:
      T - The type of the target object.
      Parameters:
      json - The JSON string to be deserialized.
      valueType - The class of the target object.
      Returns:
      The deserialized object.
    • toPojo

      public <T> T toPojo(Map fromMap, Class<T> clazz)
      Description copied from interface: JsonProvider
      Converts a Map into a plain old Java object (POJO) of the specified class.
      Type Parameters:
      T - The type of the target POJO.
      Parameters:
      fromMap - The source map.
      clazz - The class of the target POJO.
      Returns:
      The POJO converted from the map.
    • toList

      public <T> List<T> toList(String json)
      Description copied from interface: JsonProvider
      Parses a JSON string into a List.
      Type Parameters:
      T - The generic type of the elements in the list.
      Parameters:
      json - The JSON string to be deserialized.
      Returns:
      The resulting List.
    • toList

      public <T> List<T> toList(String json, Class<T> clazz)
      Description copied from interface: JsonProvider
      Parses a JSON string into a List of objects of the specified class.
      Type Parameters:
      T - The type of the elements in the list.
      Parameters:
      json - The JSON string to be deserialized.
      clazz - The class of the elements in the list.
      Returns:
      The resulting List.
    • toList

      public <T> List<T> toList(String json, Type type)
      Description copied from interface: JsonProvider
      Parses a JSON string into a List of a specific generic type.
      Type Parameters:
      T - The generic type of the elements in the list.
      Parameters:
      json - The JSON string to be deserialized.
      type - The Type representing the list's generic type.
      Returns:
      The resulting List.
    • toMap

      public <K, V> Map<K,V> toMap(String json)
      Description copied from interface: JsonProvider
      Parses a JSON string into a Map.
      Type Parameters:
      K - The type of the keys in the map.
      V - The type of the values in the map.
      Parameters:
      json - The JSON string to be deserialized.
      Returns:
      The resulting Map.
    • toMap

      public <K, V> Map<K,V> toMap(Object object)
      Description copied from interface: JsonProvider
      Converts an object into a Map.
      Type Parameters:
      K - The type of the keys in the map.
      V - The type of the values in the map.
      Parameters:
      object - The object to be converted.
      Returns:
      The resulting Map.
    • getValue

      public <T> T getValue(String json, String field)
      Description copied from interface: JsonProvider
      Extracts the value of a specific field from a JSON string.
      Type Parameters:
      T - The type of the value to be returned.
      Parameters:
      json - The JSON string to be parsed.
      field - The name of the field whose value is to be extracted.
      Returns:
      The value of the specified field.
    • isJson

      public boolean isJson(String json)
      Description copied from interface: JsonProvider
      Checks if a given string is a valid, well-formed JSON string.
      Parameters:
      json - The string to be checked.
      Returns:
      true if the string is a valid JSON, false otherwise.