Class Json
Json class provides JSON parsing, generation and bean
conversion.
Parsing is strict according to RFC 8259 and produces plain Java
values: JsonObject, JsonArray, String,
Long, Double, Boolean and null. Integral
numbers that don't fit into a Long become Double values,
which means that they can lose precision. When an object contains the
same member name multiple times, the last value wins.
For instance:
var config = Json.parseObject("""
{"name": "my-app", "port": 8080}""");
var port = config.getInt("port", 80);
Beans are converted in both directions with from(java.lang.Object) and
toBean(rife.json.JsonObject, java.lang.Class<T>), relying on the same property conventions as the rest of
RIFE2. Records are converted through their components in the same
fashion.
- Since:
- 1.10
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic JsonObjectCreates a JSON object from the properties of a bean.static JsonArrayfrom(Collection<?> elements) Creates a JSON array from the elements of a collection.static JsonObjectfromExcluded(Object bean, String... excludedProperties) Creates a JSON object from the properties of a bean, excluding particular properties.static ObjectParses a JSON document from a reader.static ObjectParses a JSON document.static JsonArrayparseArray(Reader reader) Parses a JSON document from a reader that is expected to be a JSON array.static JsonArrayparseArray(String json) Parses a JSON document that is expected to be a JSON array.static JsonObjectparseObject(Reader reader) Parses a JSON document from a reader that is expected to be a JSON object.static JsonObjectparseObject(String json) Parses a JSON document that is expected to be a JSON object.static <T> TtoBean(JsonObject json, Class<T> beanClass) Fills a new bean instance with the members of a JSON object.static <T> List<T>toBeanList(JsonArray json, Class<T> beanClass) Fills a list of new bean instances with the members of the JSON objects in a JSON array.static StringtoPrettyString(Object value) Serializes any JSON value into its multi-line indented string representation.static StringSerializes any JSON value into its compact string representation.
-
Method Details
-
parse
Parses a JSON document.This method will parse any JSON document and hand you back whichever value sits at the top level of it.
- Parameters:
json- the JSON document to parse- Returns:
- the parsed value: a
JsonObject,JsonArray,String,Long,Double,Booleanornull - Throws:
JsonParseException- when the document couldn't be parsed- Since:
- 1.10
- See Also:
-
parse
Parses a JSON document from a reader.The reader will be read out entirely before the parsing starts.
- Parameters:
reader- the reader to parse the JSON document from- Returns:
- the parsed value
- Throws:
JsonParseException- when the document couldn't be parsedIOException- when an error occurred while reading- Since:
- 1.10
- See Also:
-
parseObject
Parses a JSON document that is expected to be a JSON object.This method will refuse any document whose top-level value isn't a JSON object, so that you don't have to check the type of the result yourself.
- Parameters:
json- the JSON document to parse- Returns:
- the parsed
JsonObject - Throws:
JsonParseException- when the document couldn't be parsed or isn't a JSON object- Since:
- 1.10
- See Also:
-
parseObject
Parses a JSON document from a reader that is expected to be a JSON object.The reader will be read out entirely before the parsing starts.
- Parameters:
reader- the reader to parse the JSON document from- Returns:
- the parsed
JsonObject - Throws:
JsonParseException- when the document couldn't be parsed or isn't a JSON objectIOException- when an error occurred while reading- Since:
- 1.10
- See Also:
-
parseArray
Parses a JSON document that is expected to be a JSON array.This method will refuse any document whose top-level value isn't a JSON array, so that you don't have to check the type of the result yourself.
- Parameters:
json- the JSON document to parse- Returns:
- the parsed
JsonArray - Throws:
JsonParseException- when the document couldn't be parsed or isn't a JSON array- Since:
- 1.10
- See Also:
-
parseArray
Parses a JSON document from a reader that is expected to be a JSON array.The reader will be read out entirely before the parsing starts.
- Parameters:
reader- the reader to parse the JSON document from- Returns:
- the parsed
JsonArray - Throws:
JsonParseException- when the document couldn't be parsed or isn't a JSON arrayIOException- when an error occurred while reading- Since:
- 1.10
- See Also:
-
from
Creates a JSON object from the properties of a bean.Properties that are constrained as not
serializedare not included. Records are converted through their components instead of through their properties.- Parameters:
bean- the bean to convert- Returns:
- the JSON object with the bean's properties as members
- Since:
- 1.10
- See Also:
-
fromExcluded
Creates a JSON object from the properties of a bean, excluding particular properties.This method behaves exactly like
from(Object), apart from the properties whose names you provide, which are left out of the resulting JSON object.- Parameters:
bean- the bean to convertexcludedProperties- the names of the properties to exclude- Returns:
- the JSON object with the bean's properties as members
- Since:
- 1.10
- See Also:
-
from
Creates a JSON array from the elements of a collection.The beans and records that are amongst the elements are converted to JSON objects, just like
from(Object)does.- Parameters:
elements- the collection to convert- Returns:
- the JSON array with the converted elements
- Since:
- 1.10
- See Also:
-
toString
Serializes any JSON value into its compact string representation.This method accepts strings, numbers, booleans,
null, maps, collections, arrays, dates, temporals, enums,JsonObject,JsonArray, and beans or records, including the beans that are nested inside maps, collections and arrays, since those are automatically converted withfrom(java.lang.Object).- Parameters:
value- the value to serialize- Returns:
- the JSON string
- Since:
- 1.10
- See Also:
-
toPrettyString
Serializes any JSON value into its multi-line indented string representation.This method accepts the same values as
toString(Object).- Parameters:
value- the value to serialize- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
- See Also:
-
toBean
Fills a new bean instance with the members of a JSON object.Members without a matching bean property are ignored, while nested JSON objects are converted to the types of their matching bean properties. The elements of arrays, collections and maps are converted to the component and generic types that the property setters declare.
Properties that are constrained as not
serializedor noteditableare never filled in.Records are instantiated through their canonical constructor with the members that match their components, while members that are absent become
nullor the primitive default values.- Parameters:
json- the JSON object with the values to fill inbeanClass- the class of the bean to fill- Returns:
- the filled bean instance
- Since:
- 1.10
- See Also:
-
toBeanList
Fills a list of new bean instances with the members of the JSON objects in a JSON array.Each element is converted with the same rules as
toBean(rife.json.JsonObject, java.lang.Class<T>), while elements that arenullstaynull.- Parameters:
json- the JSON array with the JSON objects to convertbeanClass- the class of the beans to fill- Returns:
- the list with a bean instance for each element
- Since:
- 1.10
- See Also:
-