1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.chemistry.opencmis.commons.impl.json;
20
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * A JSON array. JSONObject supports java.util.List interface.
28 *
29 * (Taken from JSON.simple <http://code.google.com/p/json-simple/> and modified
30 * for OpenCMIS.)
31 *
32 * @author FangYidong<fangyidong@yahoo.com.cn>
33 */
34 public class JSONArray extends ArrayList<Object> implements List<Object>, JSONAware, JSONStreamAware {
35 private static final long serialVersionUID = 3957988303675231981L;
36
37 /**
38 * Encode a list into JSON text and write it to out. If this list is also a
39 * JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific
40 * behaviours will be ignored at this top level.
41 *
42 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
43 *
44 * @param list
45 * @param out
46 */
47 public static void writeJSONString(List<Object> list, Writer out) throws IOException {
48 if (list == null) {
49 out.write("null");
50 return;
51 }
52
53 boolean first = true;
54
55 out.write('[');
56 for (Object value : list) {
57 if (first) {
58 first = false;
59 } else {
60 out.write(',');
61 }
62
63 if (value == null) {
64 out.write("null");
65 continue;
66 }
67
68 JSONValue.writeJSONString(value, out);
69 }
70 out.write(']');
71 }
72
73 public void writeJSONString(Writer out) throws IOException {
74 writeJSONString(this, out);
75 }
76
77 /**
78 * Convert a list to JSON text. The result is a JSON array. If this list is
79 * also a JSONAware, JSONAware specific behaviours will be omitted at this
80 * top level.
81 *
82 * @see org.json.simple.JSONValue#toJSONString(Object)
83 *
84 * @param list
85 * @return JSON text, or "null" if list is null.
86 */
87 public static String toJSONString(List<Object> list) {
88 if (list == null) {
89 return "null";
90 }
91
92 boolean first = true;
93 StringBuilder sb = new StringBuilder();
94
95 sb.append('[');
96 for (Object value : list) {
97 if (first) {
98 first = false;
99 } else {
100 sb.append(',');
101 }
102
103 if (value == null) {
104 sb.append("null");
105 continue;
106 }
107 sb.append(JSONValue.toJSONString(value));
108 }
109 sb.append(']');
110 return sb.toString();
111 }
112
113 public String toJSONString() {
114 return toJSONString(this);
115 }
116
117 public String toString() {
118 return toJSONString();
119 }
120 }