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
@@ -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);
}
}
@@ -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
}
]
}