1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

whitespace

This commit is contained in:
Douglas Crockford 2012-11-13 14:43:06 -08:00
parent 96fceff148
commit 8200ac2994

558
CDL.java
View file

@ -1,279 +1,279 @@
package org.json; package org.json;
/* /*
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
The Software shall be used for Good, not Evil. The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
/** /**
* This provides static methods to convert comma delimited text into a * This provides static methods to convert comma delimited text into a
* JSONArray, and to covert a JSONArray into comma delimited text. Comma * JSONArray, and to covert a JSONArray into comma delimited text. Comma
* delimited text is a very popular format for data interchange. It is * delimited text is a very popular format for data interchange. It is
* understood by most database, spreadsheet, and organizer programs. * understood by most database, spreadsheet, and organizer programs.
* <p> * <p>
* Each row of text represents a row in a table or a data record. Each row * Each row of text represents a row in a table or a data record. Each row
* ends with a NEWLINE character. Each row contains one or more values. * ends with a NEWLINE character. Each row contains one or more values.
* Values are separated by commas. A value can contain any character except * Values are separated by commas. A value can contain any character except
* for comma, unless is is wrapped in single quotes or double quotes. * for comma, unless is is wrapped in single quotes or double quotes.
* <p> * <p>
* The first row usually contains the names of the columns. * The first row usually contains the names of the columns.
* <p> * <p>
* A comma delimited list can be converted into a JSONArray of JSONObjects. * A comma delimited list can be converted into a JSONArray of JSONObjects.
* The names for the elements in the JSONObjects can be taken from the names * The names for the elements in the JSONObjects can be taken from the names
* in the first row. * in the first row.
* @author JSON.org * @author JSON.org
* @version 2010-12-24 * @version 2012-11-13
*/ */
public class CDL { public class CDL {
/** /**
* Get the next value. The value can be wrapped in quotes. The value can * Get the next value. The value can be wrapped in quotes. The value can
* be empty. * be empty.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return The value string, or null if empty. * @return The value string, or null if empty.
* @throws JSONException if the quoted string is badly formed. * @throws JSONException if the quoted string is badly formed.
*/ */
private static String getValue(JSONTokener x) throws JSONException { private static String getValue(JSONTokener x) throws JSONException {
char c; char c;
char q; char q;
StringBuffer sb; StringBuffer sb;
do { do {
c = x.next(); c = x.next();
} while (c == ' ' || c == '\t'); } while (c == ' ' || c == '\t');
switch (c) { switch (c) {
case 0: case 0:
return null; return null;
case '"': case '"':
case '\'': case '\'':
q = c; q = c;
sb = new StringBuffer(); sb = new StringBuffer();
for (;;) { for (;;) {
c = x.next(); c = x.next();
if (c == q) { if (c == q) {
break; break;
} }
if (c == 0 || c == '\n' || c == '\r') { if (c == 0 || c == '\n' || c == '\r') {
throw x.syntaxError("Missing close quote '" + q + "'."); throw x.syntaxError("Missing close quote '" + q + "'.");
} }
sb.append(c); sb.append(c);
} }
return sb.toString(); return sb.toString();
case ',': case ',':
x.back(); x.back();
return ""; return "";
default: default:
x.back(); x.back();
return x.nextTo(','); return x.nextTo(',');
} }
} }
/** /**
* Produce a JSONArray of strings from a row of comma delimited values. * Produce a JSONArray of strings from a row of comma delimited values.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONArray of strings. * @return A JSONArray of strings.
* @throws JSONException * @throws JSONException
*/ */
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
JSONArray ja = new JSONArray(); JSONArray ja = new JSONArray();
for (;;) { for (;;) {
String value = getValue(x); String value = getValue(x);
char c = x.next(); char c = x.next();
if (value == null || if (value == null ||
(ja.length() == 0 && value.length() == 0 && c != ',')) { (ja.length() == 0 && value.length() == 0 && c != ',')) {
return null; return null;
} }
ja.put(value); ja.put(value);
for (;;) { for (;;) {
if (c == ',') { if (c == ',') {
break; break;
} }
if (c != ' ') { if (c != ' ') {
if (c == '\n' || c == '\r' || c == 0) { if (c == '\n' || c == '\r' || c == 0) {
return ja; return ja;
} }
throw x.syntaxError("Bad character '" + c + "' (" + throw x.syntaxError("Bad character '" + c + "' (" +
(int)c + ")."); (int)c + ").");
} }
c = x.next(); c = x.next();
} }
} }
} }
/** /**
* Produce a JSONObject from a row of comma delimited text, using a * Produce a JSONObject from a row of comma delimited text, using a
* parallel JSONArray of strings to provides the names of the elements. * parallel JSONArray of strings to provides the names of the elements.
* @param names A JSONArray of names. This is commonly obtained from the * @param names A JSONArray of names. This is commonly obtained from the
* first row of a comma delimited text file using the rowToJSONArray * first row of a comma delimited text file using the rowToJSONArray
* method. * method.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONObject combining the names and values. * @return A JSONObject combining the names and values.
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
throws JSONException { throws JSONException {
JSONArray ja = rowToJSONArray(x); JSONArray ja = rowToJSONArray(x);
return ja != null ? ja.toJSONObject(names) : null; return ja != null ? ja.toJSONObject(names) : null;
} }
/** /**
* Produce a comma delimited text row from a JSONArray. Values containing * Produce a comma delimited text row from a JSONArray. Values containing
* the comma character will be quoted. Troublesome characters may be * the comma character will be quoted. Troublesome characters may be
* removed. * removed.
* @param ja A JSONArray of strings. * @param ja A JSONArray of strings.
* @return A string ending in NEWLINE. * @return A string ending in NEWLINE.
*/ */
public static String rowToString(JSONArray ja) { public static String rowToString(JSONArray ja) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0; i < ja.length(); i += 1) { for (int i = 0; i < ja.length(); i += 1) {
if (i > 0) { if (i > 0) {
sb.append(','); sb.append(',');
} }
Object object = ja.opt(i); Object object = ja.opt(i);
if (object != null) { if (object != null) {
String string = object.toString(); String string = object.toString();
if (string.length() > 0 && (string.indexOf(',') >= 0 || if (string.length() > 0 && (string.indexOf(',') >= 0 ||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 || string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) { string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
sb.append('"'); sb.append('"');
int length = string.length(); int length = string.length();
for (int j = 0; j < length; j += 1) { for (int j = 0; j < length; j += 1) {
char c = string.charAt(j); char c = string.charAt(j);
if (c >= ' ' && c != '"') { if (c >= ' ' && c != '"') {
sb.append(c); sb.append(c);
} }
} }
sb.append('"'); sb.append('"');
} else { } else {
sb.append(string); sb.append(string);
} }
} }
} }
sb.append('\n'); sb.append('\n');
return sb.toString(); return sb.toString();
} }
/** /**
* Produce a JSONArray of JSONObjects from a comma delimited text string, * Produce a JSONArray of JSONObjects from a comma delimited text string,
* using the first row as a source of names. * using the first row as a source of names.
* @param string The comma delimited text. * @param string The comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException * @throws JSONException
*/ */
public static JSONArray toJSONArray(String string) throws JSONException { public static JSONArray toJSONArray(String string) throws JSONException {
return toJSONArray(new JSONTokener(string)); return toJSONArray(new JSONTokener(string));
} }
/** /**
* Produce a JSONArray of JSONObjects from a comma delimited text string, * Produce a JSONArray of JSONObjects from a comma delimited text string,
* using the first row as a source of names. * using the first row as a source of names.
* @param x The JSONTokener containing the comma delimited text. * @param x The JSONTokener containing the comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONTokener x) throws JSONException { public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
return toJSONArray(rowToJSONArray(x), x); return toJSONArray(rowToJSONArray(x), x);
} }
/** /**
* Produce a JSONArray of JSONObjects from a comma delimited text string * Produce a JSONArray of JSONObjects from a comma delimited text string
* using a supplied JSONArray as the source of element names. * using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param string The comma delimited text. * @param string The comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONArray names, String string) public static JSONArray toJSONArray(JSONArray names, String string)
throws JSONException { throws JSONException {
return toJSONArray(names, new JSONTokener(string)); return toJSONArray(names, new JSONTokener(string));
} }
/** /**
* Produce a JSONArray of JSONObjects from a comma delimited text string * Produce a JSONArray of JSONObjects from a comma delimited text string
* using a supplied JSONArray as the source of element names. * using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONArray names, JSONTokener x) public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
throws JSONException { throws JSONException {
if (names == null || names.length() == 0) { if (names == null || names.length() == 0) {
return null; return null;
} }
JSONArray ja = new JSONArray(); JSONArray ja = new JSONArray();
for (;;) { for (;;) {
JSONObject jo = rowToJSONObject(names, x); JSONObject jo = rowToJSONObject(names, x);
if (jo == null) { if (jo == null) {
break; break;
} }
ja.put(jo); ja.put(jo);
} }
if (ja.length() == 0) { if (ja.length() == 0) {
return null; return null;
} }
return ja; return ja;
} }
/** /**
* Produce a comma delimited text from a JSONArray of JSONObjects. The * Produce a comma delimited text from a JSONArray of JSONObjects. The
* first row will be a list of names obtained by inspecting the first * first row will be a list of names obtained by inspecting the first
* JSONObject. * JSONObject.
* @param ja A JSONArray of JSONObjects. * @param ja A JSONArray of JSONObjects.
* @return A comma delimited text. * @return A comma delimited text.
* @throws JSONException * @throws JSONException
*/ */
public static String toString(JSONArray ja) throws JSONException { public static String toString(JSONArray ja) throws JSONException {
JSONObject jo = ja.optJSONObject(0); JSONObject jo = ja.optJSONObject(0);
if (jo != null) { if (jo != null) {
JSONArray names = jo.names(); JSONArray names = jo.names();
if (names != null) { if (names != null) {
return rowToString(names) + toString(names, ja); return rowToString(names) + toString(names, ja);
} }
} }
return null; return null;
} }
/** /**
* Produce a comma delimited text from a JSONArray of JSONObjects using * Produce a comma delimited text from a JSONArray of JSONObjects using
* a provided list of names. The list of names is not included in the * a provided list of names. The list of names is not included in the
* output. * output.
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param ja A JSONArray of JSONObjects. * @param ja A JSONArray of JSONObjects.
* @return A comma delimited text. * @return A comma delimited text.
* @throws JSONException * @throws JSONException
*/ */
public static String toString(JSONArray names, JSONArray ja) public static String toString(JSONArray names, JSONArray ja)
throws JSONException { throws JSONException {
if (names == null || names.length() == 0) { if (names == null || names.length() == 0) {
return null; return null;
} }
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0; i < ja.length(); i += 1) { for (int i = 0; i < ja.length(); i += 1) {
JSONObject jo = ja.optJSONObject(i); JSONObject jo = ja.optJSONObject(i);
if (jo != null) { if (jo != null) {
sb.append(rowToString(jo.toJSONArray(names))); sb.append(rowToString(jo.toJSONArray(names)));
} }
} }
return sb.toString(); return sb.toString();
} }
} }