Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<version>${fastjson.version}</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
</dependencies>

<properties>
Expand Down
59 changes: 59 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/CDLDemo.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
30 changes: 30 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/CookieDemo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/DemoBean.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 52 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
59 changes: 59 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
52 changes: 52 additions & 0 deletions json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
25 changes: 25 additions & 0 deletions json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading