01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19:
20: package de.schlund.pfixcore.webservice.jsonws.serializers;
21:
22: import java.io.IOException;
23: import java.io.Writer;
24: import java.lang.reflect.Array;
25:
26: import de.schlund.pfixcore.webservice.json.JSONArray;
27: import de.schlund.pfixcore.webservice.json.JSONValue;
28: import de.schlund.pfixcore.webservice.jsonws.SerializationContext;
29: import de.schlund.pfixcore.webservice.jsonws.SerializationException;
30: import de.schlund.pfixcore.webservice.jsonws.Serializer;
31:
32: public class ArraySerializer extends Serializer {
33:
34: @Override
35: public Object serialize(SerializationContext ctx, Object obj)
36: throws SerializationException {
37: JSONArray jsonArray = new JSONArray();
38: if (obj.getClass().isArray()) {
39: int len = Array.getLength(obj);
40: for (int i = 0; i < len; i++) {
41: Object item = Array.get(obj, i);
42: if (item == null) {
43: jsonArray.add(JSONValue.NULL);
44: } else {
45: Object serObj = ctx.serialize(item);
46: jsonArray.add(serObj);
47: }
48: }
49: }
50: return jsonArray;
51: }
52:
53: @Override
54: public void serialize(SerializationContext ctx, Object obj,
55: Writer writer) throws SerializationException, IOException {
56: writer.write("[");
57: if (obj.getClass().isArray()) {
58: int len = Array.getLength(obj);
59: for (int i = 0; i < len; i++) {
60: if (i > 0)
61: writer.write(",");
62: Object item = Array.get(obj, i);
63: if (item == null) {
64: writer.write("null");
65: } else {
66: ctx.serialize(item, writer);
67: }
68: }
69: }
70: writer.write("]");
71: }
72:
73: }
|