Class JsonParser

java.lang.Object
one.tranic.t.base.parse.json.JsonParser

public class JsonParser extends Object
  • Constructor Details

    • JsonParser

      public JsonParser()
  • Method Details

    • gson

      public static com.google.gson.Gson gson()
    • requestAndParse

      public static <T> T requestAndParse(HttpURLConnection connection, Type typeOfT) throws IOException
      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 - the HttpURLConnection instance used to send the request
      typeOfT - the Type of 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 request
      classOfT - 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 provided Reader into an object of the specified type.
      Type Parameters:
      T - the type of the object to be returned
      Parameters:
      json - the Reader containing the JSON content
      classOfT - the Class of 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 format
      com.google.gson.JsonIOException - if an error occurs while reading the JSON data
    • toJson

      public static String toJson(@Nullable @Nullable Object src)
      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