BAEL 1269 Intro to JSON-JAVA (#3493)

* Final commit

* Made changes as per last review

* Moved from core-java to json module
This commit is contained in:
ramansahasi 2018-02-01 15:27:48 +05:30 committed by Grzegorz Piwowarek
parent 3e4b6df194
commit 15f18bbb83
14 changed files with 498 additions and 0 deletions

View File

@ -31,6 +31,11 @@
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,59 @@
package com.baeldung.jsonjava;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;
public class CDLDemo {
public static void main(String[] args) {
System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: ");
jsonArrayFromCDT();
System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: ");
cDTfromJSONArray();
System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
jaOfJOFromCDT2();
System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
jaOfJOFromCDT2();
}
public static void jsonArrayFromCDT() {
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
System.out.println(ja);
}
public static void cDTfromJSONArray() {
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
String cdt = CDL.rowToString(ja);
System.out.println(cdt);
}
public static void jaOfJOFromCDT() {
String string =
"name, city, age \n" +
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
JSONArray result = CDL.toJSONArray(string);
System.out.println(result.toString());
}
public static void jaOfJOFromCDT2() {
JSONArray ja = new JSONArray();
ja.put("name");
ja.put("city");
ja.put("age");
String string =
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
JSONArray result = CDL.toJSONArray(ja, string);
System.out.println(result.toString());
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.jsonjava;
import org.json.Cookie;
import org.json.JSONObject;
public class CookieDemo {
public static void main(String[] args) {
System.out.println("8.1. Converting a Cookie String into a JSONObject");
cookieStringToJSONObject();
System.out.println("\n8.2. Converting a JSONObject into Cookie String");
jSONObjectToCookieString();
}
public static void cookieStringToJSONObject() {
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
JSONObject cookieJO = Cookie.toJSONObject(cookie);
System.out.println(cookieJO);
}
public static void jSONObjectToCookieString() {
JSONObject cookieJO = new JSONObject();
cookieJO.put("name", "username");
cookieJO.put("value", "John Doe");
cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC");
cookieJO.put("path", "/");
String cookie = Cookie.toString(cookieJO);
System.out.println(cookie);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.jsonjava;
public class DemoBean {
private int id;
private String name;
private boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.jsonjava;
import org.json.HTTP;
import org.json.JSONObject;
public class HTTPDemo {
public static void main(String[] args) {
System.out.println("9.1. Converting JSONObject to HTTP Header: ");
jSONObjectToHTTPHeader();
System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: ");
hTTPHeaderToJSONObject();
}
public static void jSONObjectToHTTPHeader() {
JSONObject jo = new JSONObject();
jo.put("Method", "POST");
jo.put("Request-URI", "http://www.example.com/");
jo.put("HTTP-Version", "HTTP/1.1");
System.out.println(HTTP.toString(jo));
}
public static void hTTPHeaderToJSONObject() {
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
System.out.println(obj);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.jsonjava;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONArrayDemo {
public static void main(String[] args) {
System.out.println("5.1. Creating JSON Array: ");
creatingJSONArray();
System.out.println("\n5.2. Creating JSON Array from JSON string: ");
jsonArrayFromJSONString();
System.out.println("\n5.3. Creating JSON Array from Collection Object: ");
jsonArrayFromCollectionObj();
}
public static void creatingJSONArray() {
JSONArray ja = new JSONArray();
ja.put(Boolean.TRUE);
ja.put("lorem ipsum");
// We can also put a JSONObject in JSONArray
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
ja.put(jo);
System.out.println(ja.toString());
}
public static void jsonArrayFromJSONString() {
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
System.out.println(ja);
}
public static void jsonArrayFromCollectionObj() {
List<String> list = new ArrayList<>();
list.add("California");
list.add("Texas");
list.add("Hawaii");
list.add("Alaska");
JSONArray ja = new JSONArray(list);
System.out.println(ja);
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.jsonjava;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public class JSONObjectDemo {
public static void main(String[] args) {
System.out.println("4.1. Creating JSONObject: ");
jsonFromJSONObject();
System.out.println("\n4.2. Creating JSONObject from Map: ");
jsonFromMap();
System.out.println("\n4.3. Creating JSONObject from JSON string: ");
jsonFromJSONString();
System.out.println("\n4.4. Creating JSONObject from Java Bean: ");
jsonFromDemoBean();
}
public static void jsonFromJSONObject() {
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
System.out.println(jo.toString());
}
public static void jsonFromMap() {
Map<String, String> map = new HashMap<>();
map.put("name", "jon doe");
map.put("age", "22");
map.put("city", "chicago");
JSONObject jo = new JSONObject(map);
System.out.println(jo.toString());
}
public static void jsonFromJSONString() {
JSONObject jo = new JSONObject(
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
);
System.out.println(jo.toString());
}
public static void jsonFromDemoBean() {
DemoBean demo = new DemoBean();
demo.setId(1);
demo.setName("lorem ipsum");
demo.setActive(true);
JSONObject jo = new JSONObject(demo);
System.out.println(jo);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.jsonjava;
import org.json.JSONTokener;
public class JSONTokenerDemo {
public static void main(String[] args) {
JSONTokener jt = new JSONTokener("Sample String");
while(jt.more()) {
System.out.println(jt.next());
}
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.junit.Test;
public class CDLIntegrationTest {
@Test
public void givenCommaDelimitedText_thenConvertToJSONArray() {
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
}
@Test
public void givenJSONArray_thenConvertToCommaDelimitedText() {
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
String cdt = CDL.rowToString(ja);
assertEquals("England,USA,Canada", cdt.toString().trim());
}
@Test
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
String string =
"name, city, age \n" +
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
JSONArray result = CDL.toJSONArray(string);
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
}
@Test
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() {
JSONArray ja = new JSONArray();
ja.put("name");
ja.put("city");
ja.put("age");
String string =
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
JSONArray result = CDL.toJSONArray(ja, string);
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import org.json.Cookie;
import org.json.JSONObject;
import org.junit.Test;
public class CookieIntegrationTest {
@Test
public void givenCookieString_thenConvertToJSONObject() {
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
JSONObject cookieJO = Cookie.toJSONObject(cookie);
assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
}
@Test
public void givenJSONObject_thenConvertToCookieString() {
JSONObject cookieJO = new JSONObject();
cookieJO.put("name", "username");
cookieJO.put("value", "John Doe");
cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC");
cookieJO.put("path", "/");
String cookie = Cookie.toString(cookieJO);
assertEquals("username=John Doe;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/", cookie.toString());
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import org.json.HTTP;
import org.json.JSONObject;
import org.junit.Test;
public class HTTPIntegrationTest {
@Test
public void givenJSONObject_thenConvertToHTTPHeader() {
JSONObject jo = new JSONObject();
jo.put("Method", "POST");
jo.put("Request-URI", "http://www.example.com/");
jo.put("HTTP-Version", "HTTP/1.1");
assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo));
}
@Test
public void givenHTTPHeader_thenConvertToJSONObject() {
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString());
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
public class JSONArrayIntegrationTest {
@Test
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
JSONArray ja = new JSONArray();
ja.put(Boolean.TRUE);
ja.put("lorem ipsum");
// We can also put a JSONObject in JSONArray
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
ja.put(jo);
assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString());
}
@Test
public void givenJsonString_thenCreateNewJSONArray() {
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
assertEquals("[true,\"lorem ipsum\",215]", ja.toString());
}
@Test
public void givenListObject_thenConvertItToJSONArray() {
List<String> list = new ArrayList<>();
list.add("California");
list.add("Texas");
list.add("Hawaii");
list.add("Alaska");
JSONArray ja = new JSONArray(list);
assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString());
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.junit.Test;
public class JSONObjectIntegrationTest {
@Test
public void givenJSONJava_thenCreateNewJSONObject() {
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
}
@Test
public void givenMapObject_thenCreateJSONObject() {
Map<String, String> map = new HashMap<>();
map.put("name", "jon doe");
map.put("age", "22");
map.put("city", "chicago");
JSONObject jo = new JSONObject(map);
assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString());
}
@Test
public void givenJsonString_thenCreateJSONObject() {
JSONObject jo = new JSONObject(
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
);
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
}
@Test
public void givenDemoBean_thenCreateJSONObject() {
DemoBean demo = new DemoBean();
demo.setId(1);
demo.setName("lorem ipsum");
demo.setActive(true);
JSONObject jo = new JSONObject(demo);
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import org.json.JSONTokener;
import org.junit.Test;
public class JSONTokenerIntegrationTest {
@Test
public void givenString_convertItToJSONTokens() {
String str = "Sample String";
JSONTokener jt = new JSONTokener(str);
char[] expectedTokens = str.toCharArray();
int index = 0;
while(jt.more()) {
assertEquals(expectedTokens[index++], jt.next());
}
}
}