* 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
43 lines
1.2 KiB
Java
43 lines
1.2 KiB
Java
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());
|
|
}
|
|
}
|