Add code for BAEL-5878: Getting a Value in JSONObject (#13157)

* BAEL-5878: Getting a Value in JSONObject. Add JsonObjectValueGetter with unit tests

* Move json-value-getter under json-2 module

* Update packages for the source and unit test files

* Rename test class, move employee.json under test/resources

Co-authored-by: Rafael Engibaryan <rafael.engibaryan.1@gmail.com>
This commit is contained in:
rafyen 2022-12-14 14:58:33 +04:00 committed by GitHub
parent 78124d60dd
commit 9e7a6d2b3d
4 changed files with 197 additions and 0 deletions

View File

@ -108,6 +108,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,87 @@
package com.baeldung.jsonvaluegetter;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class JSONObjectValueGetter {
/**
* Get values associated with the provided key in the given JSONObject instance
*
* @param jsonObject JSONObject instance in which to search the key
* @param key Key we're interested in
*
* @return List of values associated with the given key, in the order of appearance.
* If the key is absent, empty list is returned.
*/
public List<String> getValuesInObject(JSONObject jsonObject, String key) {
List<String> accumulatedValues = new ArrayList<>();
for (String currentKey : jsonObject.keySet()) {
Object value = jsonObject.get(currentKey);
if (currentKey.equals(key)) {
accumulatedValues.add(value.toString());
}
if (value instanceof JSONObject) {
accumulatedValues.addAll(getValuesInObject((JSONObject)value, key));
} else if (value instanceof JSONArray) {
accumulatedValues.addAll(getValuesInArray((JSONArray)value, key));
}
}
return accumulatedValues;
}
/**
* Get values associated with the provided key in the given JSONArray instance
*
* @param jsonArray JSONArray instance in which to search the key
* @param key Key we're interested in
*
* @return List of values associated with the given key, in the order of appearance.
* If the key is absent, empty list is returned.
*/
public List<String> getValuesInArray(JSONArray jsonArray, String key) {
List<String> accumulatedValues = new ArrayList<>();
for (Object obj : jsonArray) {
if (obj instanceof JSONArray) {
accumulatedValues.addAll(getValuesInArray((JSONArray)obj, key));
} else if (obj instanceof JSONObject) {
accumulatedValues.addAll(getValuesInObject((JSONObject)obj, key));
}
}
return accumulatedValues;
}
/**
* Among all the values associated with the given key, get the N-th value
*
* @param jsonObject JSONObject instance in which to search the key
* @param key Key we're interested in
* @param N Index of the value to get
*
* @return N-th value associated with the key, or null if the key is absent or
* the number of values associated with the key is less than N
*/
public String getNthValue(JSONObject jsonObject, String key, int N) {
List<String> values = getValuesInObject(jsonObject, key);
return (values.size() >= N) ? values.get(N - 1) : null;
}
/**
* Count the number of values associated with the given key
*
* @param jsonObject JSONObject instance in which to count the key
* @param key Key we're interested in
*
* @return The number of values associated with the given key
*/
public int getCount(JSONObject jsonObject, String key) {
List<String> values = getValuesInObject(jsonObject, key);
return values.size();
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.jsonvaluegetter;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class JSONObjectValueGetterUnitTest {
private static JSONObject jsonObject;
private static JSONObjectValueGetter jsonObjectValueGetter = new JSONObjectValueGetter();
@BeforeAll
public static void loadJsonContent() throws IOException {
InputStream inputStream = JSONObjectValueGetterUnitTest.class.getClassLoader().getResourceAsStream("employee.json");
String jsonString = IOUtils.toString(inputStream, "UTF-8");
jsonObject = new JSONObject(jsonString);
}
@Test
public void getValueDirectly() {
JSONArray family = jsonObject.getJSONArray("family");
JSONObject sonObject = family.getJSONObject(1);
JSONObject sonData = sonObject.getJSONObject("son");
String sonName = sonData.getString("name");
Assertions.assertEquals(sonName, "Peter");
}
@Test
public void getAllAssociatedValuesRecursively() {
List<String> values = jsonObjectValueGetter.getValuesInObject(jsonObject, "son");
Assertions.assertEquals(values.size(), 1);
String sonString = values.get(0);
Assertions.assertTrue(sonString.contains("Peter"));
Assertions.assertTrue(sonString.contains("Schoolboy"));
Assertions.assertTrue(sonString.contains("11"));
values = jsonObjectValueGetter.getValuesInObject(jsonObject, "name");
Assertions.assertEquals(values.size(), 3);
Assertions.assertEquals(values.get(0), "Bob");
Assertions.assertEquals(values.get(1), "Alice");
Assertions.assertEquals(values.get(2), "Peter");
}
@Test
public void getNthValueRecursively() {
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 1), "Bob");
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 2), "Alice");
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 3), "Peter");
Assertions.assertNull(jsonObjectValueGetter.getNthValue(jsonObject, "nonExistingKey", 1));
}
@Test
public void getCountRecursively() {
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "name"), 3);
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "age"), 3);
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "occupation"), 1);
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "nonExistingKey"), 0);
}
}

View File

@ -0,0 +1,30 @@
{
"name" : "Bob",
"profession" : "Software engineer",
"department" : "Research",
"age" : 40,
"family" : [
{
"wife" : {
"name" : "Alice",
"profession" : "Doctor",
"age" : 38
}
},
{
"son" : {
"name" : "Peter",
"occupation" : "Schoolboy",
"age" : 11
}
}
],
"performance" : [
{
"2020" : 4.5
},
{
"2021" : 4.8
}
]
}