001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.util;
016:
017: import java.io.Serializable;
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.Map.Entry;
021: import org.apache.commons.lang.StringEscapeUtils;
022: import org.araneaframework.core.Assert;
023:
024: /**
025: * A collection of name/value pairs in JSON data-interchange format.
026: *
027: * @author Alar Kvell (alar@araneaframework.org)
028: * @since 1.1
029: */
030: public class JsonObject implements Serializable {
031:
032: protected StringBuffer buf = new StringBuffer();
033:
034: public JsonObject() {
035: buf.append('{');
036: }
037:
038: public JsonObject(Map map) {
039: this ();
040: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
041: Map.Entry entry = (Entry) i.next();
042: if (entry.getValue() instanceof String)
043: setStringProperty(entry.getKey().toString(),
044: (String) entry.getValue());
045: else
046: setProperty(entry.getKey().toString(), String
047: .valueOf(entry.getValue()));
048: }
049: }
050:
051: /**
052: * Set a name/value pair on this object. Duplicate names are not checked.
053: *
054: * @param name
055: * name of the property
056: * @param value
057: * value of the property. Can be a string in double quotes, or a
058: * number, or true or false or null, or an object or an array.
059: */
060: public void setProperty(String name, String value) {
061: Assert.notNullParam(name, "name");
062: if (buf.length() > 1)
063: buf.append(',');
064: buf.append('"');
065: buf.append(StringEscapeUtils.escapeJavaScript(name));
066: buf.append('"');
067: buf.append(':');
068: buf.append(value);
069: }
070:
071: /**
072: * Set a name/value pair on this object. Duplicate names are not checked.
073: *
074: * @param name
075: * name of the property
076: * @param value
077: * value of the property. It is automatically double-quoted to
078: * represent a string.
079: */
080: public void setStringProperty(String name, String value) {
081: Assert.notNullParam(name, "name");
082: if (buf.length() > 1)
083: buf.append(',');
084: buf.append('"');
085: buf.append(StringEscapeUtils.escapeJavaScript(name));
086: buf.append('"');
087: buf.append(':');
088: if (value != null)
089: buf.append('"');
090: buf.append(StringEscapeUtils.escapeJavaScript(String
091: .valueOf(value)));
092: if (value != null)
093: buf.append('"');
094: }
095:
096: /**
097: * Get this object in JSON data-interchange format.
098: */
099: public String toString() {
100: buf.append('}');
101: String string = buf.toString();
102: buf.deleteCharAt(buf.length() - 1);
103: return string;
104: }
105: }
|