This project has retired. For details please refer to its
Attic page.
JSONArray xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.commons.impl.json;
2021import java.io.IOException;
22import java.io.Writer;
23import java.util.ArrayList;
24import java.util.List;
2526/**27 * A JSON array. JSONObject supports java.util.List interface.28 * 29 * (Taken from JSON.simple <http://code.google.com/p/json-simple/> and modified30 * for OpenCMIS.)31 * 32 * @author FangYidong<fangyidong@yahoo.com.cn>33 */34publicclassJSONArrayextends ArrayList<Object> implements List<Object>, JSONAware, JSONStreamAware {
35privatestaticfinallong serialVersionUID = 3957988303675231981L;
3637/**38 * Encode a list into JSON text and write it to out. If this list is also a39 * JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific40 * behaviours will be ignored at this top level.41 * 42 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)43 * 44 * @param list45 * @param out46 */47publicstaticvoid writeJSONString(List<Object> list, Writer out) throws IOException {
48if (list == null) {
49 out.write("null");
50return;
51 }
5253boolean first = true;
5455 out.write('[');
56for (Object value : list) {
57if (first) {
58 first = false;
59 } else {
60 out.write(',');
61 }
6263if (value == null) {
64 out.write("null");
65continue;
66 }
6768 JSONValue.writeJSONString(value, out);
69 }
70 out.write(']');
71 }
7273publicvoid writeJSONString(Writer out) throws IOException {
74 writeJSONString(this, out);
75 }
7677/**78 * Convert a list to JSON text. The result is a JSON array. If this list is79 * also a JSONAware, JSONAware specific behaviours will be omitted at this80 * top level.81 * 82 * @see org.json.simple.JSONValue#toJSONString(Object)83 * 84 * @param list85 * @return JSON text, or "null" if list is null.86 */87publicstatic String toJSONString(List<Object> list) {
88if (list == null) {
89return"null";
90 }
9192boolean first = true;
93 StringBuilder sb = new StringBuilder();
9495 sb.append('[');
96for (Object value : list) {
97if (first) {
98 first = false;
99 } else {
100 sb.append(',');
101 }
102103if (value == null) {
104 sb.append("null");
105continue;
106 }
107 sb.append(JSONValue.toJSONString(value));
108 }
109 sb.append(']');
110return sb.toString();
111 }
112113public String toJSONString() {
114return toJSONString(this);
115 }
116117public String toString() {
118return toJSONString();
119 }
120 }