Skip to content

Commit 15f18bb

Browse files
ramansahasipivovarit
authored andcommitted
BAEL 1269 Intro to JSON-JAVA (eugenp#3493)
* Final commit * Made changes as per last review * Moved from core-java to json module
1 parent 3e4b6df commit 15f18bb

14 files changed

Lines changed: 498 additions & 0 deletions

json/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<version>${fastjson.version}</version>
3232
</dependency>
3333

34+
<dependency>
35+
<groupId>org.json</groupId>
36+
<artifactId>json</artifactId>
37+
<version>20171018</version>
38+
</dependency>
3439
</dependencies>
3540

3641
<properties>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.json.CDL;
4+
import org.json.JSONArray;
5+
import org.json.JSONTokener;
6+
7+
public class CDLDemo {
8+
public static void main(String[] args) {
9+
System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: ");
10+
jsonArrayFromCDT();
11+
12+
System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: ");
13+
cDTfromJSONArray();
14+
15+
System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
16+
jaOfJOFromCDT2();
17+
18+
System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
19+
jaOfJOFromCDT2();
20+
}
21+
22+
public static void jsonArrayFromCDT() {
23+
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
24+
System.out.println(ja);
25+
}
26+
27+
public static void cDTfromJSONArray() {
28+
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
29+
String cdt = CDL.rowToString(ja);
30+
System.out.println(cdt);
31+
}
32+
33+
public static void jaOfJOFromCDT() {
34+
String string =
35+
"name, city, age \n" +
36+
"john, chicago, 22 \n" +
37+
"gary, florida, 35 \n" +
38+
"sal, vegas, 18";
39+
40+
JSONArray result = CDL.toJSONArray(string);
41+
System.out.println(result.toString());
42+
}
43+
44+
public static void jaOfJOFromCDT2() {
45+
JSONArray ja = new JSONArray();
46+
ja.put("name");
47+
ja.put("city");
48+
ja.put("age");
49+
50+
String string =
51+
"john, chicago, 22 \n" +
52+
"gary, florida, 35 \n" +
53+
"sal, vegas, 18";
54+
55+
JSONArray result = CDL.toJSONArray(ja, string);
56+
System.out.println(result.toString());
57+
}
58+
59+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.json.Cookie;
4+
import org.json.JSONObject;
5+
6+
public class CookieDemo {
7+
public static void main(String[] args) {
8+
System.out.println("8.1. Converting a Cookie String into a JSONObject");
9+
cookieStringToJSONObject();
10+
11+
System.out.println("\n8.2. Converting a JSONObject into Cookie String");
12+
jSONObjectToCookieString();
13+
}
14+
15+
public static void cookieStringToJSONObject() {
16+
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
17+
JSONObject cookieJO = Cookie.toJSONObject(cookie);
18+
System.out.println(cookieJO);
19+
}
20+
21+
public static void jSONObjectToCookieString() {
22+
JSONObject cookieJO = new JSONObject();
23+
cookieJO.put("name", "username");
24+
cookieJO.put("value", "John Doe");
25+
cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC");
26+
cookieJO.put("path", "/");
27+
String cookie = Cookie.toString(cookieJO);
28+
System.out.println(cookie);
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jsonjava;
2+
3+
public class DemoBean {
4+
private int id;
5+
private String name;
6+
private boolean active;
7+
8+
public int getId() {
9+
return id;
10+
}
11+
public void setId(int id) {
12+
this.id = id;
13+
}
14+
public String getName() {
15+
return name;
16+
}
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
public boolean isActive() {
21+
return active;
22+
}
23+
public void setActive(boolean active) {
24+
this.active = active;
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.json.HTTP;
4+
import org.json.JSONObject;
5+
6+
public class HTTPDemo {
7+
public static void main(String[] args) {
8+
System.out.println("9.1. Converting JSONObject to HTTP Header: ");
9+
jSONObjectToHTTPHeader();
10+
11+
System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: ");
12+
hTTPHeaderToJSONObject();
13+
}
14+
15+
public static void jSONObjectToHTTPHeader() {
16+
JSONObject jo = new JSONObject();
17+
jo.put("Method", "POST");
18+
jo.put("Request-URI", "http://www.example.com/");
19+
jo.put("HTTP-Version", "HTTP/1.1");
20+
System.out.println(HTTP.toString(jo));
21+
}
22+
23+
public static void hTTPHeaderToJSONObject() {
24+
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
25+
System.out.println(obj);
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.jsonjava;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
public class JSONArrayDemo {
10+
public static void main(String[] args) {
11+
System.out.println("5.1. Creating JSON Array: ");
12+
creatingJSONArray();
13+
14+
System.out.println("\n5.2. Creating JSON Array from JSON string: ");
15+
jsonArrayFromJSONString();
16+
17+
System.out.println("\n5.3. Creating JSON Array from Collection Object: ");
18+
jsonArrayFromCollectionObj();
19+
}
20+
21+
public static void creatingJSONArray() {
22+
JSONArray ja = new JSONArray();
23+
ja.put(Boolean.TRUE);
24+
ja.put("lorem ipsum");
25+
26+
// We can also put a JSONObject in JSONArray
27+
JSONObject jo = new JSONObject();
28+
jo.put("name", "jon doe");
29+
jo.put("age", "22");
30+
jo.put("city", "chicago");
31+
32+
ja.put(jo);
33+
34+
System.out.println(ja.toString());
35+
}
36+
37+
public static void jsonArrayFromJSONString() {
38+
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
39+
System.out.println(ja);
40+
}
41+
42+
public static void jsonArrayFromCollectionObj() {
43+
List<String> list = new ArrayList<>();
44+
list.add("California");
45+
list.add("Texas");
46+
list.add("Hawaii");
47+
list.add("Alaska");
48+
49+
JSONArray ja = new JSONArray(list);
50+
System.out.println(ja);
51+
}
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.jsonjava;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.json.JSONObject;
7+
8+
public class JSONObjectDemo {
9+
public static void main(String[] args) {
10+
System.out.println("4.1. Creating JSONObject: ");
11+
jsonFromJSONObject();
12+
13+
System.out.println("\n4.2. Creating JSONObject from Map: ");
14+
jsonFromMap();
15+
16+
System.out.println("\n4.3. Creating JSONObject from JSON string: ");
17+
jsonFromJSONString();
18+
19+
System.out.println("\n4.4. Creating JSONObject from Java Bean: ");
20+
jsonFromDemoBean();
21+
}
22+
23+
public static void jsonFromJSONObject() {
24+
JSONObject jo = new JSONObject();
25+
jo.put("name", "jon doe");
26+
jo.put("age", "22");
27+
jo.put("city", "chicago");
28+
29+
System.out.println(jo.toString());
30+
}
31+
32+
public static void jsonFromMap() {
33+
Map<String, String> map = new HashMap<>();
34+
map.put("name", "jon doe");
35+
map.put("age", "22");
36+
map.put("city", "chicago");
37+
JSONObject jo = new JSONObject(map);
38+
39+
System.out.println(jo.toString());
40+
}
41+
42+
public static void jsonFromJSONString() {
43+
JSONObject jo = new JSONObject(
44+
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
45+
);
46+
47+
System.out.println(jo.toString());
48+
}
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+
}
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.jsonjava;
2+
3+
import org.json.JSONTokener;
4+
5+
public class JSONTokenerDemo {
6+
public static void main(String[] args) {
7+
JSONTokener jt = new JSONTokener("Sample String");
8+
9+
while(jt.more()) {
10+
System.out.println(jt.next());
11+
}
12+
}
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.jsonjava;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.json.CDL;
6+
import org.json.JSONArray;
7+
import org.json.JSONTokener;
8+
import org.junit.Test;
9+
10+
public class CDLIntegrationTest {
11+
@Test
12+
public void givenCommaDelimitedText_thenConvertToJSONArray() {
13+
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
14+
assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
15+
}
16+
17+
@Test
18+
public void givenJSONArray_thenConvertToCommaDelimitedText() {
19+
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
20+
String cdt = CDL.rowToString(ja);
21+
assertEquals("England,USA,Canada", cdt.toString().trim());
22+
}
23+
24+
@Test
25+
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
26+
String string =
27+
"name, city, age \n" +
28+
"john, chicago, 22 \n" +
29+
"gary, florida, 35 \n" +
30+
"sal, vegas, 18";
31+
32+
JSONArray result = CDL.toJSONArray(string);
33+
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
34+
}
35+
36+
@Test
37+
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() {
38+
JSONArray ja = new JSONArray();
39+
ja.put("name");
40+
ja.put("city");
41+
ja.put("age");
42+
43+
String string =
44+
"john, chicago, 22 \n" +
45+
"gary, florida, 35 \n" +
46+
"sal, vegas, 18";
47+
48+
JSONArray result = CDL.toJSONArray(ja, string);
49+
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
50+
}
51+
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.jsonjava;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.json.Cookie;
6+
import org.json.JSONObject;
7+
import org.junit.Test;
8+
9+
public class CookieIntegrationTest {
10+
@Test
11+
public void givenCookieString_thenConvertToJSONObject() {
12+
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
13+
JSONObject cookieJO = Cookie.toJSONObject(cookie);
14+
15+
assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
16+
}
17+
18+
@Test
19+
public void givenJSONObject_thenConvertToCookieString() {
20+
JSONObject cookieJO = new JSONObject();
21+
cookieJO.put("name", "username");
22+
cookieJO.put("value", "John Doe");
23+
cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC");
24+
cookieJO.put("path", "/");
25+
String cookie = Cookie.toString(cookieJO);
26+
27+
assertEquals("username=John Doe;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/", cookie.toString());
28+
}
29+
}

0 commit comments

Comments
 (0)