Skip to content

Commit

Permalink
Added some missing JavaDocs
Browse files Browse the repository at this point in the history
put methods for JsonObject are more convenient now.
Removed JsonObjectBuilder since it is no longer needed
  • Loading branch information
iamdudeman committed Apr 5, 2024
1 parent 866ad5b commit e2ffb63
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 134 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
* Javadoc more things
* Consider adding performance testing
* https://github.com/clarkware/junitperf
* compare vs GSON
Expand Down
1 change: 0 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
181 changes: 162 additions & 19 deletions src/main/java/technology/sola/json/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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;
}

/**
Expand Down
74 changes: 0 additions & 74 deletions src/main/java/technology/sola/json/builder/JsonObjectBuilder.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/technology/sola/json/builder/package-info.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/test/java/technology/sola/json/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Loading

0 comments on commit e2ffb63

Please sign in to comment.