Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional convenience APIs to JSON model structure #42951

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,50 @@ public JsonArray addElement(JsonElement element) {
return this;
}

/**
* Adds a boolean element to the JSON array. This element will be appended to the end of the array.
*
* @param element The boolean element to add to the array.
* @return The updated JsonArray object.
*/
public JsonArray addElement(boolean element) {
elements.add(JsonBoolean.getInstance(element));
return this;
}

/**
* Adds a number element to the JSON array. This element will be appended to the end of the array.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The number element to add to the array.
* @return The updated JsonArray object.
*/
public JsonArray addElement(Number element) {
elements.add(element == null ? JsonNull.getInstance() : new JsonNumber(element));
return this;
}

/**
* Adds a string element to the JSON array. This element will be appended to the end of the array.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The string element to add to the array.
* @return The updated JsonArray object.
*/
public JsonArray addElement(String element) {
elements.add(element == null ? JsonNull.getInstance() : new JsonString(element));
return this;
}

/**
* Adds a JsonElement to the JSON array at the specified index. This element will be inserted at the specified index
* and all elements at or after the index will be shifted.
*
* @param element The JsonElement to add to the array.
* @param index The index at which to add the element.
* @return The updated JsonArray object.
* @throws NullPointerException If the {@code element} is null.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
Expand All @@ -59,6 +95,55 @@ public JsonArray addElement(int index, JsonElement element) {
return this;
}

/**
* Adds a boolean element to the JSON array at the specified index. This element will be inserted at the specified index
* and all elements at or after the index will be shifted.
*
* @param element The boolean element to add to the array.
* @param index The index at which to add the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray addElement(int index, boolean element) {
elements.add(index, JsonBoolean.getInstance(element));
return this;
}

/**
* Adds a number element to the JSON array at the specified index. This element will be inserted at the specified index
* and all elements at or after the index will be shifted.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The number element to add to the array.
* @param index The index at which to add the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray addElement(int index, Number element) {
elements.add(index, element == null ? JsonNull.getInstance() : new JsonNumber(element));
return this;
}

/**
* Adds a string element to the JSON array at the specified index. This element will be inserted at the specified index
* and all elements at or after the index will be shifted.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The string element to add to the array.
* @param index The index at which to add the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray addElement(int index, String element) {
elements.add(index, element == null ? JsonNull.getInstance() : new JsonString(element));
return this;
}

/**
* Sets a specified JsonElement object at a specified index within the JsonArray. This will replace the current
* JsonElement at the specified index with the newly specified JsonElement object.
Expand All @@ -75,6 +160,55 @@ public JsonArray setElement(int index, JsonElement element) {
return this;
}

/**
* Sets a specified boolean element at a specified index within the JsonArray. This will replace the current
* JsonElement at the specified index with the newly specified boolean element.
*
* @param element The boolean element to set at the specified index.
* @param index The index at which to set the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray setElement(int index, boolean element) {
elements.set(index, JsonBoolean.getInstance(element));
return this;
}

/**
* Sets a specified number element at a specified index within the JsonArray. This will replace the current
* JsonElement at the specified index with the newly specified number element.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The number element to set at the specified index.
* @param index The index at which to set the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray setElement(int index, Number element) {
elements.set(index, element == null ? JsonNull.getInstance() : new JsonNumber(element));
return this;
}

/**
* Sets a specified string element at a specified index within the JsonArray. This will replace the current
* JsonElement at the specified index with the newly specified string element.
* <p>
* If the {@code element} is null this will be treated as a JSON null.
*
* @param element The string element to set at the specified index.
* @param index The index at which to set the element.
* @return The updated JsonArray object.
* @throws IndexOutOfBoundsException If the {@code index} is less than zero or greater than or equal to
* {@link #size()}.
*/
public JsonArray setElement(int index, String element) {
elements.set(index, element == null ? JsonNull.getInstance() : new JsonString(element));
return this;
}

/**
* Gets the JsonElement at the specified index from the JsonArray.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public boolean isArray() {
return false;
}

/**
* Casts the element to an array.
* <p>
* If {@link #isArray()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as an array.
*/
public JsonArray asArray() {
return (JsonArray) this;
}

/**
* Indicates whether the element is an object.
*
Expand All @@ -91,6 +102,17 @@ public boolean isObject() {
return false;
}

/**
* Casts the element to an object.
* <p>
* If {@link #isObject()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as an object.
*/
public JsonObject asObject() {
return (JsonObject) this;
}

/**
* Indicates whether the element is a boolean.
*
Expand All @@ -100,6 +122,17 @@ public boolean isBoolean() {
return false;
}

/**
* Casts the element to a boolean.
* <p>
* If {@link #isBoolean()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as a boolean.
*/
public JsonBoolean asBoolean() {
return (JsonBoolean) this;
}

/**
* Indicates whether the element is a null.
*
Expand All @@ -109,6 +142,17 @@ public boolean isNull() {
return false;
}

/**
* Casts the element to a null.
* <p>
* If {@link #isNull()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as a null.
*/
public JsonNull asNull() {
return (JsonNull) this;
}

/**
* Indicates whether the element is a number.
*
Expand All @@ -118,6 +162,17 @@ public boolean isNumber() {
return false;
}

/**
* Casts the element to a number.
* <p>
* If {@link #isNumber()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as a number.
*/
public JsonNumber asNumber() {
return (JsonNumber) this;
}

/**
* Indicates whether the element is a string.
*
Expand All @@ -126,4 +181,15 @@ public boolean isNumber() {
public boolean isString() {
return false;
}

/**
* Casts the element to a string.
* <p>
* If {@link #isString()} returns false, this will throw a {@link ClassCastException}.
*
* @return The element as a string.
*/
public JsonString asString() {
return (JsonString) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public JsonNumber(Number value) {

/**
* Returns the Number value from a JsonNumber object.
* <p>
* The value returned by this method will never be null.
*
* @return The Number value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ private JsonObject(Map<String, JsonElement> properties) {
this.properties = properties;
}

/**
* Checks whether a property with the specified key exists in the JSON object.
*
* @param key The key to check for.
* @return Whether a property with the specified key exists in the JSON object.
*/
public boolean hasProperty(String key) {
return properties.containsKey(key);
}

/**
* Gets the JsonElement value corresponding to the specified key. If the key doesn't exist, null will be returned.
*
Expand All @@ -55,6 +65,49 @@ public JsonObject setProperty(String key, JsonElement element) {
return this;
}

/**
* Sets the boolean value corresponding to the specified key. If the key already exists, the value will be
* overwritten.
*
* @param key The key of the property to set.
* @param element The boolean value to set the property to.
* @return The updated JsonObject object.
*/
public JsonObject setProperty(String key, boolean element) {
properties.put(key, JsonBoolean.getInstance(element));
return this;
}

/**
* Sets the number value corresponding to the specified key. If the key already exists, the value will be
* overwritten.
* <p>
* If {@code element} is null this will set the property to {@link JsonNull}.
*
* @param key The key of the property to set.
* @param element The number value to set the property to.
* @return The updated JsonObject object.
*/
public JsonObject setProperty(String key, Number element) {
properties.put(key, element == null ? JsonNull.getInstance() : new JsonNumber(element));
return this;
}

/**
* Sets the string value corresponding to the specified key. If the key already exists, the value will be
* overwritten.
* <p>
* If {@code element} is null this will set the property to {@link JsonNull}.
*
* @param key The key of the property to set.
* @param element The string value to set the property to.
* @return The updated JsonObject object.
*/
public JsonObject setProperty(String key, String element) {
properties.put(key, element == null ? JsonNull.getInstance() : new JsonString(element));
return this;
}

/**
* Removes the JsonElement value corresponding to the specified key. If the key doesn't exist, null will be
* returned.
Expand Down