Class Json
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 a Long become Double values and 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 convert 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
convert through their components in the same fashion.
- Since:
- 1.10
-
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, converting beans and records to JSON objects likefrom(Object)does.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.- 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
-
parse
Parses a JSON document from a reader.The reader is read fully before 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
-
parseObject
Parses a JSON document that is expected to be a JSON object.- 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
-
parseObject
Parses a JSON document from a reader that is expected to be a JSON object.- 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
-
parseArray
Parses a JSON document that is expected to be a JSON array.- 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
-
parseArray
Parses a JSON document from a reader that is expected to be a JSON array.- 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
-
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 properties.- Parameters:
bean- the bean to convert- Returns:
- the JSON object with the bean's properties as members
- Since:
- 1.10
-
fromExcluded
Creates a JSON object from the properties of a bean, excluding particular properties.- 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
-
from
Creates a JSON array from the elements of a collection, converting beans and records to JSON objects likefrom(Object)does.- Parameters:
elements- the collection to convert- Returns:
- the JSON array with the converted elements
- Since:
- 1.10
-
toString
Serializes any JSON value into its compact string representation.Accepts the same values as JSON object members and JSON array elements: strings, numbers, booleans,
null, maps, collections, arrays, dates, temporals, enums,JsonObjectandJsonArray. Usefrom(java.lang.Object)to convert beans first.- Parameters:
value- the value to serialize- Returns:
- the JSON string
- Since:
- 1.10
-
toPrettyString
Serializes any JSON value into its multi-line indented string representation.Accepts the same values as
toString(Object).- Parameters:
value- the value to serialize- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
-
toBean
Fills a new bean instance with the members of a JSON object.Members without a matching bean property are ignored. 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, absent members 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
-
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>),nullelements staynull.- 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
-