Module bus.extra

Interface JsonProvider

All Superinterfaces:
org.miaixz.bus.core.Provider, Serializable
All Known Implementing Classes:
AbstractJsonProvider, FastJsonProvider, GsonProvider, JacksonProvider

public interface JsonProvider extends org.miaixz.bus.core.Provider
Defines the contract for a JSON service provider. This interface specifies a set of common methods for JSON serialization and deserialization, allowing for different underlying JSON libraries (e.g., Jackson, Gson, Fastjson) to be used interchangeably.
Since:
Java 17+
Author:
Kimi Liu
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    getValue(String json, String field)
    Extracts the value of a specific field from a JSON string.
    boolean
    isJson(String json)
    Checks if a given string is a valid, well-formed JSON string.
    Converts an object into its JSON string representation.
    toJsonString(Object object, String format)
    Converts an object into its JSON string representation, with a specified date format.
    <T> List<T>
    toList(String json)
    Parses a JSON string into a List.
    <T> List<T>
    toList(String json, Class<T> clazz)
    Parses a JSON string into a List of objects of the specified class.
    <T> List<T>
    toList(String json, Type type)
    Parses a JSON string into a List of a specific generic type.
    <K, V> Map<K,V>
    toMap(Object object)
    Converts an object into a Map.
    <K, V> Map<K,V>
    toMap(String json)
    Parses a JSON string into a Map.
    <T> T
    toPojo(String json, Class<T> clazz)
    Parses a JSON string into an object of the specified class.
    <T> T
    toPojo(Map map, Class<T> clazz)
    Converts a Map into a plain old Java object (POJO) of the specified class.
    default Object
    Returns the type of this JSON provider.
  • Method Details

    • toJsonString

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

      String toJsonString(Object object, String format)
      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

      <T> T toPojo(String json, Class<T> clazz)
      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.
      clazz - The class of the target object.
      Returns:
      The deserialized object.
    • toPojo

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

      <T> List<T> toList(String json)
      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

      <T> List<T> toList(String json, Class<T> clazz)
      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

      <T> List<T> toList(String json, Type type)
      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

      <K, V> Map<K,V> toMap(String json)
      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

      <K, V> Map<K,V> toMap(Object object)
      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

      <T> T getValue(String json, String field)
      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

      boolean isJson(String json)
      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.
    • type

      default Object type()
      Returns the type of this JSON provider.
      Specified by:
      type in interface org.miaixz.bus.core.Provider
      Returns:
      The provider type, which is EnumValue.Povider.JSON.