From 81b2c9bc460ddb8b8650050d20b39e725dd64d3e Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 14 Nov 2024 16:45:33 -0500 Subject: [PATCH] Add additional conveniece APIs to JSON model structure --- .../java/com/azure/json/models/JsonArray.java | 136 +++++++++++++++++- .../com/azure/json/models/JsonElement.java | 66 +++++++++ .../com/azure/json/models/JsonNumber.java | 2 + .../com/azure/json/models/JsonObject.java | 53 +++++++ 4 files changed, 256 insertions(+), 1 deletion(-) diff --git a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonArray.java b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonArray.java index fe6dee7f87e37..74b25edd190e2 100644 --- a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonArray.java +++ b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonArray.java @@ -43,6 +43,43 @@ 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. + *
+ * 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. + *
+ * 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. @@ -50,7 +87,6 @@ public JsonArray addElement(JsonElement element) { * @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()}. */ @@ -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. + *
+ * 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. + *
+ * 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. @@ -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. + *
+ * 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. + *
+ * 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. * diff --git a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonElement.java b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonElement.java index 1b1fa9b39a5a6..4d4d29d30c332 100644 --- a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonElement.java +++ b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonElement.java @@ -82,6 +82,17 @@ public boolean isArray() { return false; } + /** + * Casts the element to an array. + *
+ * 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. * @@ -91,6 +102,17 @@ public boolean isObject() { return false; } + /** + * Casts the element to an object. + *
+ * 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. * @@ -100,6 +122,17 @@ public boolean isBoolean() { return false; } + /** + * Casts the element to a boolean. + *
+ * 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. * @@ -109,6 +142,17 @@ public boolean isNull() { return false; } + /** + * Casts the element to a null. + *
+ * 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. * @@ -118,6 +162,17 @@ public boolean isNumber() { return false; } + /** + * Casts the element to a number. + *
+ * 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. * @@ -126,4 +181,15 @@ public boolean isNumber() { public boolean isString() { return false; } + + /** + * Casts the element to a string. + *
+ * If {@link #isString()} returns false, this will throw a {@link ClassCastException}. + * + * @return The element as a string. + */ + public JsonString asString() { + return (JsonString) this; + } } diff --git a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonNumber.java b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonNumber.java index c47b93843004c..c7f5b097d640b 100644 --- a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonNumber.java +++ b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonNumber.java @@ -113,6 +113,8 @@ public JsonNumber(Number value) { /** * Returns the Number value from a JsonNumber object. + *
+ * The value returned by this method will never be null.
*
* @return The Number value.
*/
diff --git a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonObject.java b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonObject.java
index d147b40a6fba9..a2b55d2ec91e1 100644
--- a/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonObject.java
+++ b/sdk/serialization/azure-json/src/main/java/com/azure/json/models/JsonObject.java
@@ -31,6 +31,16 @@ private JsonObject(Map
+ * 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.
+ *
+ * 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.