Skip to content

Commit

Permalink
Merge pull request #858 from stleary/cleanup-after-commit
Browse files Browse the repository at this point in the history
cleanup-after-commit for #854 and #856
  • Loading branch information
stleary authored Feb 17, 2024
2 parents 6358b7f + f164b8c commit 77c899d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 53 deletions.
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<source>8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
142 changes: 92 additions & 50 deletions src/main/java/org/json/CDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@ private static String getValue(JSONTokener x, char delimiter) throws JSONExcepti
do {
c = x.next();
} while (c == ' ' || c == '\t');
if (c == 0) {
return null;
} else if (c == '"' || c == '\'') {
q = c;
sb = new StringBuilder();
for (;;) {
c = x.next();
if (c == q) {
//Handle escaped double-quote
char nextC = x.next();
if (nextC != '\"') {
// if our quote was the end of the file, don't step
if (nextC > 0) {
x.back();
}
break;
}
}
if (c == 0 || c == '\n' || c == '\r') {
throw x.syntaxError("Missing close quote '" + q + "'.");
}
sb.append(c);
}
return sb.toString();
} else if (c == delimiter) {
x.back();
return "";
}
x.back();
return x.nextTo(delimiter);
}
if (c == 0) {
return null;
} else if (c == '"' || c == '\'') {
q = c;
sb = new StringBuilder();
for (;;) {
c = x.next();
if (c == q) {
//Handle escaped double-quote
char nextC = x.next();
if (nextC != '\"') {
// if our quote was the end of the file, don't step
if (nextC > 0) {
x.back();
}
break;
}
}
if (c == 0 || c == '\n' || c == '\r') {
throw x.syntaxError("Missing close quote '" + q + "'.");
}
sb.append(c);
}
return sb.toString();
} else if (c == delimiter) {
x.back();
return "";
}
x.back();
return x.nextTo(delimiter);
}

/**
* Produce a JSONArray of strings from a row of comma delimited values.
Expand All @@ -83,8 +83,11 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
}

/**
* Same as {@link #rowToJSONArray(JSONTokener)}, but with a custom delimiter.
* @see #rowToJSONArray(JSONTokener)
* Produce a JSONArray of strings from a row of comma delimited values.
* @param x A JSONTokener of the source text.
* @param delimiter custom delimiter char
* @return A JSONArray of strings.
* @throws JSONException if a called function fails
*/
public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException {
JSONArray ja = new JSONArray();
Expand Down Expand Up @@ -127,9 +130,15 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws
}

/**
* Same as {@link #rowToJSONObject(JSONArray, JSONTokener)}, but with a custom {@code delimiter}.
*
* @see #rowToJSONObject(JSONArray, JSONTokener)
* Produce a JSONObject from a row of comma delimited text, using a
* parallel JSONArray of strings to provides the names of the elements.
* @param names A JSONArray of names. This is commonly obtained from the
* first row of a comma delimited text file using the rowToJSONArray
* method.
* @param x A JSONTokener of the source text.
* @param delimiter custom delimiter char
* @return A JSONObject combining the names and values.
* @throws JSONException if a called function fails
*/
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
JSONArray ja = rowToJSONArray(x, delimiter);
Expand All @@ -148,8 +157,12 @@ public static String rowToString(JSONArray ja) {
}

/**
* Same as {@link #rowToString(JSONArray)}, but with a custom delimiter.
* @see #rowToString(JSONArray)
* Produce a comma delimited text row from a JSONArray. Values containing
* the comma character will be quoted. Troublesome characters may be
* removed.
* @param ja A JSONArray of strings.
* @param delimiter custom delimiter char
* @return A string ending in NEWLINE.
*/
public static String rowToString(JSONArray ja, char delimiter) {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -193,8 +206,12 @@ public static JSONArray toJSONArray(String string) throws JSONException {
}

/**
* Same as {@link #toJSONArray(String)}, but with a custom delimiter.
* @see #toJSONArray(String)
* Produce a JSONArray of JSONObjects from a comma delimited text string,
* using the first row as a source of names.
* @param string The comma delimited text.
* @param delimiter custom delimiter char
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(String string, char delimiter) throws JSONException {
return toJSONArray(new JSONTokener(string), delimiter);
Expand All @@ -212,8 +229,12 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
}

/**
* Same as {@link #toJSONArray(JSONTokener)}, but with a custom delimiter.
* @see #toJSONArray(JSONTokener)
* Produce a JSONArray of JSONObjects from a comma delimited text string,
* using the first row as a source of names.
* @param x The JSONTokener containing the comma delimited text.
* @param delimiter custom delimiter char
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException {
return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter);
Expand All @@ -232,8 +253,13 @@ public static JSONArray toJSONArray(JSONArray names, String string) throws JSONE
}

/**
* Same as {@link #toJSONArray(JSONArray, String)}, but with a custom delimiter.
* @see #toJSONArray(JSONArray, String)
* Produce a JSONArray of JSONObjects from a comma delimited text string
* using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings.
* @param string The comma delimited text.
* @param delimiter custom delimiter char
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException {
return toJSONArray(names, new JSONTokener(string), delimiter);
Expand All @@ -252,8 +278,13 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONE
}

/**
* Same as {@link #toJSONArray(JSONArray, JSONTokener)}, but with a custom delimiter.
* @see #toJSONArray(JSONArray, JSONTokener)
* Produce a JSONArray of JSONObjects from a comma delimited text string
* using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings.
* @param x A JSONTokener of the source text.
* @param delimiter custom delimiter char
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
if (names == null || names.length() == 0) {
Expand Down Expand Up @@ -287,8 +318,13 @@ public static String toString(JSONArray ja) throws JSONException {
}

/**
* Same as {@link #toString(JSONArray)}, but with a custom delimiter.
* @see #toString(JSONArray)
* Produce a comma delimited text from a JSONArray of JSONObjects. The
* first row will be a list of names obtained by inspecting the first
* JSONObject.
* @param ja A JSONArray of JSONObjects.
* @param delimiter custom delimiter char
* @return A comma delimited text.
* @throws JSONException if a called function fails
*/
public static String toString(JSONArray ja, char delimiter) throws JSONException {
JSONObject jo = ja.optJSONObject(0);
Expand All @@ -315,8 +351,14 @@ public static String toString(JSONArray names, JSONArray ja) throws JSONExceptio
}

/**
* Same as {@link #toString(JSONArray,JSONArray)}, but with a custom delimiter.
* @see #toString(JSONArray,JSONArray)
* 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
* output.
* @param names A JSONArray of strings.
* @param ja A JSONArray of JSONObjects.
* @param delimiter custom delimiter char
* @return A comma delimited text.
* @throws JSONException if a called function fails
*/
public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException {
if (names == null || names.length() == 0) {
Expand Down

0 comments on commit 77c899d

Please sign in to comment.