Skip to content

Commit

Permalink
Merge pull request #54 from kraity/main
Browse files Browse the repository at this point in the history
Override the `get` method on JSONObject
  • Loading branch information
wenshao authored Apr 23, 2022
2 parents 857b474 + c292625 commit b4a539e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 22 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public JSONArray(int initialCapacity) {

/**
* @param collection the collection whose elements are to be placed into this {@link JSONArray}
* @throws NullPointerException If the specified collection is null
*/
public JSONArray(Collection<?> collection) {
super(collection);
}

/**
* @param items the array whose elements are to be placed into this {@link JSONArray}
* @throws NullPointerException If the specified items is null
*/
public JSONArray(Object... items) {
super(items.length);
Expand Down
105 changes: 83 additions & 22 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,79 @@ public JSONObject(int initialCapacity, float loadFactor, boolean accessOrder) {

/**
* @param map the map whose mappings are to be placed in this map
* @throws NullPointerException If the specified map is null
*/
public JSONObject(Map map) {
super(map);
}

/**
* Returns the Object of the associated keys in this {@link JSONObject}.
*
* @param key the key whose associated value is to be returned
*/
public Object get(String key) {
return super.get(key);
}

/**
* Returns the Object of the associated keys in this {@link JSONObject}.
*
* @param key the key whose associated value is to be returned
* @since 2.0.2
*/
@Override
public Object get(Object key) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
) {
return super.get(
key.toString()
);
}

return super.get(key);
}

/**
* @param key the key whose associated value is to be returned
* @param defaultValue the default mapping of the key
*/
public Object getOrDefault(String key, Object defaultValue) {
return super.getOrDefault(
key, defaultValue
);
}

/**
* @param key the key whose associated value is to be returned
* @param defaultValue the default mapping of the key
* @since 2.0.2
*/
public Object getOrDefault(Object key, Object defaultValue) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
) {
return super.getOrDefault(
key.toString(), defaultValue
);
}

return super.getOrDefault(
key, defaultValue
);
}

/**
* Returns the {@link JSONArray} of the associated keys in this {@link JSONObject}.
*
* @param key the key whose associated value is to be returned
* @return {@link JSONArray} or null
*/
public JSONArray getJSONArray(String key) {
Object value = get(key);
Object value = super.get(key);

if (value instanceof JSONArray) {
return (JSONArray) value;
Expand Down Expand Up @@ -110,14 +170,15 @@ public JSONArray getJSONArray(String key) {
* @return {@link JSONObject} or null
*/
public JSONObject getJSONObject(String key) {
Object value = get(key);
Object value = super.get(key);

if (value instanceof JSONObject) {
return (JSONObject) value;
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || str.equalsIgnoreCase("null")) {
return null;
}
Expand All @@ -143,7 +204,7 @@ public JSONObject getJSONObject(String key) {
* @return {@link String} or null
*/
public String getString(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand All @@ -165,7 +226,7 @@ public String getString(String key) {
* @throws JSONException Unsupported type conversion to {@link Double}
*/
public Double getDouble(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -201,7 +262,7 @@ public Double getDouble(String key) {
* @throws JSONException Unsupported type conversion to double value
*/
public double getDoubleValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0D;
Expand Down Expand Up @@ -233,7 +294,7 @@ public double getDoubleValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Float}
*/
public Float getFloat(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -269,7 +330,7 @@ public Float getFloat(String key) {
* @throws JSONException Unsupported type conversion to float value
*/
public float getFloatValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0F;
Expand Down Expand Up @@ -301,7 +362,7 @@ public float getFloatValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Long}
*/
public Long getLong(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -337,7 +398,7 @@ public Long getLong(String key) {
* @throws JSONException Unsupported type conversion to long value
*/
public long getLongValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0;
Expand Down Expand Up @@ -369,7 +430,7 @@ public long getLongValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Integer}
*/
public Integer getInteger(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -405,7 +466,7 @@ public Integer getInteger(String key) {
* @throws JSONException Unsupported type conversion to int value
*/
public int getIntValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0;
Expand Down Expand Up @@ -437,7 +498,7 @@ public int getIntValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Short}
*/
public Short getShort(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -473,7 +534,7 @@ public Short getShort(String key) {
* @throws JSONException Unsupported type conversion to short value
*/
public short getShortValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0;
Expand Down Expand Up @@ -505,7 +566,7 @@ public short getShortValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Byte}
*/
public Byte getByte(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -537,7 +598,7 @@ public Byte getByte(String key) {
* @throws JSONException Unsupported type conversion to byte value
*/
public byte getByteValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return 0;
Expand Down Expand Up @@ -568,7 +629,7 @@ public byte getByteValue(String key) {
* @throws JSONException Unsupported type conversion to {@link Boolean}
*/
public Boolean getBoolean(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -603,7 +664,7 @@ public Boolean getBoolean(String key) {
* @throws JSONException Unsupported type conversion to boolean value
*/
public boolean getBooleanValue(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return false;
Expand Down Expand Up @@ -634,7 +695,7 @@ public boolean getBooleanValue(String key) {
* @throws NumberFormatException If the value of get is {@link String} and it is not a valid representation of {@link BigInteger}
*/
public BigInteger getBigInteger(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -675,7 +736,7 @@ public BigInteger getBigInteger(String key) {
* @throws NumberFormatException If the value of get is {@link String} and it is not a valid representation of {@link BigDecimal}
*/
public BigDecimal getBigDecimal(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -720,7 +781,7 @@ public BigDecimal getBigDecimal(String key) {
* @return {@link Date} or null
*/
public Date getDate(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -748,7 +809,7 @@ public Date getDate(String key) {
* @return {@link BigInteger} or null
*/
public Instant getInstant(String key) {
Object value = get(key);
Object value = super.get(key);

if (value == null) {
return null;
Expand Down Expand Up @@ -837,7 +898,7 @@ public <T> T toJavaObject(Class<T> clazz) {
* @throws JSONException If no suitable conversion method is found
*/
public <T> T getObject(String key, Type type) {
Object value = get(key);
Object value = super.get(key);
if (value == null) {
return null;
}
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,4 +978,13 @@ public void testGet() {
JSONObject jsonObject = JSONObject.of("key", "1");
assertNull(jsonObject.getObject("a", String.class));
}

@Test
public void test_get() {
JSONObject jsonObject = JSONObject.of("123", "value1", "456.789", "value2", null, "value3");
assertEquals("value1", jsonObject.get(123));
assertEquals("value2", jsonObject.get(456.789));
assertEquals("value3", jsonObject.get(null));
assertEquals("value4", jsonObject.getOrDefault(false, "value4"));
}
}

0 comments on commit b4a539e

Please sign in to comment.