Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.json; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.reflect.*; |
| 5 | import java.util.*; |
| 6 | |
| 7 | public class MapHandler extends Handler { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | final Class< ? > rawClass; |
| 9 | final Type keyType; |
| 10 | final Type valueType; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | MapHandler(Class< ? > rawClass, Type keyType, Type valueType) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | this.keyType = keyType; |
| 14 | this.valueType = valueType; |
| 15 | if (rawClass.isInterface()) { |
| 16 | if (rawClass.isAssignableFrom(HashMap.class)) |
| 17 | rawClass = HashMap.class; |
| 18 | else if (rawClass.isAssignableFrom(TreeMap.class)) |
| 19 | rawClass = TreeMap.class; |
| 20 | else if (rawClass.isAssignableFrom(Hashtable.class)) |
| 21 | rawClass = Hashtable.class; |
| 22 | else if (rawClass.isAssignableFrom(LinkedHashMap.class)) |
| 23 | rawClass = LinkedHashMap.class; |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 24 | else if (rawClass.isAssignableFrom(Dictionary.class)) |
| 25 | rawClass = Hashtable.class; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | else |
| 27 | throw new IllegalArgumentException("Unknown map interface: " + rawClass); |
| 28 | } |
| 29 | this.rawClass = rawClass; |
| 30 | } |
| 31 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | @Override |
| 33 | void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { |
| 34 | Map< ? , ? > map = (Map< ? , ? >) object; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
| 36 | app.append("{"); |
| 37 | String del = ""; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | for (Map.Entry< ? , ? > e : map.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | app.append(del); |
| 40 | String key; |
| 41 | if (e.getKey() != null && (keyType == String.class || keyType == Object.class)) |
| 42 | key = e.getKey().toString(); |
| 43 | else { |
| 44 | key = app.codec.enc().put(e.getKey()).toString(); |
| 45 | } |
| 46 | StringHandler.string(app, key); |
| 47 | app.append(":"); |
| 48 | app.encode(e.getValue(), valueType, visited); |
| 49 | del = ","; |
| 50 | } |
| 51 | app.append("}"); |
| 52 | } |
| 53 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 54 | @Override |
| 55 | Object decodeObject(Decoder r) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | assert r.current() == '{'; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 57 | |
| 58 | Map<Object,Object> map = (Map<Object,Object>) rawClass.newInstance(); |
| 59 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | int c = r.next(); |
| 61 | while (JSONCodec.START_CHARACTERS.indexOf(c) >= 0) { |
| 62 | Object key = r.codec.parseString(r); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 63 | if (!(keyType == null || keyType == Object.class)) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 64 | Handler h = r.codec.getHandler(keyType); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 65 | key = h.decode((String) key); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 67 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 68 | c = r.skipWs(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 69 | if (c != ':') |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 70 | throw new IllegalArgumentException("Expected ':' but got " + (char) c); |
| 71 | |
| 72 | c = r.next(); |
| 73 | Object value = r.codec.decode(valueType, r); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 74 | if (value != null || !r.codec.ignorenull) |
Stuart McCulloch | 285034f | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 75 | map.put(key, value); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 76 | |
| 77 | c = r.skipWs(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | |
| 79 | if (c == '}') |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 80 | break; |
| 81 | |
| 82 | if (c == ',') { |
| 83 | c = r.next(); |
| 84 | continue; |
| 85 | } |
| 86 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 87 | throw new IllegalArgumentException("Invalid character in parsing list, expected } or , but found " |
| 88 | + (char) c); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 89 | } |
| 90 | assert r.current() == '}'; |
| 91 | r.read(); // skip closing |
| 92 | return map; |
| 93 | } |
| 94 | |
| 95 | } |