01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.http.util;
16:
17: import java.io.Serializable;
18: import org.apache.commons.lang.StringEscapeUtils;
19: import org.araneaframework.core.Assert;
20:
21: /**
22: * An ordered list of values in JSON data-interchange format.
23: *
24: * @author Alar Kvell (alar@araneaframework.org)
25: * @since 1.1
26: */
27: public class JsonArray implements Serializable {
28:
29: protected StringBuffer buf = new StringBuffer();
30:
31: public JsonArray() {
32: buf.append('[');
33: }
34:
35: /**
36: * Append a value to this array.
37: *
38: * @param element
39: * value to append to this array. Can be a string in double quotes,
40: * or a number, or true or false or null, or an object or an array.
41: */
42: public void append(String element) {
43: Assert.notNullParam(element, "element");
44: if (buf.length() > 1)
45: buf.append(',');
46: buf.append(element);
47: }
48:
49: /**
50: * Append a value to this array.
51: *
52: * @param element
53: * value to append to this array. It is automatically double-quoted
54: * to represent a string.
55: */
56: public void appendString(String element) {
57: Assert.notNullParam(element, "element");
58: if (buf.length() > 1)
59: buf.append(',');
60: buf.append('"');
61: buf.append(StringEscapeUtils.escapeJavaScript(element));
62: buf.append('"');
63: }
64:
65: /**
66: * Get this array in JSON data-interchange format.
67: */
68: public String toString() {
69: buf.append(']');
70: String string = buf.toString();
71: buf.deleteCharAt(buf.length() - 1);
72: return string;
73: }
74:
75: }
|