Package one.tranic.t.base.parse.json
Class JsonParser
java.lang.Object
one.tranic.t.base.parse.json.JsonParser
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TParses the JSON content from the providedReaderinto an object of the specified type.static com.google.gson.Gsongson()static <T> TrequestAndParse(HttpURLConnection connection, Class<T> classOfT) Sends a request using the given HttpURLConnection and parses the response into the specified class type.static <T> TrequestAndParse(HttpURLConnection connection, Type typeOfT) Sends an HTTP request using the provided connection, reads the response stream, and parses it into an object of the specified type.static StringConverts the given object into its JSON representation.
-
Constructor Details
-
JsonParser
public JsonParser()
-
-
Method Details
-
gson
public static com.google.gson.Gson gson() -
requestAndParse
Sends an HTTP request using the provided connection, reads the response stream, and parses it into an object of the specified type.- Type Parameters:
T- the type of the object to return after parsing- Parameters:
connection- theHttpURLConnectioninstance used to send the requesttypeOfT- theTypeof the object to parse the response into- Returns:
- the parsed object of the specified type
- Throws:
IOException- if an error occurs while reading the response or closing resources
-
requestAndParse
public static <T> T requestAndParse(HttpURLConnection connection, Class<T> classOfT) throws IOException Sends a request using the given HttpURLConnection and parses the response into the specified class type.- Type Parameters:
T- the type of the object to be returned- Parameters:
connection- the HttpURLConnection instance used to perform the requestclassOfT- the class of the object to parse the response into- Returns:
- an instance of the specified class type containing the parsed response
- Throws:
IOException- if an I/O error occurs while making the request or reading the response
-
fromJson
public static <T> T fromJson(@NotNull @NotNull Reader json, @NotNull @NotNull Class<T> classOfT) throws com.google.gson.JsonSyntaxException, com.google.gson.JsonIOException Parses the JSON content from the providedReaderinto an object of the specified type.- Type Parameters:
T- the type of the object to be returned- Parameters:
json- theReadercontaining the JSON contentclassOfT- theClassof the object to parse the JSON into- Returns:
- an instance of the specified class type populated with the parsed data
- Throws:
com.google.gson.JsonSyntaxException- if the JSON is not in the expected formatcom.google.gson.JsonIOException- if an error occurs while reading the JSON data
-
toJson
Converts the given object into its JSON representation.- Parameters:
src- the object to be converted to JSON; can be null- Returns:
- a JSON-formatted string representing the given object or "null" if the input is null
-