- package net.junisphere.eranger.test;
- import static org.junit.Assert.*;
- import static org.springframework.util.FileCopyUtils.*;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.codehaus.jackson.JsonParseException;
- import org.codehaus.jackson.map.JsonMappingException;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.junit.Test;
- public class JacksonTests {
- private static class Foo {
- private String name;
- private int i;
- private double pi;
- private Map<String, Object> properties = new HashMap<String, Object>();
- public void setPi(double pi) {
- this.pi = pi;
- }
- public double getPi() {
- return pi;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public int getI() {
- return i;
- }
- public void setI(int i) {
- this.i = i;
- }
- public Map<String, Object> getProperties() {
- Map<String, Object> result = new HashMap<String, Object>();
- for (String key : this.properties.keySet()) {
- String[] parts = key.split("\\.");
- Map<String, Object> currentMap = result;
- for (int i = 0; i < parts.length - 1; i++) {
- String part = parts[i];
- Map<String, Object> childMap = null;
- if (currentMap.containsKey(part)) {
- childMap = (Map<String, Object>) currentMap.get(part);
- } else {
- childMap = new HashMap<String, Object>();
- currentMap.put(part, childMap);
- }
- currentMap = childMap;
- }
- String part = parts[parts.length - 1];
- currentMap.put(part, this.properties.get(part));
- }
- return result;
- }
- private String dottedNotation(List<String> prefix, String key) {
- StringBuilder sb = new StringBuilder();
- for (String s : prefix) {
- sb.append(s);
- sb.append(".");
- }
- sb.append(key);
- return sb.toString();
- }
- private void processProperties(List<String> prefix, Map<String, Object> map) {
- for (String key : map.keySet()) {
- Object o = map.get(key);
- if (o instanceof Map<?, ?>) {
- List<String> newPrefix = new ArrayList<String>(prefix);
- newPrefix.add(key);
- processProperties(newPrefix, (Map<String, Object>) o);
- } else {
- this.properties.put(dottedNotation(prefix, key), o);
- }
- }
- }
- public void setProperties(Map<String, Object> properties) {
- List<String> prefix = new ArrayList<String>();
- processProperties(prefix, properties);
- }
- public void setRawProperties(Map<String, Object> properties) {
- this.properties = properties;
- }
- }
- @Test
- public void jsonToPojo() throws JsonParseException, JsonMappingException, IOException {
- final String json;
- InputStream is = getClass().getResourceAsStream("/foo.json");
- json = copyToString(new InputStreamReader(is));
- ObjectMapper mapper = new ObjectMapper();
- Foo foo = mapper.readValue(json, Foo.class);
- assertEquals("foo", foo.getName());
- assertEquals(99, foo.getI());
- assertEquals(3.1415, foo.getPi(), 0.00000001);
- System.out.println(foo.getProperties());
- }
- @Test
- public void pojoToJson() throws JsonParseException, JsonMappingException, IOException {
- Foo foo = new Foo();
- foo.setI(99);
- foo.setPi(3.1415);
- foo.setName("foo");
- Map<String, Object> bar = new HashMap<String, Object>();
- bar.put("a", 1);
- bar.put("b", 2);
- Map<String, Object> baz = new HashMap<String, Object>();
- baz.put("x", 10);
- baz.put("y", 11);
- Map<String, Object> bounds = new HashMap<String, Object>();
- bounds.put("bar", bar);
- bounds.put("baz", baz);
- Map<String, Object> properties = new HashMap<String, Object>();
- properties.put("bounds", bounds);
- foo.setRawProperties(properties);
- System.out.println("====");
- System.out.println(properties);
- ObjectMapper mapper = new ObjectMapper();
- String json = mapper.writeValueAsString(foo);
- System.out.println(json);
- // How to test if the string is correct?
- }
- }