java.lang.Object
org.miaixz.bus.extra.json.provider.AbstractJsonProvider
org.miaixz.bus.extra.json.provider.GsonProvider
- All Implemented Interfaces:
Serializable,org.miaixz.bus.core.Provider,JsonProvider
A
JsonProvider implementation based on Google's Gson library. This class provides
JSON serialization and deserialization functionalities using Gson.- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic com.google.gson.GsonThe underlying Gson instance used for JSON operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TExtracts the value of a specific field from a JSON string.booleanChecks if a given string is a valid, well-formed JSON string.toJsonString(Object object) 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> Parses a JSON string into aList.<T> List<T> Parses a JSON string into aListof objects of the specified class.<T> List<T> Parses a JSON string into aListof a specific generic type.<K,V> Map <K, V> Converts an object into aMap.<K,V> Map <K, V> Parses a JSON string into aMap.<T> TParses a JSON string into an object of the specified class.<T> TConverts aMapinto a plain old Java object (POJO) of the specified class.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.miaixz.bus.extra.json.JsonProvider
type
-
Field Details
-
gson
public static com.google.gson.Gson gsonThe underlying Gson instance used for JSON operations. It is configured with custom type adapters.
-
-
Constructor Details
-
GsonProvider
public GsonProvider()Constructs a newGsonProviderinstance. Initializes aGsoninstance with custom type adapters to handle potential issues, such as integers being converted to floating-point numbers during deserialization.
-
-
Method Details
-
toJsonString
Description copied from interface:JsonProviderConverts an object into its JSON string representation.- Parameters:
object- The object to be serialized.- Returns:
- The JSON string representation of the object.
-
toJsonString
Description copied from interface:JsonProviderConverts 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
Description copied from interface:JsonProviderParses 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
Description copied from interface:JsonProviderConverts aMapinto 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
Description copied from interface:JsonProviderParses a JSON string into aList.- 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
Description copied from interface:JsonProviderParses a JSON string into aListof 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
Description copied from interface:JsonProviderParses a JSON string into aListof a specific generic type. -
toMap
Description copied from interface:JsonProviderParses a JSON string into aMap.- 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
Description copied from interface:JsonProviderConverts an object into aMap.- 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
Description copied from interface:JsonProviderExtracts 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
Description copied from interface:JsonProviderChecks if a given string is a valid, well-formed JSON string.- Parameters:
json- The string to be checked.- Returns:
trueif the string is a valid JSON,falseotherwise.
-