- java.lang.Object
-
- io.github.jopenlibs.vault.json.Json
-
public final class Json extends java.lang.Object
This class serves as the entry point to the minimal-json API.To parse a given JSON input, use the
parse()
methods like in this example:JsonObject object = Json.parse(string).asObject();
To create a JSON data structure to be serialized, use the methods
value()
,array()
, andobject()
. For example, the following snippet will produce the JSON string {"foo": 23, "bar": true}:String string = Json.object().add("foo", 23).add("bar", true).toString();
To create a JSON array from a given Java array, you can use one of the
array()
methods with varargs parameters:String[] names = ... JsonArray array = Json.array(names);
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static JsonValue
array()
Creates a new empty JsonArray.static JsonArray
array(boolean... values)
Creates a new JsonArray that contains the JSON representations of the givenboolean
values.static JsonArray
array(double... values)
Creates a new JsonArray that contains the JSON representations of the givendouble
values.static JsonArray
array(float... values)
Creates a new JsonArray that contains the JSON representations of the givenfloat
values.static JsonArray
array(int... values)
Creates a new JsonArray that contains the JSON representations of the givenint
values.static JsonArray
array(long... values)
Creates a new JsonArray that contains the JSON representations of the givenlong
values.static JsonArray
array(java.lang.String... strings)
Creates a new JsonArray that contains the JSON representations of the given strings.static JsonObject
object()
Creates a new empty JsonObject.static JsonValue
parse(java.io.Reader reader)
Reads the entire input stream from the given reader and parses it as JSON.static JsonValue
parse(java.lang.String string)
Parses the given input string as JSON.static JsonValue
value(boolean value)
Returns a JsonValue instance that represents the givenboolean
value.static JsonValue
value(double value)
Returns a JsonValue instance that represents the givendouble
value.static JsonValue
value(float value)
Returns a JsonValue instance that represents the givenfloat
value.static JsonValue
value(int value)
Returns a JsonValue instance that represents the givenint
value.static JsonValue
value(int[] value)
Returns a JsonValue instance that represents the givenint[]
value.static JsonValue
value(long value)
Returns a JsonValue instance that represents the givenlong
value.static JsonValue
value(java.lang.String string)
Returns a JsonValue instance that represents the given string.
-
-
-
Method Detail
-
value
public static JsonValue value(int value)
Returns a JsonValue instance that represents the givenint
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
value
public static JsonValue value(int[] value)
Returns a JsonValue instance that represents the givenint[]
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
value
public static JsonValue value(long value)
Returns a JsonValue instance that represents the givenlong
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
value
public static JsonValue value(float value)
Returns a JsonValue instance that represents the givenfloat
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
value
public static JsonValue value(double value)
Returns a JsonValue instance that represents the givendouble
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
value
public static JsonValue value(java.lang.String string)
Returns a JsonValue instance that represents the given string.- Parameters:
string
- the string to get a JSON representation for- Returns:
- a JSON value that represents the given string
-
value
public static JsonValue value(boolean value)
Returns a JsonValue instance that represents the givenboolean
value.- Parameters:
value
- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
array
public static JsonValue array()
Creates a new empty JsonArray. This is equivalent to creating a new JsonArray using the constructor.- Returns:
- a new empty JSON array
-
array
public static JsonArray array(int... values)
Creates a new JsonArray that contains the JSON representations of the givenint
values.- Parameters:
values
- the values to be included in the new JSON array- Returns:
- a new JSON array that contains the given values
-
array
public static JsonArray array(long... values)
Creates a new JsonArray that contains the JSON representations of the givenlong
values.- Parameters:
values
- the values to be included in the new JSON array- Returns:
- a new JSON array that contains the given values
-
array
public static JsonArray array(float... values)
Creates a new JsonArray that contains the JSON representations of the givenfloat
values.- Parameters:
values
- the values to be included in the new JSON array- Returns:
- a new JSON array that contains the given values
-
array
public static JsonArray array(double... values)
Creates a new JsonArray that contains the JSON representations of the givendouble
values.- Parameters:
values
- the values to be included in the new JSON array- Returns:
- a new JSON array that contains the given values
-
array
public static JsonArray array(boolean... values)
Creates a new JsonArray that contains the JSON representations of the givenboolean
values.- Parameters:
values
- the values to be included in the new JSON array- Returns:
- a new JSON array that contains the given values
-
array
public static JsonArray array(java.lang.String... strings)
Creates a new JsonArray that contains the JSON representations of the given strings.- Parameters:
strings
- the strings to be included in the new JSON array- Returns:
- a new JSON array that contains the given strings
-
object
public static JsonObject object()
Creates a new empty JsonObject. This is equivalent to creating a new JsonObject using the constructor.- Returns:
- a new empty JSON object
-
parse
public static JsonValue parse(java.lang.String string)
Parses the given input string as JSON. The input must contain a valid JSON value, optionally padded with whitespace.- Parameters:
string
- the input string, must be valid JSON- Returns:
- a value that represents the parsed JSON
- Throws:
ParseException
- if the input is not valid JSON
-
parse
public static JsonValue parse(java.io.Reader reader) throws java.io.IOException
Reads the entire input stream from the given reader and parses it as JSON. The input must contain a valid JSON value, optionally padded with whitespace.Characters are read in chunks and buffered internally, therefore wrapping an existing reader in an additional
BufferedReader
does not improve reading performance.- Parameters:
reader
- the reader to read the JSON value from- Returns:
- a value that represents the parsed JSON
- Throws:
java.io.IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid JSON
-
-