001: /*
002: * Copyright 2005 Joe Walker
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: */
016: package org.directwebremoting.json;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.directwebremoting.util.JavascriptUtil;
024:
025: /**
026: * In official JSON parlance this should be called Object, however this would
027: * cause confusion with {@link java.lang.Object} which is auto-imported.
028: * @author Joe Walker [joe at getahead dot ltd dot uk]
029: */
030: public class JsonObject extends JsonValue implements
031: Map<String, JsonValue> {
032: /* (non-Javadoc)
033: * @see org.directwebremoting.json.JsonValue#toExternalRepresentation()
034: */
035: @Override
036: public String toExternalRepresentation() {
037: StringBuffer output = new StringBuffer();
038: output.append("{ ");
039:
040: boolean isFirst = true;
041: for (Map.Entry<String, JsonValue> entry : proxy.entrySet()) {
042: if (isFirst) {
043: isFirst = false;
044: } else {
045: output.append(", ");
046: }
047:
048: output.append('\'');
049: output.append(JavascriptUtil.escapeJavaScript(entry
050: .getKey()));
051: output.append("':");
052: output.append(entry.getValue().toExternalRepresentation());
053: }
054: output.append(" }");
055: return output.toString();
056: }
057:
058: /* (non-Javadoc)
059: * @see java.util.Map#clear()
060: */
061: public void clear() {
062: proxy.clear();
063: }
064:
065: /* (non-Javadoc)
066: * @see java.util.Map#containsKey(java.lang.Object)
067: */
068: public boolean containsKey(Object key) {
069: return proxy.containsKey(key);
070: }
071:
072: /* (non-Javadoc)
073: * @see java.util.Map#containsValue(java.lang.Object)
074: */
075: public boolean containsValue(Object value) {
076: return proxy.containsValue(value);
077: }
078:
079: /* (non-Javadoc)
080: * @see java.util.Map#entrySet()
081: */
082: public Set<Entry<String, JsonValue>> entrySet() {
083: return proxy.entrySet();
084: }
085:
086: /* (non-Javadoc)
087: * @see java.util.Map#get(java.lang.Object)
088: */
089: public JsonValue get(Object key) {
090: return proxy.get(key);
091: }
092:
093: /* (non-Javadoc)
094: * @see java.util.Map#isEmpty()
095: */
096: public boolean isEmpty() {
097: return proxy.isEmpty();
098: }
099:
100: /* (non-Javadoc)
101: * @see java.util.Map#keySet()
102: */
103: public Set<String> keySet() {
104: return proxy.keySet();
105: }
106:
107: /* (non-Javadoc)
108: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
109: */
110: public JsonValue put(String key, JsonValue value) {
111: return proxy.put(key, value);
112: }
113:
114: /* (non-Javadoc)
115: * @see java.util.Map#putAll(java.util.Map)
116: */
117: public void putAll(
118: Map<? extends String, ? extends JsonValue> otherMap) {
119: proxy.putAll(otherMap);
120: }
121:
122: /* (non-Javadoc)
123: * @see java.util.Map#remove(java.lang.Object)
124: */
125: public JsonValue remove(Object key) {
126: return proxy.remove(key);
127: }
128:
129: /* (non-Javadoc)
130: * @see java.util.Map#size()
131: */
132: public int size() {
133: return proxy.size();
134: }
135:
136: /* (non-Javadoc)
137: * @see java.util.Map#values()
138: */
139: public Collection<JsonValue> values() {
140: return proxy.values();
141: }
142:
143: /* (non-Javadoc)
144: * @see java.lang.Object#toString()
145: */
146: @Override
147: public String toString() {
148: return toExternalRepresentation();
149: }
150:
151: /* (non-Javadoc)
152: * @see java.lang.Object#equals(java.lang.Object)
153: */
154: @Override
155: public boolean equals(Object o) {
156: return proxy.equals(o);
157: }
158:
159: /* (non-Javadoc)
160: * @see java.lang.Object#hashCode()
161: */
162: @Override
163: public int hashCode() {
164: return proxy.hashCode();
165: }
166:
167: /**
168: * Where we store the values
169: */
170: private Map<String, JsonValue> proxy = new HashMap<String, JsonValue>();
171: }
|