Package br.dev.rplus.cup.utils
Class Json
java.lang.Object
br.dev.rplus.cup.utils.Json
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 Summary
Modifier and TypeMethodDescriptionstatic boolean
isValidJson
(String json) Checks if a string is a valid JSON.Converts a JSON string to aMap
.static String
Converts aMap
to a JSON string.static org.json.JSONArray
parseJsonArray
(String json) Converts a JSON string to aJSONArray
.static org.json.JSONObject
parseJsonObject
(String json) Converts a JSON string to aJSONObject
.static String
prettyPrintJson
(String json) Pretty prints a JSON string with indentation.static String
Converts a Java object to a JSON string.
-
Method Details
-
mapToJson
Converts aMap
to a JSON string. -
jsonToMap
Converts a JSON string to aMap
.- Parameters:
json
- the JSON string to convert.- Returns:
- the
Map
representation of the JSON.
-
toJson
Converts a Java object to a JSON string.The object can be a
Map
or anIterable
. Other types are not supported.- Parameters:
object
- the Java object to convert (must be of a type supported byJSONObject
orJSONArray
).- Returns:
- the JSON string representation of the object.
- Throws:
org.json.JSONException
- if the object cannot be converted to JSON.
-
parseJsonObject
Converts a JSON string to aJSONObject
.- 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
Converts a JSON string to aJSONArray
.- 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
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
Checks if a string is a valid JSON.The string is considered valid if it can be parsed as either a
JSONObject
or aJSONArray
.- Parameters:
json
- the string to check.- Returns:
true
if the string is a valid JSON,false
otherwise.
-