mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-09 20:14:28 +08:00
Merge branch 'stleary:master' into master
This commit is contained in:
@@ -5,15 +5,15 @@ Public Domain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This provides static methods to convert comma delimited text into a
|
||||
* JSONArray, and to convert a JSONArray into comma delimited text. Comma
|
||||
* This provides static methods to convert comma (or otherwise) delimited text into a
|
||||
* JSONArray, and to convert a JSONArray into comma (or otherwise) delimited text. Comma
|
||||
* delimited text is a very popular format for data interchange. It is
|
||||
* understood by most database, spreadsheet, and organizer programs.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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 it is wrapped in single quotes or double quotes.
|
||||
* <p>
|
||||
* The first row usually contains the names of the columns.
|
||||
* <p>
|
||||
@@ -29,50 +29,48 @@ public class CDL {
|
||||
* Get the next value. The value can be wrapped in quotes. The value can
|
||||
* be empty.
|
||||
* @param x A JSONTokener of the source text.
|
||||
* @param delimiter used in the file
|
||||
* @return The value string, or null if empty.
|
||||
* @throws JSONException if the quoted string is badly formed.
|
||||
*/
|
||||
private static String getValue(JSONTokener x) throws JSONException {
|
||||
private static String getValue(JSONTokener x, char delimiter) throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
StringBuilder sb;
|
||||
do {
|
||||
c = x.next();
|
||||
} while (c == ' ' || c == '\t');
|
||||
switch (c) {
|
||||
case 0:
|
||||
return null;
|
||||
case '"':
|
||||
case '\'':
|
||||
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();
|
||||
case ',':
|
||||
x.back();
|
||||
return "";
|
||||
default:
|
||||
x.back();
|
||||
return x.nextTo(',');
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -81,17 +79,25 @@ public class CDL {
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
|
||||
return rowToJSONArray(x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #rowToJSONArray(JSONTokener)}, but with a custom delimiter.
|
||||
* @see #rowToJSONArray(JSONTokener)
|
||||
*/
|
||||
public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException {
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
String value = getValue(x);
|
||||
String value = getValue(x,delimiter);
|
||||
char c = x.next();
|
||||
if (value == null ||
|
||||
(ja.length() == 0 && value.length() == 0 && c != ',')) {
|
||||
(ja.length() == 0 && value.length() == 0 && c != delimiter)) {
|
||||
return null;
|
||||
}
|
||||
ja.put(value);
|
||||
for (;;) {
|
||||
if (c == ',') {
|
||||
if (c == delimiter) {
|
||||
break;
|
||||
}
|
||||
if (c != ' ') {
|
||||
@@ -116,9 +122,17 @@ public class CDL {
|
||||
* @return A JSONObject combining the names and values.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
JSONArray ja = rowToJSONArray(x);
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws JSONException {
|
||||
return rowToJSONObject(names, x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #rowToJSONObject(JSONArray, JSONTokener)}, but with a custom {@code delimiter}.
|
||||
*
|
||||
* @see #rowToJSONObject(JSONArray, JSONTokener)
|
||||
*/
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
|
||||
JSONArray ja = rowToJSONArray(x, delimiter);
|
||||
return ja != null ? ja.toJSONObject(names) : null;
|
||||
}
|
||||
|
||||
@@ -130,15 +144,23 @@ public class CDL {
|
||||
* @return A string ending in NEWLINE.
|
||||
*/
|
||||
public static String rowToString(JSONArray ja) {
|
||||
return rowToString(ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #rowToString(JSONArray)}, but with a custom delimiter.
|
||||
* @see #rowToString(JSONArray)
|
||||
*/
|
||||
public static String rowToString(JSONArray ja, char delimiter) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
sb.append(delimiter);
|
||||
}
|
||||
Object object = ja.opt(i);
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
|
||||
if (string.length() > 0 && (string.indexOf(delimiter) >= 0 ||
|
||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
||||
sb.append('"');
|
||||
@@ -167,7 +189,15 @@ public class CDL {
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return toJSONArray(new JSONTokener(string));
|
||||
return toJSONArray(string, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toJSONArray(String)}, but with a custom delimiter.
|
||||
* @see #toJSONArray(String)
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, char delimiter) throws JSONException {
|
||||
return toJSONArray(new JSONTokener(string), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,7 +208,15 @@ public class CDL {
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
|
||||
return toJSONArray(rowToJSONArray(x), x);
|
||||
return toJSONArray(x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toJSONArray(JSONTokener)}, but with a custom delimiter.
|
||||
* @see #toJSONArray(JSONTokener)
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException {
|
||||
return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,9 +227,16 @@ public class CDL {
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, String string)
|
||||
throws JSONException {
|
||||
return toJSONArray(names, new JSONTokener(string));
|
||||
public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException {
|
||||
return toJSONArray(names, string, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toJSONArray(JSONArray, String)}, but with a custom delimiter.
|
||||
* @see #toJSONArray(JSONArray, String)
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException {
|
||||
return toJSONArray(names, new JSONTokener(string), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,14 +247,21 @@ public class CDL {
|
||||
* @return A JSONArray of JSONObjects.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException {
|
||||
return toJSONArray(names, x, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toJSONArray(JSONArray, JSONTokener)}, but with a custom delimiter.
|
||||
* @see #toJSONArray(JSONArray, JSONTokener)
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
JSONObject jo = rowToJSONObject(names, x);
|
||||
JSONObject jo = rowToJSONObject(names, x, delimiter);
|
||||
if (jo == null) {
|
||||
break;
|
||||
}
|
||||
@@ -231,11 +283,19 @@ public class CDL {
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray ja) throws JSONException {
|
||||
return toString(ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toString(JSONArray)}, but with a custom delimiter.
|
||||
* @see #toString(JSONArray)
|
||||
*/
|
||||
public static String toString(JSONArray ja, char delimiter) throws JSONException {
|
||||
JSONObject jo = ja.optJSONObject(0);
|
||||
if (jo != null) {
|
||||
JSONArray names = jo.names();
|
||||
if (names != null) {
|
||||
return rowToString(names) + toString(names, ja);
|
||||
return rowToString(names, delimiter) + toString(names, ja, delimiter);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -250,8 +310,15 @@ public class CDL {
|
||||
* @return A comma delimited text.
|
||||
* @throws JSONException if a called function fails
|
||||
*/
|
||||
public static String toString(JSONArray names, JSONArray ja)
|
||||
throws JSONException {
|
||||
public static String toString(JSONArray names, JSONArray ja) throws JSONException {
|
||||
return toString(names, ja, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #toString(JSONArray,JSONArray)}, but with a custom delimiter.
|
||||
* @see #toString(JSONArray,JSONArray)
|
||||
*/
|
||||
public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -259,7 +326,7 @@ public class CDL {
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
JSONObject jo = ja.optJSONObject(i);
|
||||
if (jo != null) {
|
||||
sb.append(rowToString(jo.toJSONArray(names)));
|
||||
sb.append(rowToString(jo.toJSONArray(names), delimiter));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
|
@@ -139,6 +139,11 @@ public class JSONObject {
|
||||
*/
|
||||
private final Map<String, Object> map;
|
||||
|
||||
/**
|
||||
* Retrieves the type of the underlying Map in this class.
|
||||
*
|
||||
* @return The class object representing the type of the underlying Map.
|
||||
*/
|
||||
public Class<? extends Map> getMapType() {
|
||||
return map.getClass();
|
||||
}
|
||||
@@ -375,7 +380,6 @@ public class JSONObject {
|
||||
* @JSONPropertyIgnore
|
||||
* public String getName() { return this.name; }
|
||||
* </pre>
|
||||
* <p>
|
||||
*
|
||||
* @param bean
|
||||
* An object that has getter methods that should be used to make
|
||||
@@ -2256,6 +2260,14 @@ public class JSONObject {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quotes a string and appends the result to a given Writer.
|
||||
*
|
||||
* @param string The input string to be quoted.
|
||||
* @param w The Writer to which the quoted string will be appended.
|
||||
* @return The same Writer instance after appending the quoted string.
|
||||
* @throws IOException If an I/O error occurs while writing to the Writer.
|
||||
*/
|
||||
public static Writer quote(String string, Writer w) throws IOException {
|
||||
if (string == null || string.isEmpty()) {
|
||||
w.write("\"\"");
|
||||
|
@@ -163,6 +163,12 @@ public class JSONPointer {
|
||||
//}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointer instance with the provided list of reference tokens.
|
||||
*
|
||||
* @param refTokens A list of strings representing the reference tokens for the JSON Pointer.
|
||||
* Each token identifies a step in the path to the targeted value.
|
||||
*/
|
||||
public JSONPointer(List<String> refTokens) {
|
||||
this.refTokens = new ArrayList<String>(refTokens);
|
||||
}
|
||||
|
@@ -14,10 +14,21 @@ Public Domain.
|
||||
public class JSONPointerException extends JSONException {
|
||||
private static final long serialVersionUID = 8872944667561856751L;
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointerException with the specified error message.
|
||||
*
|
||||
* @param message The detail message describing the reason for the exception.
|
||||
*/
|
||||
public JSONPointerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new JSONPointerException with the specified error message and cause.
|
||||
*
|
||||
* @param message The detail message describing the reason for the exception.
|
||||
* @param cause The cause of the exception.
|
||||
*/
|
||||
public JSONPointerException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
@@ -525,6 +525,11 @@ public class JSONTokener {
|
||||
this.line + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying reader, releasing any resources associated with it.
|
||||
*
|
||||
* @throws IOException If an I/O error occurs while closing the reader.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if(reader!=null){
|
||||
reader.close();
|
||||
|
@@ -29,11 +29,20 @@ public class ParserConfiguration {
|
||||
*/
|
||||
protected int maxNestingDepth;
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with default settings.
|
||||
*/
|
||||
public ParserConfiguration() {
|
||||
this.keepStrings = false;
|
||||
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with the specified settings.
|
||||
*
|
||||
* @param keepStrings A boolean indicating whether to preserve strings during parsing.
|
||||
* @param maxNestingDepth An integer representing the maximum allowed nesting depth.
|
||||
*/
|
||||
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.maxNestingDepth = maxNestingDepth;
|
||||
|
@@ -56,6 +56,9 @@ public class XML {
|
||||
*/
|
||||
public static final String NULL_ATTR = "xsi:nil";
|
||||
|
||||
/**
|
||||
* Represents the XML attribute name for specifying type information.
|
||||
*/
|
||||
public static final String TYPE_ATTR = "xsi:type";
|
||||
|
||||
/**
|
||||
|
@@ -352,9 +352,20 @@ public class XMLParserConfiguration extends ParserConfiguration {
|
||||
return clonedConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser should automatically close empty XML tags.
|
||||
*
|
||||
* @return {@code true} if empty XML tags should be automatically closed, {@code false} otherwise.
|
||||
*/
|
||||
public boolean isCloseEmptyTag() {
|
||||
return this.closeEmptyTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser should trim white spaces from XML content.
|
||||
*
|
||||
* @return {@code true} if white spaces should be trimmed, {@code false} otherwise.
|
||||
*/
|
||||
public boolean shouldTrimWhiteSpace() {
|
||||
return this.shouldTrimWhiteSpace;
|
||||
}
|
||||
|
@@ -42,5 +42,12 @@ Public Domain.
|
||||
* @param <T> return type of convert method
|
||||
*/
|
||||
public interface XMLXsiTypeConverter<T> {
|
||||
|
||||
/**
|
||||
* Converts an XML xsi:type attribute value to the specified type {@code T}.
|
||||
*
|
||||
* @param value The string representation of the XML xsi:type attribute value to be converted.
|
||||
* @return An object of type {@code T} representing the converted value.
|
||||
*/
|
||||
T convert(String value);
|
||||
}
|
||||
|
Reference in New Issue
Block a user