blob: 7e3b35e5f3ed271b5266a6e165e67ddbc0c56549 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.json;
2
3import java.io.*;
4import java.lang.reflect.*;
5import java.util.*;
6
7public class ArrayHandler extends Handler {
8 Type componentType;
9
Stuart McCullochd4826102012-06-26 16:34:24 +000010 ArrayHandler(@SuppressWarnings("unused") Class< ? > rawClass, Type componentType) {
Stuart McCullochbb014372012-06-07 21:57:32 +000011 this.componentType = componentType;
12 }
13
14 @Override
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +000016 app.append("[");
17 String del = "";
18 int l = Array.getLength(object);
19 for (int i = 0; i < l; i++) {
20 app.append(del);
21 app.encode(Array.get(object, i), componentType, visited);
22 del = ",";
23 }
24 app.append("]");
25 }
26
27 @Override
28 Object decodeArray(Decoder r) throws Exception {
29 ArrayList<Object> list = new ArrayList<Object>();
30 r.codec.parseArray(list, componentType, r);
Stuart McCulloch2286f232012-06-15 13:27:53 +000031 Object array = Array.newInstance(r.codec.getRawClass(componentType), list.size());
Stuart McCullochbb014372012-06-07 21:57:32 +000032 int n = 0;
33 for (Object o : list)
34 Array.set(array, n++, o);
35
36 return array;
37 }
38}