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 class
Parser support class for JSON. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
parse
(char[] buffer) JSON parser that reads JSON-formated text from a character array.static Object
parse
(InputStream is, String charsetName) JSON parser that reads JSON-formatted text from anInputStream
given aString
providing the name of a character set.static Object
parse
(InputStream is, Charset cs) JSON parser that reads JSON-formatted text from anInputStream
given aCharset
.static Object
parse
(InputStream is, CharsetDecoder dec) JSON parser that reads JSON-formatted text from anInputStream
given aCharsetDecoder
.static Object
JSON parser that reads JSON-formated text from aReader
.static Object
JSON parser that reads JSON-formated text from aString
.static String
Encode a string using JSON escape sequences.protected void
Write an object whose type isBoolean
.protected void
Write an object whose type isNumber
.protected void
Write an object.protected void
Write an object whose type isString
.protected void
Write an object whose type isJSArray
.protected void
Write an object whose type isJSObject
.static void
Write 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
null
if the argument is null
-
parse
public static Object parse(InputStream is, String charsetName) throws IOException, UnsupportedEncodingException JSON parser that reads JSON-formatted text from anInputStream
given aString
providing 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 anInputStream
given aCharsetDecoder
. The result's type, if the result is not null, is eitherJSObject
,JSArray
,String
,Number
, orBoolean
. -
parse
-
parse
-
parse
-
parse
-