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.lang.reflect.Field;
025: import java.lang.reflect.Method;
026: import java.util.Iterator;
027: import java.util.Set;
028:
029: import de.schlund.pfixcore.beans.BeanDescriptor;
030: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
031: import de.schlund.pfixcore.webservice.json.JSONObject;
032: import de.schlund.pfixcore.webservice.json.JSONValue;
033: import de.schlund.pfixcore.webservice.jsonws.SerializationContext;
034: import de.schlund.pfixcore.webservice.jsonws.SerializationException;
035: import de.schlund.pfixcore.webservice.jsonws.Serializer;
036:
037: /**
038: * @author mleidig@schlund.de
039: */
040: public class BeanSerializer extends Serializer {
041:
042: BeanDescriptorFactory beanDescFactory;
043:
044: public BeanSerializer(BeanDescriptorFactory beanDescFactory) {
045: this .beanDescFactory = beanDescFactory;
046: }
047:
048: public Object serialize(SerializationContext ctx, Object obj)
049: throws SerializationException {
050: JSONObject jsonObj = new JSONObject();
051: if (ctx.doClassHinting()) {
052: jsonObj.putMember(ctx.getClassHintPropertyName(), obj
053: .getClass().getName());
054: }
055: BeanDescriptor bd = beanDescFactory.getBeanDescriptor(obj
056: .getClass());
057: Set<String> props = bd.getReadableProperties();
058: Iterator<String> it = props.iterator();
059: while (it.hasNext()) {
060: String prop = it.next();
061: try {
062: Object val = null;
063: Method meth = bd.getGetMethod(prop);
064: if (meth != null) {
065: val = meth.invoke(obj, new Object[0]);
066: } else {
067: Field field = bd.getDirectAccessField(prop);
068: if (field != null)
069: val = field.get(obj);
070: else
071: throw new SerializationException(
072: "Bean of type '"
073: + obj.getClass().getName()
074: + "' doesn't "
075: + " have getter method or direct access to property '"
076: + prop + "'.");
077: }
078: if (val == null) {
079: jsonObj.putMember(prop, JSONValue.NULL);
080: } else {
081: Object serObj = ctx.serialize(val);
082: jsonObj.putMember(prop, serObj);
083: }
084: } catch (Exception x) {
085: throw new SerializationException(
086: "Error during serialization.", x);
087: }
088: }
089: return jsonObj;
090: }
091:
092: public void serialize(SerializationContext ctx, Object obj,
093: Writer writer) throws SerializationException, IOException {
094: writer.write("{");
095: if (ctx.doClassHinting()) {
096: writer.write("\"");
097: writer.write(ctx.getClassHintPropertyName());
098: writer.write("\":\"");
099: writer.write(obj.getClass().getName());
100: writer.write("\",");
101: }
102: BeanDescriptor bd = beanDescFactory.getBeanDescriptor(obj
103: .getClass());
104: Set<String> props = bd.getReadableProperties();
105: Iterator<String> it = props.iterator();
106: while (it.hasNext()) {
107: String prop = it.next();
108: try {
109: Object val = null;
110: Method meth = bd.getGetMethod(prop);
111: if (meth != null) {
112: val = meth.invoke(obj, new Object[0]);
113: } else {
114: Field field = bd.getDirectAccessField(prop);
115: if (field != null)
116: val = field.get(obj);
117: else
118: throw new SerializationException(
119: "Bean of type '"
120: + obj.getClass().getName()
121: + "' doesn't "
122: + " have getter method or direct access to property '"
123: + prop + "'.");
124: }
125: if (val == null) {
126: writer.write("\"");
127: writer.write(prop);
128: writer.write("\":null");
129: } else {
130: writer.write("\"");
131: writer.write(prop);
132: writer.write("\":");
133: ctx.serialize(val, writer);
134: }
135: if (it.hasNext())
136: writer.write(",");
137: } catch (Exception x) {
138: throw new SerializationException(
139: "Error during serialization.", x);
140: }
141: }
142: writer.write("}");
143: }
144:
145: }
|