java.lang.Object
org.bzdev.util.JSUtilities.JSON
org.bzdev.obnaming.NJSUtilities.JSON
- Enclosing class:
- NJSUtilities
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.
- For a JSON object in a key/value pair, the quoting the key is optional when the key is syntactically a Java identifier.
NamedObjectFactory.-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.util.JSUtilities.JSON
JSUtilities.JSON.Parser -
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.protected voidWrite an object.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 beNamedObjectOps,JSObject,JSArray,String,Number, orBoolean. For an instance ofNamedObjectOps, the name of the object will be written as a JSON string.- Parameters:
w- a writerobject- the object to write- Throws:
IOException- an error occurred while writing
-
write
Description copied from class:JSUtilities.JSONWrite an object. This method dispatches the operation to methods for specific object types.- Overrides:
writein classJSUtilities.JSON- Parameters:
w- the writerobject- the object- Throws:
IOException- if an IO error occurred
-
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:
- the result of parsing
- 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.- Parameters:
is- the input stream (containing a JSON value)dec- the charset decoder used to turn the input stream into a sequence of characters- Returns:
- the result of parsing
- Throws:
IOException- an error occurred while reading from the input stream
-
parse
JSON parser that reads JSON-formatted text from anInputStreamgiven aCharset. 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)cs- the charset for the input stream- Returns:
- the result of parsing
- Throws:
IOException- an error occurred while reading from the input stream
-
parse
JSON parser that reads JSON-formated text from a character array. The result's type, if the result is not null, is eitherJSObject,JSArray,String,Number, orBoolean.- Parameters:
buffer- the input character sequence (containing a JSON value)- Returns:
- the result of parsing
- Throws:
IOException- an error occurred while reading from the input stream
-
parse
JSON parser that reads JSON-formated text from aString. The result's type, if the result is not null, is eitherJSObject,JSArray,String,Number, orBoolean.- Parameters:
s- the input string (containing a JSON value)- Returns:
- the result of parsing
- Throws:
IOException- an error occurred while reading from the input stream
-
parse
JSON parser that reads JSON-formated text from aReader. The result's type, if the result is not null, is eitherJSObject,JSArray,String,Number, orBoolean.- Parameters:
r- the reader- Returns:
- the object.
- Throws:
IOException- an error occurred while reading from the input stream
-