Class Json

java.lang.Object
br.dev.rplus.cup.utils.Json

public class Json extends Object
Utility class for working with JSON data. Provides methods to convert between JSON strings and Java objects, as well as to validate and format JSON data.
  • Method Details

    • mapToJson

      public static String mapToJson(Map<String,Object> map)
      Converts a Map to a JSON string.
      Parameters:
      map - the Map to convert.
      Returns:
      the JSON string representation of the Map.
    • jsonToMap

      public static Map<String,Object> jsonToMap(String json)
      Converts a JSON string to a Map.
      Parameters:
      json - the JSON string to convert.
      Returns:
      the Map representation of the JSON.
    • toJson

      public static String toJson(Object object) throws org.json.JSONException
      Converts a Java object to a JSON string.

      The object can be a Map or an Iterable. Other types are not supported.

      Parameters:
      object - the Java object to convert (must be of a type supported by JSONObject or JSONArray).
      Returns:
      the JSON string representation of the object.
      Throws:
      org.json.JSONException - if the object cannot be converted to JSON.
    • parseJsonObject

      public static org.json.JSONObject parseJsonObject(String json) throws org.json.JSONException
      Converts a JSON string to a JSONObject.
      Parameters:
      json - the JSON string to convert.
      Returns:
      the JSONObject representation of the JSON string.
      Throws:
      org.json.JSONException - if the JSON is invalid.
    • parseJsonArray

      public static org.json.JSONArray parseJsonArray(String json) throws org.json.JSONException
      Converts a JSON string to a JSONArray.
      Parameters:
      json - the JSON string to convert.
      Returns:
      the JSONArray representation of the JSON string.
      Throws:
      org.json.JSONException - if the JSON is invalid.
    • prettyPrintJson

      public static String prettyPrintJson(String json) throws org.json.JSONException
      Pretty prints a JSON string with indentation.
      Parameters:
      json - the JSON string to pretty print.
      Returns:
      the pretty-printed JSON string.
      Throws:
      org.json.JSONException - if the JSON is invalid.
    • isValidJson

      public static boolean isValidJson(String json)
      Checks if a string is a valid JSON.

      The string is considered valid if it can be parsed as either a JSONObject or a JSONArray.

      Parameters:
      json - the string to check.
      Returns:
      true if the string is a valid JSON, false otherwise.