java.lang.Object
org.bzdev.util.JSUtilities.JSON
- Direct Known Subclasses:
JSUtilities.YAML,NJSUtilities.JSON
- Enclosing class:
- JSUtilities
Methods for parsing and generating JSON.
All the public methods of this class are static.
An extended syntax is used for parsing (but not for writing):
- Comments consist of the sequence '//', not within a string, and contain all characters up to the end of a line. Comments will be silently dropped and are allowed only where whitespace is allowed.
- For a JSON object in a key/value pair, quoting the key is optional when the key is syntactically a Java identifier.
NamedObjectFactory<F extends NamedObjectFactory<F,NMR,NMD,OBJ>,NMR extends ObjectNamerOps<NMD>,NMD extends NamedObjectOps,OBJ extends NMD> .-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classParser support class for JSON. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Objectparse(char[] buffer) JSON parser that reads JSON-formated text from a character array.static Objectparse(InputStream is, String charsetName) JSON parser that reads JSON-formatted text from anInputStreamgiven aStringproviding the name of a character set.static Objectparse(InputStream is, Charset cs) JSON parser that reads JSON-formatted text from anInputStreamgiven aCharset.static Objectparse(InputStream is, CharsetDecoder dec) JSON parser that reads JSON-formatted text from anInputStreamgiven aCharsetDecoder.static ObjectJSON parser that reads JSON-formated text from aReader.static ObjectJSON parser that reads JSON-formated text from aString.static StringEncode a string using JSON escape sequences.protected voidWrite an object whose type isBoolean.protected voidWrite an object whose type isNumber.protected voidWrite an object.protected voidWrite an object whose type isString.protected voidWrite an object whose type isJSArray.protected voidWrite an object whose type isJSObject.static voidWrite a JSON representation of an object.
-
Constructor Details
-
JSON
protected JSON()Constructor.
-
-
Method Details
-
writeTo
Write a JSON representation of an object. The type of the object may beJSObject,JSArray,String,Number, orBoolean.- Parameters:
w- a writerobject- the object to write- Throws:
IOException- an error occurred while writing
-
write
Write an object. This method dispatches the operation to methods for specific object types.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
write
Write an object whose type isJSObject.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
write
Write an object whose type isJSArray.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
write
Write an object whose type isBoolean.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
write
Write an object whose type isNumber.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
write
Write an object whose type isString.- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
quote
Encode a string using JSON escape sequences.- Parameters:
s- the string to encode- Returns:
- the encoded string, including the delimiting
double quotes; the token
nullif the argument is null
-
parse
public static Object parse(InputStream is, String charsetName) throws IOException, UnsupportedEncodingException JSON parser that reads JSON-formatted text from anInputStreamgiven aStringproviding the name of a character set. The result's type, if the result is not null, is eitherJSObject,JSArray,String,Number, orBoolean.- Parameters:
is- the input stream (containing a JSON value)charsetName- the name of the charset used by the input stream- Returns:
- either a
JSObject,JSArray,String,Number,Boolean, or null - Throws:
IOException- an error occurred while reading from the input streamUnsupportedEncodingException- the charsetName does not match a known charset
-
parse
JSON parser that reads JSON-formatted text from anInputStreamgiven aCharsetDecoder. The result's type, if the result is not null, is eitherJSObject,JSArray,String,Number, orBoolean. -
parse
-
parse
-
parse
-
parse
-