1 package net.minidev.json.writer;
2
3 /*
4  *    Copyright 2011 JSON-SMART authors
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import java.lang.reflect.Array;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class ArraysMapper<T> extends JsonReaderI<T> {
23     public ArraysMapper(JsonReader base) {
24         super(base);
25     }
26
27     @Override
28     public Object createArray() {
29         return new ArrayList<Object>();
30     }
31
32     @SuppressWarnings("unchecked")
33     @Override
34     public void addValue(Object current, Object value) {
35         ((List<Object>) current).add(value);
36     }
37
38     @SuppressWarnings("unchecked")
39     @Override
40     public T convert(Object current) {
41         return (T) current;
42     }
43
44     public static class GenericMapper<T> extends ArraysMapper<T> {
45         final Class<?> componentType;
46         JsonReaderI<?> subMapper;
47
48         public GenericMapper(JsonReader base, Class<T> type) {
49             super(base);
50             this.componentType = type.getComponentType();
51         }
52
53         @SuppressWarnings("unchecked")
54         @Override
55         public T convert(Object current) {
56             int p = 0;
57
58             Object[] r = (Object[]) Array.newInstance(componentType, ((List<?>) current).size());
59             for (Object e : ((List<?>) current))
60                 r[p++] = e;
61             return (T) r;
62         }
63
64         @Override
65         public JsonReaderI<?> startArray(String key) {
66             if (subMapper == null)
67                 subMapper = base.getMapper(componentType);
68             return subMapper;
69         }
70
71         @Override
72         public JsonReaderI<?> startObject(String key) {
73             if (subMapper == null)
74                 subMapper = base.getMapper(componentType);
75             return subMapper;
76         }
77     };
78
79     public static JsonReaderI<int[]> MAPPER_PRIM_INT = new ArraysMapper<int[]>(null) {
80         @Override
81         public int[] convert(Object current) {
82             int p = 0;
83             int[] r = new int[((List<?>) current).size()];
84             for (Object e : ((List<?>) current))
85                 r[p++] = ((Number) e).intValue();
86             return r;
87         }
88     };
89
90     public static JsonReaderI<Integer[]> MAPPER_INT = new ArraysMapper<Integer[]>(null) {
91         @Override
92         public Integer[] convert(Object current) {
93             int p = 0;
94             Integer[] r = new Integer[((List<?>) current).size()];
95             for (Object e : ((List<?>) current)) {
96                 if (e == null)
97                     continue;
98                 if (e instanceof Integer)
99                     r[p] = (Integer) e;
100                 else
101                     r[p] = ((Number) e).intValue();
102                 p++;
103             }
104             return r;
105         }
106     };
107
108     public static JsonReaderI<short[]> MAPPER_PRIM_SHORT = new ArraysMapper<short[]>(null) {
109         @Override
110         public short[] convert(Object current) {
111             int p = 0;
112             short[] r = new short[((List<?>) current).size()];
113             for (Object e : ((List<?>) current))
114                 r[p++] = ((Number) e).shortValue();
115             return r;
116         }
117     };
118
119     public static JsonReaderI<Short[]> MAPPER_SHORT = new ArraysMapper<Short[]>(null) {
120         @Override
121         public Short[] convert(Object current) {
122             int p = 0;
123             Short[] r = new Short[((List<?>) current).size()];
124             for (Object e : ((List<?>) current)) {
125                 if (e == null)
126                     continue;
127                 if (e instanceof Short)
128                     r[p] = (Short) e;
129                 else
130                     r[p] = ((Number) e).shortValue();
131                 p++;
132             }
133             return r;
134         }
135     };
136
137     public static JsonReaderI<byte[]> MAPPER_PRIM_BYTE = new ArraysMapper<byte[]>(null) {
138         @Override
139         public byte[] convert(Object current) {
140             int p = 0;
141             byte[] r = new byte[((List<?>) current).size()];
142             for (Object e : ((List<?>) current))
143                 r[p++] = ((Number) e).byteValue();
144             return r;
145         }
146     };
147
148     public static JsonReaderI<Byte[]> MAPPER_BYTE = new ArraysMapper<Byte[]>(null) {
149         @Override
150         public Byte[] convert(Object current) {
151             int p = 0;
152             Byte[] r = new Byte[((List<?>) current).size()];
153             for (Object e : ((List<?>) current)) {
154                 if (e == null)
155                     continue;
156                 if (e instanceof Byte)
157                     r[p] = (Byte) e;
158                 else
159                     r[p] = ((Number) e).byteValue();
160                 p++;
161             }
162             return r;
163         }
164     };
165
166     public static JsonReaderI<char[]> MAPPER_PRIM_CHAR = new ArraysMapper<char[]>(null) {
167         @Override
168         public char[] convert(Object current) {
169             int p = 0;
170             char[] r = new char[((List<?>) current).size()];
171             for (Object e : ((List<?>) current))
172                 r[p++] = e.toString().charAt(0);
173             return r;
174         }
175     };
176
177     public static JsonReaderI<Character[]> MAPPER_CHAR = new ArraysMapper<Character[]>(null) {
178         @Override
179         public Character[] convert(Object current) {
180             int p = 0;
181             Character[] r = new Character[((List<?>) current).size()];
182             for (Object e : ((List<?>) current)) {
183                 if (e == null)
184                     continue;
185                 r[p] = e.toString().charAt(0);
186                 p++;
187             }
188             return r;
189         }
190     };
191
192     public static JsonReaderI<long[]> MAPPER_PRIM_LONG = new ArraysMapper<long[]>(null) {
193         @Override
194         public long[] convert(Object current) {
195             int p = 0;
196             long[] r = new long[((List<?>) current).size()];
197             for (Object e : ((List<?>) current))
198                 r[p++] = ((Number) e).intValue();
199             return r;
200         }
201     };
202
203     public static JsonReaderI<Long[]> MAPPER_LONG = new ArraysMapper<Long[]>(null) {
204         @Override
205         public Long[] convert(Object current) {
206             int p = 0;
207             Long[] r = new Long[((List<?>) current).size()];
208             for (Object e : ((List<?>) current)) {
209                 if (e == null)
210                     continue;
211                 if (e instanceof Float)
212                     r[p] = ((Long) e);
213                 else
214                     r[p] = ((Number) e).longValue();
215                 p++;
216             }
217             return r;
218         }
219     };
220
221     public static JsonReaderI<float[]> MAPPER_PRIM_FLOAT = new ArraysMapper<float[]>(null) {
222         @Override
223         public float[] convert(Object current) {
224             int p = 0;
225             float[] r = new float[((List<?>) current).size()];
226             for (Object e : ((List<?>) current))
227                 r[p++] = ((Number) e).floatValue();
228             return r;
229         }
230     };
231
232     public static JsonReaderI<Float[]> MAPPER_FLOAT = new ArraysMapper<Float[]>(null) {
233         @Override
234         public Float[] convert(Object current) {
235             int p = 0;
236             Float[] r = new Float[((List<?>) current).size()];
237             for (Object e : ((List<?>) current)) {
238                 if (e == null)
239                     continue;
240                 if (e instanceof Float)
241                     r[p] = ((Float) e);
242                 else
243                     r[p] = ((Number) e).floatValue();
244                 p++;
245             }
246             return r;
247         }
248     };
249
250     public static JsonReaderI<double[]> MAPPER_PRIM_DOUBLE = new ArraysMapper<double[]>(null) {
251         @Override
252         public double[] convert(Object current) {
253             int p = 0;
254             double[] r = new double[((List<?>) current).size()];
255             for (Object e : ((List<?>) current))
256                 r[p++] = ((Number) e).doubleValue();
257             return r;
258         }
259     };
260
261     public static JsonReaderI<Double[]> MAPPER_DOUBLE = new ArraysMapper<Double[]>(null) {
262         @Override
263         public Double[] convert(Object current) {
264             int p = 0;
265             Double[] r = new Double[((List<?>) current).size()];
266             for (Object e : ((List<?>) current)) {
267                 if (e == null)
268                     continue;
269                 if (e instanceof Double)
270                     r[p] = ((Double) e);
271                 else
272                     r[p] = ((Number) e).doubleValue();
273                 p++;
274             }
275             return r;
276         }
277     };
278
279     public static JsonReaderI<boolean[]> MAPPER_PRIM_BOOL = new ArraysMapper<boolean[]>(null) {
280         @Override
281         public boolean[] convert(Object current) {
282             int p = 0;
283             boolean[] r = new boolean[((List<?>) current).size()];
284             for (Object e : ((List<?>) current))
285                 r[p++] = ((Boolean) e).booleanValue();
286             return r;
287         }
288     };
289
290     public static JsonReaderI<Boolean[]> MAPPER_BOOL = new ArraysMapper<Boolean[]>(null) {
291         @Override
292         public Boolean[] convert(Object current) {
293             int p = 0;
294             Boolean[] r = new Boolean[((List<?>) current).size()];
295             for (Object e : ((List<?>) current)) {
296                 if (e == null)
297                     continue;
298                 if (e instanceof Boolean)
299                     r[p] = ((Boolean) e).booleanValue();
300                 else if (e instanceof Number)
301                     r[p] = ((Number) e).intValue() != 0;
302                 else
303                     throw new RuntimeException("can not convert " + e + " toBoolean");
304                 p++;
305             }
306             return r;
307         }
308     };
309 }
310