Return JSON data as Map

pull/81/head
Richard Körber 2019-04-19 15:19:02 +02:00
parent 9a22a74429
commit c0d6bfb057
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
2 changed files with 21 additions and 0 deletions

View File

@ -172,6 +172,15 @@ public final class JSON implements Serializable {
return JsonUtil.toJson(data);
}
/**
* Returns the content as unmodifiable Map.
*
* @since 2.8
*/
public Map<String,Object> toMap() {
return Collections.unmodifiableMap(data);
}
/**
* Serialize the data map in JSON.
*/

View File

@ -35,6 +35,7 @@ import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
@ -58,6 +59,7 @@ public class JSONTest {
public void testEmpty() {
JSON empty = JSON.empty();
assertThat(empty.toString(), is("{}"));
assertThat(empty.toMap().keySet(), is(empty()));
}
/**
@ -69,10 +71,20 @@ public class JSONTest {
JSON fromString = JSON.parse(json);
assertThat(fromString.toString(), is(sameJSONAs(json)));
Map<String, Object> map = fromString.toMap();
assertThat(map.size(), is(2));
assertThat(map.keySet(), containsInAnyOrder("foo", "bar"));
assertThat(map.get("foo"), is("a-text"));
assertThat(map.get("bar"), is(123L));
try (InputStream in = new ByteArrayInputStream(json.getBytes("utf-8"))) {
JSON fromStream = JSON.parse(in);
assertThat(fromStream.toString(), is(sameJSONAs(json)));
Map<String, Object> map2 = fromStream.toMap();
assertThat(map2.size(), is(2));
assertThat(map2.keySet(), containsInAnyOrder("foo", "bar"));
assertThat(map2.get("foo"), is("a-text"));
assertThat(map2.get("bar"), is(123L));
}
}