Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 4.76 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package net.junisphere.eranger.test;
  2.  
  3. import static org.junit.Assert.*;
  4. import static org.springframework.util.FileCopyUtils.*;
  5.  
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13.  
  14. import org.codehaus.jackson.JsonParseException;
  15. import org.codehaus.jackson.map.JsonMappingException;
  16. import org.codehaus.jackson.map.ObjectMapper;
  17. import org.junit.Test;
  18.  
  19. public class JacksonTests {
  20.  
  21.     private static class Foo {
  22.         private String name;
  23.         private int i;
  24.         private double pi;
  25.         private Map<String, Object> properties = new HashMap<String, Object>();
  26.  
  27.         public void setPi(double pi) {
  28.             this.pi = pi;
  29.         }
  30.  
  31.         public double getPi() {
  32.             return pi;
  33.         }
  34.  
  35.         public void setName(String name) {
  36.             this.name = name;
  37.         }
  38.  
  39.         public String getName() {
  40.             return name;
  41.         }
  42.  
  43.         public int getI() {
  44.             return i;
  45.         }
  46.  
  47.         public void setI(int i) {
  48.             this.i = i;
  49.         }
  50.  
  51.         public Map<String, Object> getProperties() {
  52.             Map<String, Object> result = new HashMap<String, Object>();
  53.             for (String key : this.properties.keySet()) {
  54.                 String[] parts = key.split("\\.");
  55.                 Map<String, Object> currentMap = result;
  56.                 for (int i = 0; i < parts.length - 1; i++) {
  57.                     String part = parts[i];
  58.                     Map<String, Object> childMap = null;
  59.                     if (currentMap.containsKey(part)) {
  60.                         childMap = (Map<String, Object>) currentMap.get(part);
  61.                     } else {
  62.                         childMap = new HashMap<String, Object>();
  63.                         currentMap.put(part, childMap);
  64.                     }
  65.                     currentMap = childMap;
  66.                 }
  67.                 String part = parts[parts.length - 1];
  68.                 currentMap.put(part, this.properties.get(part));
  69.             }
  70.             return result;
  71.         }
  72.  
  73.         private String dottedNotation(List<String> prefix, String key) {
  74.             StringBuilder sb = new StringBuilder();
  75.             for (String s : prefix) {
  76.                 sb.append(s);
  77.                 sb.append(".");
  78.             }
  79.             sb.append(key);
  80.  
  81.             return sb.toString();
  82.         }
  83.  
  84.         private void processProperties(List<String> prefix, Map<String, Object> map) {
  85.             for (String key : map.keySet()) {
  86.                 Object o = map.get(key);
  87.                 if (o instanceof Map<?, ?>) {
  88.                     List<String> newPrefix = new ArrayList<String>(prefix);
  89.                     newPrefix.add(key);
  90.                     processProperties(newPrefix, (Map<String, Object>) o);
  91.                 } else {
  92.                     this.properties.put(dottedNotation(prefix, key), o);
  93.                 }
  94.             }
  95.         }
  96.  
  97.         public void setProperties(Map<String, Object> properties) {
  98.             List<String> prefix = new ArrayList<String>();
  99.             processProperties(prefix, properties);
  100.         }
  101.  
  102.         public void setRawProperties(Map<String, Object> properties) {
  103.             this.properties = properties;
  104.         }
  105.     }
  106.  
  107.     @Test
  108.     public void jsonToPojo() throws JsonParseException, JsonMappingException, IOException {
  109.         final String json;
  110.         InputStream is = getClass().getResourceAsStream("/foo.json");
  111.         json = copyToString(new InputStreamReader(is));
  112.  
  113.         ObjectMapper mapper = new ObjectMapper();
  114.         Foo foo = mapper.readValue(json, Foo.class);
  115.         assertEquals("foo", foo.getName());
  116.         assertEquals(99, foo.getI());
  117.         assertEquals(3.1415, foo.getPi(), 0.00000001);
  118.         System.out.println(foo.getProperties());
  119.     }
  120.  
  121.     @Test
  122.     public void pojoToJson() throws JsonParseException, JsonMappingException, IOException {
  123.         Foo foo = new Foo();
  124.         foo.setI(99);
  125.         foo.setPi(3.1415);
  126.         foo.setName("foo");
  127.         Map<String, Object> bar = new HashMap<String, Object>();
  128.         bar.put("a", 1);
  129.         bar.put("b", 2);
  130.  
  131.         Map<String, Object> baz = new HashMap<String, Object>();
  132.         baz.put("x", 10);
  133.         baz.put("y", 11);
  134.  
  135.         Map<String, Object> bounds = new HashMap<String, Object>();
  136.         bounds.put("bar", bar);
  137.         bounds.put("baz", baz);
  138.  
  139.         Map<String, Object> properties = new HashMap<String, Object>();
  140.         properties.put("bounds", bounds);
  141.         foo.setRawProperties(properties);
  142.  
  143.         System.out.println("====");
  144.         System.out.println(properties);
  145.  
  146.         ObjectMapper mapper = new ObjectMapper();
  147.         String json = mapper.writeValueAsString(foo);
  148.         System.out.println(json);
  149.  
  150.         // How to test if the string is correct?
  151.     }
  152. }