Skip to content

Commit d70fbdc

Browse files
ramansahasidaoire
authored andcommitted
Bael 1269 Intro to JSON Java (eugenp#3662)
* Final commit * Made changes as per last review * Moved from core-java to json module * Interrupted thread before logging * Separated out JSON serialization to Java Object * Reverting access modifier
1 parent f17816d commit d70fbdc

8 files changed

Lines changed: 51 additions & 35 deletions

File tree

‎json/src/main/java/com/baeldung/jsonjava/CDLDemo.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
public class CDLDemo {
88
public static void main(String[] args) {
9-
System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: ");
9+
System.out.println("8.1. Producing JSONArray Directly from Comma Delimited Text: ");
1010
jsonArrayFromCDT();
1111

12-
System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: ");
12+
System.out.println("\n8.2. Producing Comma Delimited Text from JSONArray: ");
1313
cDTfromJSONArray();
1414

15-
System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
15+
System.out.println("\n8.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
1616
jaOfJOFromCDT2();
1717

18-
System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
18+
System.out.println("\n8.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
1919
jaOfJOFromCDT2();
2020
}
2121

‎json/src/main/java/com/baeldung/jsonjava/CookieDemo.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
public class CookieDemo {
77
public static void main(String[] args) {
8-
System.out.println("8.1. Converting a Cookie String into a JSONObject");
8+
System.out.println("9.1. Converting a Cookie String into a JSONObject");
99
cookieStringToJSONObject();
1010

11-
System.out.println("\n8.2. Converting a JSONObject into Cookie String");
11+
System.out.println("\n9.2. Converting a JSONObject into Cookie String");
1212
jSONObjectToCookieString();
1313
}
1414

‎json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
public class HTTPDemo {
77
public static void main(String[] args) {
8-
System.out.println("9.1. Converting JSONObject to HTTP Header: ");
8+
System.out.println("10.1. Converting JSONObject to HTTP Header: ");
99
jSONObjectToHTTPHeader();
1010

11-
System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: ");
11+
System.out.println("\n10.2. Converting HTTP Header String Back to JSONObject: ");
1212
hTTPHeaderToJSONObject();
1313
}
1414

‎json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
public class JSONArrayDemo {
1010
public static void main(String[] args) {
11-
System.out.println("5.1. Creating JSON Array: ");
11+
System.out.println("6.1. Creating JSON Array: ");
1212
creatingJSONArray();
1313

14-
System.out.println("\n5.2. Creating JSON Array from JSON string: ");
14+
System.out.println("\n6.2. Creating JSON Array from JSON string: ");
1515
jsonArrayFromJSONString();
1616

17-
System.out.println("\n5.3. Creating JSON Array from Collection Object: ");
17+
System.out.println("\n6.3. Creating JSON Array from Collection Object: ");
1818
jsonArrayFromCollectionObj();
1919
}
2020

‎json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ public static void main(String[] args) {
1515

1616
System.out.println("\n4.3. Creating JSONObject from JSON string: ");
1717
jsonFromJSONString();
18-
19-
System.out.println("\n4.4. Creating JSONObject from Java Bean: ");
20-
jsonFromDemoBean();
2118
}
2219

2320
public static void jsonFromJSONObject() {
@@ -46,14 +43,4 @@ public static void jsonFromJSONString() {
4643

4744
System.out.println(jo.toString());
4845
}
49-
50-
public static void jsonFromDemoBean() {
51-
DemoBean demo = new DemoBean();
52-
demo.setId(1);
53-
demo.setName("lorem ipsum");
54-
demo.setActive(true);
55-
56-
JSONObject jo = new JSONObject(demo);
57-
System.out.println(jo);
58-
}
5946
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.json.JSONObject;
4+
5+
public class ObjectToFromJSON {
6+
7+
public static void main(String args[]) throws Exception {
8+
System.out.println("\n5.1. Creating JSONObject from Java Bean: ");
9+
jsonFromDemoBean();
10+
}
11+
12+
public static void jsonFromDemoBean() {
13+
DemoBean demo = new DemoBean();
14+
demo.setId(1);
15+
demo.setName("lorem ipsum");
16+
demo.setActive(true);
17+
18+
JSONObject jo = new JSONObject(demo);
19+
System.out.println(jo);
20+
}
21+
}

‎json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,4 @@ public void givenJsonString_thenCreateJSONObject() {
3939

4040
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
4141
}
42-
43-
@Test
44-
public void givenDemoBean_thenCreateJSONObject() {
45-
DemoBean demo = new DemoBean();
46-
demo.setId(1);
47-
demo.setName("lorem ipsum");
48-
demo.setActive(true);
49-
50-
JSONObject jo = new JSONObject(demo);
51-
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
52-
}
5342
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jsonjava;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.json.JSONObject;
6+
import org.junit.Test;
7+
8+
public class ObjectToFromJSONIntegrationTest {
9+
@Test
10+
public void givenDemoBean_thenCreateJSONObject() {
11+
DemoBean demo = new DemoBean();
12+
demo.setId(1);
13+
demo.setName("lorem ipsum");
14+
demo.setActive(true);
15+
16+
JSONObject jo = new JSONObject(demo);
17+
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
18+
}
19+
}

0 commit comments

Comments
 (0)