Skip to content

Commit

Permalink
Added methods to get/set arrays
Browse files Browse the repository at this point in the history
- pretty print json
- get method with generics
- tests
  • Loading branch information
ajermakovics committed Feb 27, 2017
1 parent 48464eb commit bc42584
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/andrejs/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static java.lang.reflect.Modifier.isStatic;
import static java.util.stream.Collectors.toList;

/** JSON Object **/
public class Json extends MapBindings {
Expand Down Expand Up @@ -54,6 +56,11 @@ public String toString() {
return JsonSerializer.toJsonString(toMap());
}

/** Convert to pretty JSON string with indentation **/
public String toStringPretty() {
return JsonSerializer.toPrettyString(map);
}

@Override
public boolean equals(Object it) {
return (it instanceof Json) && toMap().equals( ((Json)it).toMap() );
Expand All @@ -64,6 +71,11 @@ public int hashCode() {
return toMap().hashCode();
}

@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) map.get(key);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
/** Get a value or a default value **/
public <T> T get(String key, T defaultValue) {
Expand All @@ -83,6 +95,12 @@ public Object put(String key, Object value) {
return toMap().put(key, unwrap(value));
}

public Json putArray(String arrayField, List<?> values) {
List<Object> vals = values.stream().map(Json::unwrap).collect(toList());
put(arrayField, vals);
return this;
}

@SuppressWarnings("unchecked")
/** Get the map out of Json. Store nested Json objects as Maps **/
static <T> T unwrap(T val) {
Expand All @@ -108,6 +126,19 @@ public Json at(String ... keys) {
return (nested == MapOps.EMPTY_MAP) ? EMPTY : new Json(nested);
}

public List<Object> getArray(String key) {
return get(key);
}

public List<Json> getObjects(String key) {
List<Object> array = getArray(key);
if(array == null)
return null;
return array.stream()
.map(Json.of::bean)
.collect(toList());
}

/** Create a (deep) copy of this Json. **/
public Json copy() {
return new Json( MapOps.deepCopyMap(toMap()) );
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/andrejs/json/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ static Map<String, Object> readStream(InputStream json) {
@SuppressWarnings("unchecked")
static Map<String, Object> serialize(Object pojoBean) {
try {
if(pojoBean instanceof Map)
return (Map<String, Object>) pojoBean;
return serializer.convertValue(pojoBean, Map.class);
} catch (Exception e) {
throw new IllegalStateException(e);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/andrejs/json/MapBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public boolean containsKey(Object key) {
}

@Override
@Deprecated
public Object get(Object key) {
return toMap().get(key);
}
Expand Down
42 changes: 37 additions & 5 deletions src/test/java/org/andrejs/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.*;

Expand All @@ -22,7 +22,8 @@ public void newJsonIsEmpty() throws Exception {
public void containsConstructorKeyValue() throws Exception {
Json json = new Json("key", 123);
assertTrue( json.containsKey("key") );
assertEquals(123, json.get("key"));
int val = json.get("key");
assertEquals(123, val);
}

@Test
Expand All @@ -41,7 +42,8 @@ public void containsAnonymousClassFields() throws Exception {
int key = 123;
String key2 = "val";
};
assertEquals(123, json.get("key"));
int val = json.get("key");
assertEquals(123, val);
assertEquals("val", json.get("key2"));
}

Expand All @@ -52,7 +54,7 @@ public void createdWithHasValues() throws Exception {

@Test
public void listPropertyProducesArrayInJson() throws Exception {
Json json = new Json("key", Arrays.asList(1, 2));
Json json = new Json("key", asList(1, 2));
assertEquals("{\"key\":[1,2]}", json.toString());
}

Expand Down Expand Up @@ -111,12 +113,42 @@ public void mergeProducesCombinedJson() throws Exception {

@Test
public void copyMakesDeepCopy() throws Exception {

Json j1 = new Json("a", new Json("b", 1));
Json copy = j1.copy();

assertEquals(j1, copy);
copy.set("c", 1);
assertNotSame(j1, copy);
}

@Test
public void getArray() throws Exception {
Json json = new Json("{key: [1, 2]}");
assertEquals(asList(1, 2), json.getArray("key"));
}

@Test
public void putArray() throws Exception {
Json json = new Json().putArray("key", asList(1, 2));
Json expected = new Json("{key: [1, 2]}");

assertEquals(expected, json);
}

@Test
public void getObjects() throws Exception {
Json o1 = new Json("a", 1), o2 = new Json("a", 2);
Json json = new Json("{key: [{a: 1}, {a: 2}]}");

assertEquals(asList(o1, o2), json.getObjects("key"));
}

@Test
public void putObjects() throws Exception {
Json o1 = new Json("a", 1), o2 = new Json("a", 2);
Json expected = new Json("{key: [{a: 1}, {a: 2}]}");
Json json = new Json().putArray("key", asList(o1, o2));

assertEquals(expected, json);
}
}

0 comments on commit bc42584

Please sign in to comment.