001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jsonws.serializers;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import de.schlund.pfixcore.webservice.json.JSONObject;
028: import de.schlund.pfixcore.webservice.json.JSONValue;
029: import de.schlund.pfixcore.webservice.json.ParserUtils;
030: import de.schlund.pfixcore.webservice.jsonws.SerializationContext;
031: import de.schlund.pfixcore.webservice.jsonws.SerializationException;
032: import de.schlund.pfixcore.webservice.jsonws.Serializer;
033:
034: /**
035: * @author mleidig@schlund.de
036: */
037: public class MapSerializer extends Serializer {
038:
039: public MapSerializer() {
040: }
041:
042: public Object serialize(SerializationContext ctx, Object obj)
043: throws SerializationException {
044: JSONObject jsonObj = new JSONObject();
045: if (obj instanceof Map) {
046: if (ctx.doClassHinting()) {
047: jsonObj.putMember(ctx.getClassHintPropertyName(), obj
048: .getClass().getName());
049: }
050: Map<?, ?> map = (Map<?, ?>) obj;
051: Iterator<?> it = map.keySet().iterator();
052: while (it.hasNext()) {
053: Object key = it.next();
054: if (key instanceof String) {
055: Object val = map.get(key);
056: if (val == null) {
057: jsonObj.putMember(key.toString(),
058: JSONValue.NULL);
059: } else {
060: Object serObj = ctx.serialize(val);
061: jsonObj.putMember(key.toString(), serObj);
062: }
063: } else
064: throw new SerializationException(
065: "Unsupported Map key type (must be java.lang.String): "
066: + key.getClass().getName());
067: }
068: }
069: return jsonObj;
070: }
071:
072: public void serialize(SerializationContext ctx, Object obj,
073: Writer writer) throws SerializationException, IOException {
074: writer.write("{");
075: if (obj instanceof Map) {
076: if (ctx.doClassHinting()) {
077: writer.write("\"");
078: writer.write(ctx.getClassHintPropertyName());
079: writer.write("\":\"");
080: writer.write(obj.getClass().getName());
081: writer.write("\",");
082: }
083: Map<?, ?> map = (Map<?, ?>) obj;
084: Iterator<?> it = map.keySet().iterator();
085: while (it.hasNext()) {
086: Object key = it.next();
087: if (key instanceof String) {
088: Object val = map.get(key);
089: String keyStr = ParserUtils.jsonEscape(key
090: .toString());
091: if (val == null) {
092: writer.write(keyStr);
093: writer.write(":null");
094: } else {
095: writer.write(keyStr);
096: writer.write(":");
097: ctx.serialize(val, writer);
098: }
099: if (it.hasNext())
100: writer.write(",");
101: } else
102: throw new SerializationException(
103: "Unsupported Map key type (must be java.lang.String): "
104: + key.getClass().getName());
105: }
106: }
107: writer.write("}");
108: }
109:
110: }
|