From e2ffb63fbb23f8f2743f301d5189ca6af50aafc1 Mon Sep 17 00:00:00 2001 From: iamdudeman Date: Fri, 5 Apr 2024 17:39:59 -0500 Subject: [PATCH] Added some missing JavaDocs put methods for JsonObject are more convenient now. Removed JsonObjectBuilder since it is no longer needed --- TODO.md | 1 - src/main/java/module-info.java | 1 - .../java/technology/sola/json/JsonObject.java | 181 ++++++++++++++++-- .../sola/json/builder/JsonObjectBuilder.java | 74 ------- .../sola/json/builder/package-info.java | 4 - .../technology/sola/json/JsonArrayTest.java | 3 +- .../technology/sola/json/JsonObjectTest.java | 24 +++ .../json/builder/JsonObjectBuilderTest.java | 33 ---- 8 files changed, 187 insertions(+), 134 deletions(-) delete mode 100644 src/main/java/technology/sola/json/builder/JsonObjectBuilder.java delete mode 100644 src/main/java/technology/sola/json/builder/package-info.java delete mode 100644 src/test/java/technology/sola/json/builder/JsonObjectBuilderTest.java diff --git a/TODO.md b/TODO.md index 8215921..96f8d2e 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -* Javadoc more things * Consider adding performance testing * https://github.com/clarkware/junitperf * compare vs GSON diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index e908403..362e891 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,7 +3,6 @@ */ module technology.sola.json { exports technology.sola.json; - exports technology.sola.json.builder; exports technology.sola.json.exception; exports technology.sola.json.mapper; exports technology.sola.json.parser; diff --git a/src/main/java/technology/sola/json/JsonObject.java b/src/main/java/technology/sola/json/JsonObject.java index 85617c0..e62b7c2 100644 --- a/src/main/java/technology/sola/json/JsonObject.java +++ b/src/main/java/technology/sola/json/JsonObject.java @@ -49,6 +49,14 @@ public JsonObject getObject(String key) { return get(key).asObject(); } + /** + * Returns the {@link JsonObject} for the specified key. Default value is returned if key is missing. + * + * @param key the key of the {@link JsonObject} to be returned + * @param defaultValue the default value to return if key is missing + * @return the {@link JsonObject} to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#JSON_OBJECT} + */ public JsonObject getObject(String key, JsonObject defaultValue) { var value = get(key); @@ -70,6 +78,14 @@ public JsonArray getArray(String key) { return get(key).asArray(); } + /** + * Returns the {@link JsonArray} for the specified key. Default value is returned if key is missing. + * + * @param key the key of the {@link JsonArray} to be returned + * @param defaultValue the default value to return if key is missing + * @return the {@link JsonArray} to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#JSON_ARRAY} + */ public JsonArray getArray(String key, JsonArray defaultValue) { var value = get(key); @@ -91,6 +107,14 @@ public String getString(String key) { return get(key).asString(); } + /** + * Returns the string for the specified key. Default value is returned if key is missing. + * + * @param key the key of the string to be returned + * @param defaultValue the default value to return if key is missing + * @return the string to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#STRING} + */ public String getString(String key, String defaultValue) { var value = get(key); @@ -112,6 +136,14 @@ public double getDouble(String key) { return get(key).asDouble(); } + /** + * Returns the double for the specified key. Default value is returned if key is missing. + * + * @param key the key of the double to be returned + * @param defaultValue the default value to return if key is missing + * @return the double to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#DOUBLE} + */ public Double getDouble(String key, Double defaultValue) { var value = get(key); @@ -133,6 +165,14 @@ public float getFloat(String key) { return get(key).asFloat(); } + /** + * Returns the float for the specified key. Default value is returned if key is missing. + * + * @param key the key of the float to be returned + * @param defaultValue the default value to return if key is missing + * @return the float to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#DOUBLE} + */ public Float getFloat(String key, Float defaultValue) { var value = get(key); @@ -154,6 +194,14 @@ public int getInt(String key) { return get(key).asInt(); } + /** + * Returns the integer for the specified key. Default value is returned if key is missing. + * + * @param key the key of the integer to be returned + * @param defaultValue the default value to return if key is missing + * @return the integer to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#LONG} + */ public Integer getInt(String key, Integer defaultValue) { var value = get(key); @@ -175,6 +223,14 @@ public long getLong(String key) { return get(key).asLong(); } + /** + * Returns the long for the specified key. Default value is returned if key is missing. + * + * @param key the key of the long to be returned + * @param defaultValue the default value to return if key is missing + * @return the long to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#LONG} + */ public Long getLong(String key, Long defaultValue) { var value = get(key); @@ -196,6 +252,14 @@ public boolean getBoolean(String key) { return get(key).asBoolean(); } + /** + * Returns the boolean for the specified key. Default value is returned if key is missing. + * + * @param key the key of the boolean to be returned + * @param defaultValue the default value to return if key is missing + * @return the boolean to which the specified key is mapped + * @throws JsonElementTypeException if {@link JsonElement} at index is not of type {@link JsonElementType#BOOLEAN} + */ public Boolean getBoolean(String key, Boolean defaultValue) { var value = get(key); @@ -216,46 +280,125 @@ public boolean isNull(String key) { return get(key).isNull(); } - public JsonElement put(String key, JsonObject value) { - return put(key, new JsonElement(value)); + @Override + public JsonElement put(String key, JsonElement value) { + return super.put(key, value == null ? new JsonElement() : value); } - public JsonElement put(String key, JsonArray value) { - return put(key, new JsonElement(value)); + /** + * Associates a {@link JsonObject} with specified key. + * + * @param key the key to associate the {@link JsonObject} with + * @param value the {@link JsonObject} to associate + * @return this + */ + public JsonObject put(String key, JsonObject value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, String value) { - return put(key, new JsonElement(value)); + /** + * Associates a {@link JsonArray} with specified key. + * + * @param key the key to associate the {@link JsonArray} with + * @param value the {@link JsonArray} to associate + * @return this + */ + public JsonObject put(String key, JsonArray value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, Integer value) { - return put(key, new JsonElement(value)); + /** + * Associates a string with specified key. + * + * @param key the key to associate the string with + * @param value the string to associate + * @return this + */ + public JsonObject put(String key, String value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, Long value) { - return put(key, new JsonElement(value)); + /** + * Associates a integer with specified key. + * + * @param key the key to associate the integer with + * @param value the integer to associate + * @return this + */ + public JsonObject put(String key, Integer value) { + put(key, new JsonElement(value)); + + return this; + } + + /** + * Associates a long with specified key. + * + * @param key the key to associate the long with + * @param value the long to associate + * @return this + */ + public JsonObject put(String key, Long value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, Double value) { - return put(key, new JsonElement(value)); + /** + * Associates a double with specified key. + * + * @param key the key to associate the double with + * @param value the double to associate + * @return this + */ + public JsonObject put(String key, Double value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, Float value) { - return put(key, new JsonElement(value)); + /** + * Associates a float with specified key. + * + * @param key the key to associate the float with + * @param value the float to associate + * @return this + */ + public JsonObject put(String key, Float value) { + put(key, new JsonElement(value)); + + return this; } - public JsonElement put(String key, Boolean value) { - return put(key, new JsonElement(value)); + /** + * Associates a boolean with specified key. + * + * @param key the key to associate the boolean with + * @param value the boolean to associate + * @return this + */ + public JsonObject put(String key, Boolean value) { + put(key, new JsonElement(value)); + + return this; } /** * Puts {@link JsonElement} of type {@link JsonElementType#NULL} into this {@link JsonObject}. * * @param key the key to put a NULL JSON value into - * @return the previous {@link JsonElement} associated with this key + * @return this */ - public JsonElement putNull(String key) { - return put(key, new JsonElement()); + public JsonObject putNull(String key) { + put(key, new JsonElement()); + + return this; } /** diff --git a/src/main/java/technology/sola/json/builder/JsonObjectBuilder.java b/src/main/java/technology/sola/json/builder/JsonObjectBuilder.java deleted file mode 100644 index ac74458..0000000 --- a/src/main/java/technology/sola/json/builder/JsonObjectBuilder.java +++ /dev/null @@ -1,74 +0,0 @@ -package technology.sola.json.builder; - -import technology.sola.json.JsonArray; -import technology.sola.json.JsonObject; - -import java.util.function.Function; - -/** - * JsonObjectBuilder provides convenient methods for creating a new {@link JsonObject}. - */ -public class JsonObjectBuilder { - private final JsonObject jsonObject; - - public JsonObjectBuilder() { - jsonObject = new JsonObject(); - } - - public JsonObjectBuilder addInt(String key, Integer value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addLong(String key, Long value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addFloat(String key, Float value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addDouble(String key, Double value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addBoolean(String key, Boolean value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addString(String key, String value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addNull(String key) { - jsonObject.putNull(key); - return this; - } - - public JsonObjectBuilder addObject(String key, JsonObject value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addObject(String key, Function consumer) { - return addObject(key, consumer.apply(new JsonObjectBuilder()).build()); - } - - public JsonObjectBuilder addArray(String key, JsonArray value) { - jsonObject.put(key, value); - return this; - } - - public JsonObjectBuilder addArray(String key, Function consumer) { - return addArray(key, consumer.apply(new JsonArray())); - } - - public JsonObject build() { - return jsonObject; - } -} diff --git a/src/main/java/technology/sola/json/builder/package-info.java b/src/main/java/technology/sola/json/builder/package-info.java deleted file mode 100644 index 0eda937..0000000 --- a/src/main/java/technology/sola/json/builder/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * This package provides builders for {@link technology.sola.json.JsonArray} and {@link technology.sola.json.JsonObject}. - */ -package technology.sola.json.builder; diff --git a/src/test/java/technology/sola/json/JsonArrayTest.java b/src/test/java/technology/sola/json/JsonArrayTest.java index 1187ea5..c0b2fc2 100644 --- a/src/test/java/technology/sola/json/JsonArrayTest.java +++ b/src/test/java/technology/sola/json/JsonArrayTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import technology.sola.json.builder.JsonObjectBuilder; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -123,7 +122,7 @@ void addMethodsChaining() { .add("value") .addNull() .add(new JsonArray().add("test")) - .add(new JsonObjectBuilder().addString("string", "value").build()); + .add(new JsonObject().put("string", "value")); assertEquals(2, result.getInt(0)); assertEquals(3L, result.getLong(1)); diff --git a/src/test/java/technology/sola/json/JsonObjectTest.java b/src/test/java/technology/sola/json/JsonObjectTest.java index d363b73..c13121a 100644 --- a/src/test/java/technology/sola/json/JsonObjectTest.java +++ b/src/test/java/technology/sola/json/JsonObjectTest.java @@ -134,6 +134,30 @@ void nullMethods() { root.putNull(TEST_KEY); assertTrue(root.isNull(TEST_KEY)); } + + @Test + void putMethodsChaining() { + var result = new JsonObject() + .put("int", 2) + .put("long", 3L) + .put("float", 1.5f) + .put("double", 2.5) + .put("boolean", true) + .put("string", "value") + .putNull("null") + .put("array", new JsonArray().add("test")) + .put("object", new JsonObject().put("string", "value")); + + assertEquals(2, result.getInt("int")); + assertEquals(3L, result.getLong("long")); + assertEquals(1.5f, result.getFloat("float")); + assertEquals(2.5, result.getDouble("double")); + assertEquals("value", result.getString("string")); + assertTrue(result.getBoolean("boolean")); + assertTrue(result.isNull("null")); + assertEquals("test", result.getArray("array").getString(0)); + assertEquals("value", result.getObject("object").getString("string")); + } } @Nested diff --git a/src/test/java/technology/sola/json/builder/JsonObjectBuilderTest.java b/src/test/java/technology/sola/json/builder/JsonObjectBuilderTest.java deleted file mode 100644 index 8de1dd8..0000000 --- a/src/test/java/technology/sola/json/builder/JsonObjectBuilderTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package technology.sola.json.builder; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class JsonObjectBuilderTest { - @Test - void test() { - var result = new JsonObjectBuilder() - .addInt("int", 2) - .addLong("long", 3L) - .addFloat("float", 1.5f) - .addDouble("double", 2.5) - .addBoolean("boolean", true) - .addString("string", "value") - .addNull("null") - .addArray("array", array -> array.add("test")) - .addObject("object", object -> object.addString("string", "value")) - .build(); - - assertEquals(2, result.getInt("int")); - assertEquals(3L, result.getLong("long")); - assertEquals(1.5f, result.getFloat("float")); - assertEquals(2.5, result.getDouble("double")); - assertEquals("value", result.getString("string")); - assertTrue(result.getBoolean("boolean")); - assertTrue(result.isNull("null")); - assertEquals("test", result.getArray("array").getString(0)); - assertEquals("value", result.getObject("object").getString("string")); - } -}