Skip to content

Commit

Permalink
more sig matching for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Sep 22, 2019
1 parent 6eaab3f commit e9d7da0
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 68 deletions.
150 changes: 131 additions & 19 deletions unirest/src/main/java/kong/unirest/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public JSONArray put(double num) throws JSONException {
* @param num a int
* @return this JSONArray
*/
public JSONArray put(int num) throws JSONException {
public JSONArray put(int num) {
obj.add(num);
return this;
}
Expand All @@ -148,7 +148,7 @@ public JSONArray put(int num) throws JSONException {
* @param num a long
* @return this JSONArray
*/
public JSONArray put(long num) throws JSONException {
public JSONArray put(long num) {
obj.add(num);
return this;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public JSONArray put(Number num) {
* @param bool a Boolean
* @return this JSONArray
*/
public JSONArray put(Boolean bool) {
public JSONArray put(boolean bool) {
obj.add(bool);
return this;
}
Expand Down Expand Up @@ -234,6 +234,52 @@ public JSONArray put(int index, long number) throws JSONException {
return put(index, new JsonPrimitive(number));
}

/**
* put a double at a specific instance
* if the index is beyond the currently length the array will be buffered with nulls
* @param index the index position to put to
* @param number a double
* @return this JSONArray
*/
public JSONArray put(int index, double number) throws JSONException {
return put(index, new JsonPrimitive(number));
}

/**
* put a boolean at a specific index
* @param index the index position to put to
* @param bool a bool value
* @return this JSONArray
*/
public JSONArray put(int index, boolean bool) throws JSONException {
return put(index, new JsonPrimitive(bool));
}

/**
* put a object at a specific instance
* if the index is beyond the currently length the array will be buffered with nulls
* @param index the index position to put to
* @param object a long
* @return this JSONArray
* @throws JSONException
*/
public JSONArray put(int index, Object object) throws JSONException {
if (object == null) {
put(index, JsonNull.INSTANCE);
} else if (object instanceof Number) {
put(index, (Number) object);
} else if (object instanceof Boolean) {
put(index, (boolean) object);
} else if (object instanceof JSONObject) {
put(index, ((JSONObject) object).getElement());
} else if (object instanceof JSONArray) {
put(index, ((JSONArray) object).getElement());
} else {
put(index, String.valueOf(object));
}
return this;
}

/**
* put a float at a specific instance
* if the index is beyond the currently length the array will be buffered with nulls
Expand Down Expand Up @@ -285,7 +331,7 @@ public JSONArray put(int index, String string) {
* @param map a Map of String keys and values of JSON Types
* @return this JSONArray
*/
public JSONArray put(int index, Map map) {
public JSONArray put(int index, Map map) throws JSONException {
return put(index, toJsonObject(map));
}

Expand Down Expand Up @@ -361,6 +407,38 @@ public Object remove(int index) {
}
}

/**
* get a boolean at a specified index
* @param index the array index position
* @return a boolean
* @throws JSONException if the element is not a boolean or index is out of bounds
*/
public boolean getBoolean(int index) throws JSONException {
JsonElement e = getElement(index);
if (!e.isJsonPrimitive() || !e.getAsJsonPrimitive().isBoolean()) {
throw new JSONException("JSONArray[%s] is not a boolean.", index);
}
return e.getAsBoolean();
}

/**
* get a boolean at a specified index
* @param index the array index position
* @return a boolean
*/
public boolean optBoolean(int index) {
return optBoolean(index, false);
}

/**
* get a boolean at a specified index
* @param index the array index position
* @return a boolean
*/
public boolean optBoolean(int index, boolean defaultValue) {
return getOrDefault(() -> getElement(index).getAsBoolean(), defaultValue);
}

/**
* get a JSONObject at a specified index
* @param index the array index position
Expand Down Expand Up @@ -391,7 +469,7 @@ public JSONObject optJSONObject(int index) {
* @return a Double
* @throws JSONException if the element is not a Double or index is out of bounds
*/
public Double getDouble(int index) throws JSONException {
public double getDouble(int index) throws JSONException {
return tryNumber(() -> getElement(index).getAsDouble(), index);
}

Expand Down Expand Up @@ -421,7 +499,7 @@ public double optDouble(int index, double defaultValue) {
* @return a Float
* @throws JSONException if the element is not a Float or index is out of bounds
*/
public Float getFloat(int index) throws JSONException {
public float getFloat(int index) throws JSONException {
return tryNumber(() -> getElement(index).getAsFloat(), index);
}

Expand All @@ -431,7 +509,7 @@ public Float getFloat(int index) throws JSONException {
* @param index the array index position
* @return a Float
*/
public Float optFloat(int index) {
public float optFloat(int index) {
return optFloat(index, Float.NaN);
}

Expand All @@ -442,7 +520,7 @@ public Float optFloat(int index) {
* @param defaultValue the default value to return if the index or value type are not valid
* @return a Float
*/
public Float optFloat(int index, float defaultValue) {
public float optFloat(int index, float defaultValue) {
return getOrDefault(() -> getFloat(index), defaultValue);
}

Expand Down Expand Up @@ -480,13 +558,32 @@ public long optLong(int index, long defaultValue) {
/**
* get a Number at a specified index
* @param index the array index position
* @return a int
* @return a Number
* @throws JSONException if the element is not a Number or index is out of bounds
*/
public Number getNumber(int index) throws JSONException {
return tryNumber(() -> getElement(index).getAsInt(), index);
}

/**
* get a Number at a specified index
* @param index the array index position
* @return a int
*/
public Number optNumber(int index) {
return getOrDefault(() -> getNumber(index), null);
}

/**
* get a Number at a specified index
* @param index the array index position
* @param defaultValue the default value if the index does not exist or is not a number
* @return a Number
*/
public Number optNumber(int index, Number defaultValue) {
return getOrDefault(() -> getNumber(index), defaultValue);
}

/**
* get a int at a specified index
* @param index the array index position
Expand Down Expand Up @@ -615,7 +712,15 @@ public JSONArray optJSONArray(int index) {
return getOrDefault(() -> getJSONArray(index), null);
}

public <T extends Enum<T>> T getEnum(Class<T> enumClass, int index) {
/**
* get a enum value based on name from a specific index
* @param enumClass the enum type
* @param index the index
* @param <T> the type of enum
* @return a enum value
* @throws JSONException if the index is out of bounds or the value cannot be converted to the enum type
*/
public <T extends Enum<T>> T getEnum(Class<T> enumClass, int index) throws JSONException {

String raw = getElement(index).getAsString();
try {
Expand Down Expand Up @@ -651,7 +756,7 @@ public Object get(int index) throws JSONException {
public Object opt(int index) {
try {
return get(index);
}catch (Exception e){
} catch (Exception e) {
return null;
}
}
Expand All @@ -671,7 +776,7 @@ public String toString() {
* @param indent currently ignored until this is addressed by GSON
* @return string json
*/
public String toString(int indent) {
public String toString(int indent) throws JSONException {
return toPrettyJson(obj);
}

Expand All @@ -680,7 +785,7 @@ public String toString(int indent) {
* @param token
* @return a token delimited array
*/
public String join(String token) {
public String join(String token) throws JSONException {
return StreamSupport.stream(obj.spliterator(), false)
.map(String::valueOf)
.collect(Collectors.joining(token));
Expand Down Expand Up @@ -730,7 +835,14 @@ public boolean isNull(int index) {
}

public boolean equals(Object o) {
return o == this || o instanceof JsonArray && ((JSONArray) o).obj.equals(this.obj);
if (o == this) {
return true;
} else if (o instanceof JsonArray) {
return this.obj.equals(o);
} else if (o instanceof JSONArray) {
return this.obj.equals(((JSONArray) o).obj);
}
return false;
}

public int hashCode() {
Expand Down Expand Up @@ -775,22 +887,22 @@ private <T extends Number> T tryNumber(Supplier<T> supplier, int index) {
* @throws JSONException If any of the names are null.
*/
public JSONObject toJSONObject(JSONArray names) throws JSONException {
if(names == null || names.isEmpty() || isEmpty()){
if (names == null || names.isEmpty() || isEmpty()) {
return null;
}
JSONObject object = new JSONObject();
int index = 0;
for(Object key : names){
if(index >= length()){
for (Object key : names) {
if (index >= length()) {
break;
}
if(key == null){
if (key == null) {
throw new JSONException("JSONArray[%s] not a string.", index);
}
object.put(String.valueOf(key), get(index));
index++;
}
return object;
return object;
}

/**
Expand Down
32 changes: 28 additions & 4 deletions unirest/src/main/java/kong/unirest/json/JSONElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ protected JSONElement(JsonElement e){
/**
* Write the JSON to a Writer
* @param sw the writer
* @throws JSONException for IO problems
*/
public void write(Writer sw) {
public Writer write(Writer sw) throws JSONException {
write(element, sw);
return sw;
}

/**
* Write the JSON to a Writer with a pretty format
* due to limitations in GSON the index and indent are currently ignored
* @param sw the writer
* @param indentFactor currently ignored
* @param indent currently ignored
* @throws JSONException for IO problems
*/
public void write(Writer sw, int indentFactor, int indent) {
public Writer write(Writer sw, int indentFactor, int indent) throws JSONException {
writePretty(element, sw);
return sw;
}

/**
Expand All @@ -68,8 +74,18 @@ public void write(Writer sw, int indentFactor, int indent) {
* @return the thing you asked for
*/
public Object query(String query) {
JSONPointer pointer = JSONPointer.compile(query);
return pointer.queryFrom(this);
return query(JSONPointer.compile(query));
}

/**
* query the object graph using JSONPointer
* https://tools.ietf.org/html/rfc6901
*
* @param query the pointer to get
* @return the thing you asked for
*/
public Object query(JSONPointer query) {
return query.queryFrom(this);
}

public Object optQuery(String query){
Expand All @@ -80,6 +96,14 @@ public Object optQuery(String query){
}
}

public Object optQuery(JSONPointer query){
try{
return query.queryFrom(this);
} catch (Exception e){
return null;
}
}

JsonElement getElement() {
return element;
}
Expand Down
Loading

0 comments on commit e9d7da0

Please sign in to comment.