Java 11497 (#12399)
* Added/created parent module (json-modules) * moved json(submodule) to json-modules(parent) * moved json-2(submodule) to json-modules(parent) * moved json-path(submodule) to json-modules(parent) * moved gson(submodule) to json-modules(parent) * deleted sub-modules that we moved to json-modules Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.escape;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class JsonEscapeUnitTest {
|
||||
|
||||
private JsonEscape testedInstance;
|
||||
private static final String EXPECTED = "{\"message\":\"Hello \\\"World\\\"\"}";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testedInstance = new JsonEscape();
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson() {
|
||||
String actual = testedInstance.escapeJson("Hello \"World\"");
|
||||
assertEquals(EXPECTED, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeGson() {
|
||||
String actual = testedInstance.escapeGson("Hello \"World\"");
|
||||
assertEquals(EXPECTED, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJackson() throws JsonProcessingException {
|
||||
String actual = testedInstance.escapeJackson("Hello \"World\"");
|
||||
assertEquals(EXPECTED, actual);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.json.schema;
|
||||
|
||||
import org.everit.json.schema.Schema;
|
||||
import org.everit.json.schema.ValidationException;
|
||||
import org.everit.json.schema.loader.SchemaLoader;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONSchemaUnitTest {
|
||||
|
||||
@Test(expected = ValidationException.class)
|
||||
public void givenInvalidInput_whenValidating_thenInvalid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_invalid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_whenValidating_thenValid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.baeldung.jsonb;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.json.bind.Jsonb;
|
||||
import javax.json.bind.JsonbBuilder;
|
||||
import javax.json.bind.JsonbConfig;
|
||||
import javax.json.bind.config.PropertyNamingStrategy;
|
||||
import javax.json.bind.config.PropertyOrderStrategy;
|
||||
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.adapter.PersonAdapter;
|
||||
import com.baeldung.jsonb.Person;
|
||||
|
||||
public class JsonbUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() {
|
||||
Jsonb jsonb = JsonbBuilder.create();
|
||||
// @formatter:off
|
||||
List<Person> personList = Arrays.asList(
|
||||
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
||||
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
|
||||
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
||||
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
|
||||
// @formatter:on
|
||||
String jsonArrayPerson = jsonb.toJson(personList);
|
||||
// @formatter:off
|
||||
String jsonExpected = "[" +
|
||||
"{\"email\":\"jhon@test.com\"," +
|
||||
"\"id\":1,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}," +
|
||||
"{\"email\":\"jhon1@test.com\"," +
|
||||
"\"id\":2,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1500.0\"},{\"email\":null," +
|
||||
"\"id\":3,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}," +
|
||||
"{\"email\":\"tom@test.com\"," +
|
||||
"\"id\":4,\"person-name\":\"Tom\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1500.0\"}"
|
||||
+ "]";
|
||||
// @formatter:on
|
||||
assertTrue(jsonArrayPerson.equals(jsonExpected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonJsonArray_whenDeserializeWithJsonb_thenGetPersonList() {
|
||||
Jsonb jsonb = JsonbBuilder.create();
|
||||
// @formatter:off
|
||||
String personJsonArray =
|
||||
"[" +
|
||||
"{\"email\":\"jhon@test.com\"," +
|
||||
"\"id\":1,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}," +
|
||||
"{\"email\":\"jhon1@test.com\"," +
|
||||
"\"id\":2,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1500.0\"},{\"email\":null," +
|
||||
"\"id\":3,\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}," +
|
||||
"{\"email\":\"tom@test.com\"," +
|
||||
"\"id\":4,\"person-name\":\"Tom\"," +
|
||||
"\"registeredDate\":\"09-09-2019\"," +
|
||||
"\"salary\":\"1500.0\"}"
|
||||
+ "]";
|
||||
// @formatter:on
|
||||
@SuppressWarnings("serial")
|
||||
List<Person> personList = jsonb.fromJson(personJsonArray, new ArrayList<Person>() {
|
||||
}.getClass()
|
||||
.getGenericSuperclass());
|
||||
// @formatter:off
|
||||
List<Person> personListExpected = Arrays.asList(
|
||||
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
||||
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
|
||||
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
|
||||
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
|
||||
// @formatter:on
|
||||
assertTrue(ListUtils.isEqualList(personList, personListExpected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() {
|
||||
JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
|
||||
Jsonb jsonb = JsonbBuilder.create(config);
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
|
||||
String jsonPerson = jsonb.toJson(person);
|
||||
// @formatter:off
|
||||
String jsonExpected =
|
||||
"{\"email\":\"jhon@test.com\"," +
|
||||
"\"id\":1," +
|
||||
"\"person-name\":\"Jhon\"," +
|
||||
"\"registered_date\":\"07-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonExpected.equals(jsonPerson));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson() {
|
||||
JsonbConfig config = new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);
|
||||
Jsonb jsonb = JsonbBuilder.create(config);
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
|
||||
String jsonPerson = jsonb.toJson(person);
|
||||
// @formatter:off
|
||||
String jsonExpected =
|
||||
"{\"salary\":\"1000.0\","+
|
||||
"\"registeredDate\":\"07-09-2019\"," +
|
||||
"\"person-name\":\"Jhon\"," +
|
||||
"\"id\":1," +
|
||||
"\"email\":\"jhon@test.com\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonExpected.equals(jsonPerson));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
|
||||
Jsonb jsonb = JsonbBuilder.create();
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
|
||||
String jsonPerson = jsonb.toJson(person);
|
||||
// @formatter:off
|
||||
String jsonExpected =
|
||||
"{\"email\":\"jhon@test.com\"," +
|
||||
"\"id\":1," +
|
||||
"\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"07-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonExpected.equals(jsonPerson));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
|
||||
Jsonb jsonb = JsonbBuilder.create();
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
|
||||
// @formatter:off
|
||||
String jsonPerson =
|
||||
"{\"email\":\"jhon@test.com\"," +
|
||||
"\"id\":1," +
|
||||
"\"person-name\":\"Jhon\"," +
|
||||
"\"registeredDate\":\"07-09-2019\"," +
|
||||
"\"salary\":\"1000.0\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
|
||||
.equals(person));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonObject_whenSerializeWithAdapter_thenGetPersonJson() {
|
||||
JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
|
||||
Jsonb jsonb = JsonbBuilder.create(config);
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));// new Person(1, "Jhon");
|
||||
String jsonPerson = jsonb.toJson(person);
|
||||
// @formatter:off
|
||||
String jsonExpected =
|
||||
"{\"id\":1," +
|
||||
"\"name\":\"Jhon\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonExpected.equals(jsonPerson));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonJson_whenDeserializeWithAdapter_thenGetPersonObject() {
|
||||
JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
|
||||
Jsonb jsonb = JsonbBuilder.create(config);
|
||||
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));// new Person(1, "Jhon");
|
||||
// @formatter:off
|
||||
String jsonPerson =
|
||||
"{\"id\":1," +
|
||||
"\"name\":\"Jhon\"}";
|
||||
// @formatter:on
|
||||
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
|
||||
.equals(person));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.CDL;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CDLIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenCommaDelimitedText_thenConvertToJSONArray() {
|
||||
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
||||
|
||||
assertThatJson(ja)
|
||||
.isEqualTo("[\"England\",\"USA\",\"Canada\"]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJSONArray_thenConvertToCommaDelimitedText() {
|
||||
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
||||
|
||||
String cdt = CDL.rowToString(ja);
|
||||
|
||||
assertThat(cdt.trim()).isEqualTo("England,USA,Canada");
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
assertThatJson(result)
|
||||
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
assertThatJson(result)
|
||||
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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);
|
||||
|
||||
assertThatJson(cookieJO)
|
||||
.isEqualTo("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}");
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
assertThat(cookie.split(";"))
|
||||
.containsExactlyInAnyOrder("username=John Doe", "path=/", "expires=Thu, 18 Dec 2013 12:00:00 UTC");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.HTTP;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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");
|
||||
|
||||
assertThat(HTTP.toString(jo))
|
||||
.isEqualTo("POST \"http://www.example.com/\" HTTP/1.1" + HTTP.CRLF + HTTP.CRLF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHTTPHeader_thenConvertToJSONObject() {
|
||||
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
|
||||
|
||||
assertThatJson(obj)
|
||||
.isEqualTo("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}");
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JSONArrayGetValueByKeyUnitTest {
|
||||
|
||||
private static final JSONArrayGetValueByKey obj = new JSONArrayGetValueByKey();
|
||||
|
||||
@Test
|
||||
public void givenJSONArrayAndAKey_thenReturnAllValuesForGivenKey() {
|
||||
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
|
||||
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
|
||||
|
||||
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
|
||||
|
||||
assertThat(actualValues)
|
||||
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJSONArrayAndAKey_whenUsingJava8Syntax_thenReturnAllValuesForGivenKey() {
|
||||
String jsonStr = "[" + " {" + " \"name\": \"John\"," + " \"city\": \"chicago\"," + " \"age\": \"22\" " + "}," + " { " + "\"name\": \"Gary\"," + " \"city\": \"florida\"," + " \"age\": \"35\" " + "}," + " { " + "\"name\": \"Selena\","
|
||||
+ " \"city\": \"vegas\"," + " \"age\": \"18\" " + "} " + "]";
|
||||
|
||||
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
|
||||
|
||||
assertThat(actualValues)
|
||||
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
|
||||
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);
|
||||
|
||||
assertThatJson(ja)
|
||||
.isEqualTo("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonString_thenCreateNewJSONArray() {
|
||||
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
|
||||
|
||||
assertThatJson(ja)
|
||||
.isEqualTo("[true,\"lorem ipsum\",215]");
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
assertThatJson(ja)
|
||||
.isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
|
||||
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");
|
||||
|
||||
assertThatJson(jo)
|
||||
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
assertThatJson(jo)
|
||||
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonString_thenCreateJSONObject() {
|
||||
JSONObject jo = new JSONObject(
|
||||
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
|
||||
);
|
||||
|
||||
assertThatJson(jo)
|
||||
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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()) {
|
||||
assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
|
||||
public class ObjectToFromJSONIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenDemoBean_thenCreateJSONObject() {
|
||||
DemoBean demo = new DemoBean();
|
||||
demo.setId(1);
|
||||
demo.setName("lorem ipsum");
|
||||
demo.setActive(true);
|
||||
|
||||
JSONObject jo = new JSONObject(demo);
|
||||
|
||||
assertThatJson(jo)
|
||||
.isEqualTo("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}");
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.jsonobject.iterate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONObjectIteratorUnitTest {
|
||||
|
||||
private JSONObjectIterator jsonObjectIterator = new JSONObjectIterator();
|
||||
|
||||
@Test
|
||||
public void givenJSONObject_whenIterating_thenGetKeyValuePairs() {
|
||||
JSONObject jsonObject = getJsonObject();
|
||||
|
||||
jsonObjectIterator.handleJSONObject(jsonObject);
|
||||
|
||||
Map<String, Object> keyValuePairs = jsonObjectIterator.getKeyValuePairs();
|
||||
assertThat(keyValuePairs.get("rType")).isEqualTo("Regular");
|
||||
assertThat(keyValuePairs.get("rId")).isEqualTo("1001");
|
||||
assertThat(keyValuePairs.get("cType")).isEqualTo("Chocolate");
|
||||
assertThat(keyValuePairs.get("cId")).isEqualTo("1002");
|
||||
assertThat(keyValuePairs.get("bType")).isEqualTo("BlueBerry");
|
||||
assertThat(keyValuePairs.get("bId")).isEqualTo("1003");
|
||||
assertThat(keyValuePairs.get("name")).isEqualTo("Cake");
|
||||
assertThat(keyValuePairs.get("cakeId")).isEqualTo("0001");
|
||||
assertThat(keyValuePairs.get("type")).isEqualTo("donut");
|
||||
assertThat(keyValuePairs.get("Type")).isEqualTo("Maple");
|
||||
assertThat(keyValuePairs.get("tId")).isEqualTo("5001");
|
||||
assertThat(keyValuePairs.get("batters")
|
||||
.toString()).isEqualTo("[{\"rType\":\"Regular\",\"rId\":\"1001\"},{\"cType\":\"Chocolate\",\"cId\":\"1002\"},{\"bType\":\"BlueBerry\",\"bId\":\"1003\"}]");
|
||||
assertThat(keyValuePairs.get("cakeShapes")
|
||||
.toString()).isEqualTo("[\"square\",\"circle\",\"heart\"]");
|
||||
assertThat(keyValuePairs.get("topping")
|
||||
.toString()).isEqualTo("{\"Type\":\"Maple\",\"tId\":\"5001\"}");
|
||||
}
|
||||
|
||||
private JSONObject getJsonObject() {
|
||||
JSONObject cake = new JSONObject();
|
||||
cake.put("cakeId", "0001");
|
||||
cake.put("type", "donut");
|
||||
cake.put("name", "Cake");
|
||||
|
||||
JSONArray batters = new JSONArray();
|
||||
JSONObject regular = new JSONObject();
|
||||
regular.put("rId", "1001");
|
||||
regular.put("rType", "Regular");
|
||||
batters.put(regular);
|
||||
JSONObject chocolate = new JSONObject();
|
||||
chocolate.put("cId", "1002");
|
||||
chocolate.put("cType", "Chocolate");
|
||||
batters.put(chocolate);
|
||||
JSONObject blueberry = new JSONObject();
|
||||
blueberry.put("bId", "1003");
|
||||
blueberry.put("bType", "BlueBerry");
|
||||
batters.put(blueberry);
|
||||
|
||||
JSONArray cakeShapes = new JSONArray();
|
||||
cakeShapes.put("square");
|
||||
cakeShapes.put("circle");
|
||||
cakeShapes.put("heart");
|
||||
|
||||
cake.put("cakeShapes", cakeShapes);
|
||||
|
||||
cake.put("batters", batters);
|
||||
|
||||
JSONObject topping = new JSONObject();
|
||||
topping.put("tId", "5001");
|
||||
topping.put("Type", "Maple");
|
||||
|
||||
cake.put("topping", topping);
|
||||
|
||||
return cake;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.jsonpointer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsonPointerCrudUnitTest {
|
||||
|
||||
@Test
|
||||
public void testJsonPointerCrudForAddress() {
|
||||
|
||||
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/address.json"));
|
||||
|
||||
assertFalse(jsonPointerCrud.check("city"));
|
||||
|
||||
// insert a value
|
||||
jsonPointerCrud.insert("city", "Rio de Janeiro");
|
||||
|
||||
assertTrue(jsonPointerCrud.check("city"));
|
||||
|
||||
// fetch full json
|
||||
String fullJSON = jsonPointerCrud.fetchFullJSON();
|
||||
|
||||
assertTrue(fullJSON.contains("name"));
|
||||
|
||||
assertTrue(fullJSON.contains("city"));
|
||||
|
||||
// fetch value
|
||||
String cityName = jsonPointerCrud.fetchValueFromKey("city");
|
||||
|
||||
assertEquals(cityName, "Rio de Janeiro");
|
||||
|
||||
// update value
|
||||
jsonPointerCrud.update("city", "Sao Paulo");
|
||||
|
||||
// fetch value
|
||||
cityName = jsonPointerCrud.fetchValueFromKey("city");
|
||||
|
||||
assertEquals(cityName, "Sao Paulo");
|
||||
|
||||
// delete
|
||||
jsonPointerCrud.delete("city");
|
||||
|
||||
assertFalse(jsonPointerCrud.check("city"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonPointerCrudForBooks() {
|
||||
|
||||
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/books.json"));
|
||||
|
||||
// fetch value
|
||||
String book = jsonPointerCrud.fetchListValues("books/1");
|
||||
|
||||
assertEquals(book, "{\"title\":\"Title 2\",\"author\":\"John Doe\"}");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "Customer 01",
|
||||
"street name": "Street 01"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"library": "My Personal Library",
|
||||
"books": [
|
||||
{ "title":"Title 1", "author":"Jane Doe" },
|
||||
{ "title":"Title 2", "author":"John Doe" }
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Lampshade",
|
||||
"price": 0
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Lampshade",
|
||||
"price": 10
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"title": "Product",
|
||||
"description": "A product from the catalog",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"description": "The unique identifier for a product",
|
||||
"type": "integer"
|
||||
},
|
||||
"name": {
|
||||
"description": "Name of the product",
|
||||
"type": "string"
|
||||
},
|
||||
"price": {
|
||||
"type": "number",
|
||||
"minimum": 0,
|
||||
"exclusiveMinimum": true
|
||||
}
|
||||
},
|
||||
"required": ["id", "name", "price"]
|
||||
}
|
||||
Reference in New Issue
Block a user