1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
26 Copyright (c) 2002 JSON.org
\r
28 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
29 of this software and associated documentation files (the "Software"), to deal
\r
30 in the Software without restriction, including without limitation the rights
\r
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
32 copies of the Software, and to permit persons to whom the Software is
\r
33 furnished to do so, subject to the following conditions:
\r
35 The above copyright notice and this permission notice shall be included in all
\r
36 copies or substantial portions of the Software.
\r
38 The Software shall be used for Good, not Evil.
\r
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
43 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
44 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
45 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
50 * This provides static methods to convert comma delimited text into a
\r
51 * JSONArray, and to covert a JSONArray into comma delimited text. Comma
\r
52 * delimited text is a very popular format for data interchange. It is
\r
53 * understood by most database, spreadsheet, and organizer programs.
\r
55 * Each row of text represents a row in a table or a data record. Each row
\r
56 * ends with a NEWLINE character. Each row contains one or more values.
\r
57 * Values are separated by commas. A value can contain any character except
\r
58 * for comma, unless is is wrapped in single quotes or double quotes.
\r
60 * The first row usually contains the names of the columns.
\r
62 * A comma delimited list can be converted into a JSONArray of JSONObjects.
\r
63 * The names for the elements in the JSONObjects can be taken from the names
\r
66 * @version 2012-11-13
\r
71 * Get the next value. The value can be wrapped in quotes. The value can
\r
73 * @param x A JSONTokener of the source text.
\r
74 * @return The value string, or null if empty.
\r
75 * @throws JSONException if the quoted string is badly formed.
\r
77 private static String getValue(JSONTokener x) throws JSONException {
\r
83 } while (c == ' ' || c == '\t');
\r
90 sb = new StringBuffer();
\r
96 if (c == 0 || c == '\n' || c == '\r') {
\r
97 throw x.syntaxError("Missing close quote '" + q + "'.");
\r
101 return sb.toString();
\r
107 return x.nextTo(',');
\r
112 * Produce a JSONArray of strings from a row of comma delimited values.
\r
113 * @param x A JSONTokener of the source text.
\r
114 * @return A JSONArray of strings.
\r
115 * @throws JSONException
\r
117 public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
\r
118 JSONArray ja = new JSONArray();
\r
120 String value = getValue(x);
\r
122 if (value == null ||
\r
123 (ja.length() == 0 && value.length() == 0 && c != ',')) {
\r
132 if (c == '\n' || c == '\r' || c == 0) {
\r
135 throw x.syntaxError("Bad character '" + c + "' (" +
\r
144 * Produce a JSONObject from a row of comma delimited text, using a
\r
145 * parallel JSONArray of strings to provides the names of the elements.
\r
146 * @param names A JSONArray of names. This is commonly obtained from the
\r
147 * first row of a comma delimited text file using the rowToJSONArray
\r
149 * @param x A JSONTokener of the source text.
\r
150 * @return A JSONObject combining the names and values.
\r
151 * @throws JSONException
\r
153 public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
\r
154 throws JSONException {
\r
155 JSONArray ja = rowToJSONArray(x);
\r
156 return ja != null ? ja.toJSONObject(names) : null;
\r
160 * Produce a comma delimited text row from a JSONArray. Values containing
\r
161 * the comma character will be quoted. Troublesome characters may be
\r
163 * @param ja A JSONArray of strings.
\r
164 * @return A string ending in NEWLINE.
\r
166 public static String rowToString(JSONArray ja) {
\r
167 StringBuffer sb = new StringBuffer();
\r
168 for (int i = 0; i < ja.length(); i += 1) {
\r
172 Object object = ja.opt(i);
\r
173 if (object != null) {
\r
174 String string = object.toString();
\r
175 if (string.length() > 0 && (string.indexOf(',') >= 0 ||
\r
176 string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
\r
177 string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
\r
179 int length = string.length();
\r
180 for (int j = 0; j < length; j += 1) {
\r
181 char c = string.charAt(j);
\r
182 if (c >= ' ' && c != '"') {
\r
193 return sb.toString();
\r
197 * Produce a JSONArray of JSONObjects from a comma delimited text string,
\r
198 * using the first row as a source of names.
\r
199 * @param string The comma delimited text.
\r
200 * @return A JSONArray of JSONObjects.
\r
201 * @throws JSONException
\r
203 public static JSONArray toJSONArray(String string) throws JSONException {
\r
204 return toJSONArray(new JSONTokener(string));
\r
208 * Produce a JSONArray of JSONObjects from a comma delimited text string,
\r
209 * using the first row as a source of names.
\r
210 * @param x The JSONTokener containing the comma delimited text.
\r
211 * @return A JSONArray of JSONObjects.
\r
212 * @throws JSONException
\r
214 public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
\r
215 return toJSONArray(rowToJSONArray(x), x);
\r
219 * Produce a JSONArray of JSONObjects from a comma delimited text string
\r
220 * using a supplied JSONArray as the source of element names.
\r
221 * @param names A JSONArray of strings.
\r
222 * @param string The comma delimited text.
\r
223 * @return A JSONArray of JSONObjects.
\r
224 * @throws JSONException
\r
226 public static JSONArray toJSONArray(JSONArray names, String string)
\r
227 throws JSONException {
\r
228 return toJSONArray(names, new JSONTokener(string));
\r
232 * Produce a JSONArray of JSONObjects from a comma delimited text string
\r
233 * using a supplied JSONArray as the source of element names.
\r
234 * @param names A JSONArray of strings.
\r
235 * @param x A JSONTokener of the source text.
\r
236 * @return A JSONArray of JSONObjects.
\r
237 * @throws JSONException
\r
239 public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
\r
240 throws JSONException {
\r
241 if (names == null || names.length() == 0) {
\r
244 JSONArray ja = new JSONArray();
\r
246 JSONObject jo = rowToJSONObject(names, x);
\r
252 if (ja.length() == 0) {
\r
260 * Produce a comma delimited text from a JSONArray of JSONObjects. The
\r
261 * first row will be a list of names obtained by inspecting the first
\r
263 * @param ja A JSONArray of JSONObjects.
\r
264 * @return A comma delimited text.
\r
265 * @throws JSONException
\r
267 public static String toString(JSONArray ja) throws JSONException {
\r
268 JSONObject jo = ja.optJSONObject(0);
\r
270 JSONArray names = jo.names();
\r
271 if (names != null) {
\r
272 return rowToString(names) + toString(names, ja);
\r
279 * Produce a comma delimited text from a JSONArray of JSONObjects using
\r
280 * a provided list of names. The list of names is not included in the
\r
282 * @param names A JSONArray of strings.
\r
283 * @param ja A JSONArray of JSONObjects.
\r
284 * @return A comma delimited text.
\r
285 * @throws JSONException
\r
287 public static String toString(JSONArray names, JSONArray ja)
\r
288 throws JSONException {
\r
289 if (names == null || names.length() == 0) {
\r
292 StringBuffer sb = new StringBuffer();
\r
293 for (int i = 0; i < ja.length(); i += 1) {
\r
294 JSONObject jo = ja.optJSONObject(i);
\r
296 sb.append(rowToString(jo.toJSONArray(names)));
\r
299 return sb.toString();
\r