Skip to content

Commit 075614d

Browse files
authored
BAEL-9317 Update Parsing JSON boolean Value in Java Article
Add sections referring to mapping 0/1 values to false and true
1 parent e771841 commit 075614d

9 files changed

+97
-19
lines changed

json-modules/json/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
</dependencies>
6060

6161
<properties>
62+
<java.version>17</java.version>
6263
<networknt.json.schema.version>1.4.0</networknt.json.schema.version>
6364
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
6465
</properties>
6566

66-
</project>
67+
</project>

json-modules/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
public class CDLIntegrationTest {
1212

1313
@Test
14-
public void givenCommaDelimitedText_thenConvertToJSONArray() {
14+
public void givenCommaDelimitedText_whenConvertingToJSONArray_thenCorrectJSONArrayCreated() {
1515
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
1616

1717
assertThatJson(ja)
1818
.isEqualTo("[\"England\",\"USA\",\"Canada\"]");
1919
}
2020

2121
@Test
22-
public void givenJSONArray_thenConvertToCommaDelimitedText() {
22+
public void givenJSONArray_whenConvertingToCommaDelimitedText_thenCorrectCommaDelimitedTextCreated() {
2323
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
2424

2525
String cdt = CDL.rowToString(ja);
@@ -28,7 +28,7 @@ public void givenJSONArray_thenConvertToCommaDelimitedText() {
2828
}
2929

3030
@Test
31-
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
31+
public void givenCommaDelimitedText_whenConvertingToArrayOfJSONObjects_thenCorrectArrayOfJSONObjectsCreated() {
3232
String string =
3333
"name, city, age \n" +
3434
"john, chicago, 22 \n" +
@@ -42,7 +42,7 @@ public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
4242
}
4343

4444
@Test
45-
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() {
45+
public void givenCommaDelimitedText_whenConvertingToArrayOfJSONObjects2_thenCorrectArrayOfJSONObjects2Created() {
4646
JSONArray ja = new JSONArray();
4747
ja.put("name");
4848
ja.put("city");

json-modules/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class CookieIntegrationTest {
1111

1212
@Test
13-
public void givenCookieString_thenConvertToJSONObject() {
13+
public void givenCookieString_whenConvertingToJSONObject_thenCorrectJSONObjectCreated() {
1414
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
1515
JSONObject cookieJO = Cookie.toJSONObject(cookie);
1616

@@ -19,7 +19,7 @@ public void givenCookieString_thenConvertToJSONObject() {
1919
}
2020

2121
@Test
22-
public void givenJSONObject_thenConvertToCookieString() {
22+
public void givenJSONObject_whenConvertingToCookieString_thenCorrectCookieStringCreated() {
2323
JSONObject cookieJO = new JSONObject();
2424
cookieJO.put("name", "username");
2525
cookieJO.put("value", "John Doe");

json-modules/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class HTTPIntegrationTest {
1111
@Test
12-
public void givenJSONObject_thenConvertToHTTPHeader() {
12+
public void givenJSONObject_whenConvertingToHTTPHeader_thenCorrectHTTPHeaderCreated() {
1313
JSONObject jo = new JSONObject();
1414
jo.put("Method", "POST");
1515
jo.put("Request-URI", "http://www.example.com/");
@@ -20,7 +20,7 @@ public void givenJSONObject_thenConvertToHTTPHeader() {
2020
}
2121

2222
@Test
23-
public void givenHTTPHeader_thenConvertToJSONObject() {
23+
public void givenHTTPHeader_whenConvertingToJSONObject_thenCorrectJSONObjectCreated() {
2424
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
2525

2626
assertThatJson(obj)

json-modules/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class JSONArrayIntegrationTest {
1313

1414
@Test
15-
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
15+
public void givenJSONJava_whenCreatingNewJSONArrayFromScratch_thenCorrectNewJSONArrayCreated() {
1616
JSONArray ja = new JSONArray();
1717
ja.put(Boolean.TRUE);
1818
ja.put("lorem ipsum");
@@ -30,15 +30,15 @@ public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
3030
}
3131

3232
@Test
33-
public void givenJsonString_thenCreateNewJSONArray() {
33+
public void givenJsonString_whenCreatingNewJSONArray_thenCorrectNewJSONArrayCreated() {
3434
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
3535

3636
assertThatJson(ja)
3737
.isEqualTo("[true,\"lorem ipsum\",215]");
3838
}
3939

4040
@Test
41-
public void givenListObject_thenConvertItToJSONArray() {
41+
public void givenListObject_whenCreatingNewJSONArray_thenCorrectJSONArrayCreated() {
4242
List<String> list = new ArrayList<>();
4343
list.add("California");
4444
list.add("Texas");
@@ -50,4 +50,4 @@ public void givenListObject_thenConvertItToJSONArray() {
5050
assertThatJson(ja)
5151
.isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
5252
}
53-
}
53+
}

json-modules/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.json.JSONObject;
44
import org.junit.Test;
5-
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
67
import java.util.HashMap;
78
import java.util.Map;
89

@@ -11,7 +12,7 @@
1112
public class JSONObjectIntegrationTest {
1213

1314
@Test
14-
public void givenJSONJava_thenCreateNewJSONObject() {
15+
public void givenJSONJava_whenCreatingNewJSONObject_thenCorrectNewJSONObjectCreated() {
1516
JSONObject jo = new JSONObject();
1617
jo.put("name", "jon doe");
1718
jo.put("age", "22");
@@ -22,7 +23,29 @@ public void givenJSONJava_thenCreateNewJSONObject() {
2223
}
2324

2425
@Test
25-
public void givenMapObject_thenCreateJSONObject() {
26+
void givenJSON_whenParsed_thenCorrectValueReturned() {
27+
String jsonString = """
28+
{
29+
"type": "Feature",
30+
"geometry": "Point",
31+
"properties": {
32+
"isValid": true,
33+
"name": "Sample Point"
34+
}
35+
}
36+
""";
37+
JSONObject jsonObject = new JSONObject(jsonString);
38+
String type = jsonObject.getString("type");
39+
String geometry = jsonObject.getString("geometry");
40+
JSONObject properties = jsonObject.getJSONObject("properties");
41+
boolean isValid = properties.getBoolean("isValid");
42+
assertEquals("Feature",type);
43+
assertEquals("Point",geometry);
44+
assertTrue(isValid);
45+
}
46+
47+
@Test
48+
public void givenMapObject_whenCreatingNewJSONObject_thenCorrectNewJSONObjectCreated() {
2649
Map<String, String> map = new HashMap<>();
2750
map.put("name", "jon doe");
2851
map.put("age", "22");
@@ -34,7 +57,7 @@ public void givenMapObject_thenCreateJSONObject() {
3457
}
3558

3659
@Test
37-
public void givenJsonString_thenCreateJSONObject() {
60+
public void givenJSONString_whenCreatingNewJSONObject_thenCorrectNewJSONObjectCreated() {
3861
JSONObject jo = new JSONObject(
3962
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
4063
);

json-modules/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class JSONTokenerIntegrationTest {
99

1010
@Test
11-
public void givenString_convertItToJSONTokens() {
11+
public void givenString_whenConvertingToJSONTokens_thenCorrectlyConvertedToJSONTokens() {
1212
String str = "Sample String";
1313
JSONTokener jt = new JSONTokener(str);
1414

json-modules/json/src/test/java/com/baeldung/jsonjava/ObjectToFromJSONIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class ObjectToFromJSONIntegrationTest {
99

1010
@Test
11-
public void givenDemoBean_thenCreateJSONObject() {
11+
public void givenDemoBean_whenCreatingJSONObject_thenCreatedJSONObjectCorrectly() {
1212
DemoBean demo = new DemoBean();
1313
demo.setId(1);
1414
demo.setName("lorem ipsum");
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
import org.json.JSONObject;
10+
11+
import com.google.gson.JsonObject;
12+
import com.google.gson.JsonParser;
13+
14+
public class ParseJsonBooleanUnitTest {
15+
16+
private final String json = """
17+
{
18+
"name": "lorem ipsum",
19+
"active": true,
20+
"id": 1
21+
}
22+
""";
23+
24+
@Test
25+
void givenJSONString_whenParsed_thenCorrectBooleanValueReturned() {
26+
JSONObject jsonObject = new JSONObject(json);
27+
boolean active = jsonObject.getBoolean("active");
28+
assertTrue(active);
29+
}
30+
31+
@Test
32+
void givenJSONWithBooleanAs0Or1_whenParsed_thenCorrectBooleanValueReturned() {
33+
String json = """
34+
{
35+
"name": "lorem ipsum",
36+
"active": 1,
37+
"id": 1
38+
}
39+
""";
40+
JSONObject jsonObject = new JSONObject(json);
41+
assertThat(jsonObject.getInt("active")).isEqualTo(1);
42+
}
43+
44+
@Test
45+
void givenJSONWithMixedRepresentationForBoolean_whenParsed_thenCorrectBooleanValueReturned() {
46+
JSONObject jsonObject = new JSONObject(json);
47+
Object activeObject = jsonObject.get("active");
48+
if (activeObject instanceof Integer value) {
49+
assertTrue(value == 1);
50+
} else if (activeObject instanceof Boolean value) {
51+
assertTrue(value);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)