public class JSON extends Object implements Versioned
Note that instances are fully immutable, and thereby thread-safe.
Note on source types: source to read is declared as Object
but covers following types:
InputStream
Reader
byte[]
char[]
String
/CharSequence
URL
File
Modifier and Type | Class and Description |
---|---|
static class |
JSON.Builder
Builder class that needs to be used for certain kind of "static" configuration
(settings that can not vary on per-call basis for
JSON ), such as
Extension registration. |
static class |
JSON.Feature
Simple on/off (enabled/disabled) features for
JSON ; used for simple
configuration aspects. |
Modifier and Type | Field and Description |
---|---|
protected int |
_features |
protected JsonFactory |
_jsonFactory
Underlying JSON factory used for creating Streaming parsers and
generators.
|
protected PrettyPrinter |
_prettyPrinter |
protected JSONReader |
_reader
Blueprint instance of the reader to use for reading JSON as simple
Objects.
|
protected TreeCodec |
_treeCodec
|
protected ValueReaderLocator |
_valueReaderLocator |
protected ValueWriterLocator |
_valueWriterLocator |
protected JSONWriter |
_writer
Blueprint instance of the writer to use for writing JSON given
simple Objects.
|
static int |
CACHE_FLAGS |
static JSON |
std
Singleton instance with standard, default configuration.
|
Modifier | Constructor and Description |
---|---|
|
JSON() |
|
JSON(JSON.Builder b)
Constructor used when creating instance using
JSON.Builder . |
|
JSON(JsonFactory jsonF) |
protected |
JSON(JSON base,
int features,
JsonFactory jsonF,
TreeCodec trees,
JSONReader r,
JSONWriter w,
PrettyPrinter pp) |
protected |
JSON(JSON base,
ValueReaderLocator rloc,
ValueWriterLocator wloc) |
Modifier and Type | Method and Description |
---|---|
protected void |
_close(Closeable cl) |
protected <T> T |
_closeWithError(Closeable cl,
Exception e) |
protected JsonGenerator |
_config(JsonGenerator g) |
protected JsonParser |
_config(JsonParser p) |
protected JSONReader |
_defaultReader() |
protected JSONWriter |
_defaultWriter() |
protected JsonParser |
_initForReading(JsonParser p) |
protected void |
_noTreeCodec(String msg) |
protected JsonParser |
_parser(Object source) |
protected JSONReader |
_readerForOperation(JsonParser p) |
protected <T> T |
_throw(Exception e) |
protected JSON |
_with(int features)
Internal mutant factory method used for constructing
|
protected JSON |
_with(int features,
JsonFactory jsonF,
TreeCodec trees,
JSONReader reader,
JSONWriter writer,
PrettyPrinter pp) |
protected void |
_writeAndClose(Object value,
JsonGenerator g) |
protected JSONWriter |
_writerForOperation(JsonGenerator gen) |
Object |
anyFrom(Object source)
|
ValueIterator<Object> |
anySequenceFrom(Object source)
Method for creating
ValueIterator for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound as "Simple" type: Map ,
List , String , Number or Boolean . |
Object[] |
arrayFrom(Object source) |
<T> T[] |
arrayOfFrom(Class<T> type,
Object source) |
byte[] |
asBytes(Object value) |
ObjectCodec |
asCodec()
Convenience method for constructing an adapter that uses this
instance as a
ObjectCodec |
String |
asString(Object value) |
<T> T |
beanFrom(Class<T> type,
Object source) |
<T> ValueIterator<T> |
beanSequenceFrom(Class<T> type,
Object source)
Method for creating
ValueIterator for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound to specific Bean (POJO) type. |
static JSON.Builder |
builder() |
static JSON.Builder |
builder(JsonFactory streamFactory) |
JSONComposer<byte[]> |
composeBytes() |
<C extends Collection<Object>> |
composeCollection(C collection) |
CollectionComposer<?,List<Object>> |
composeList() |
MapComposer<?> |
composeMap() |
MapComposer<?> |
composeMap(Map<String,Object> map) |
JSONComposer<String> |
composeString() |
JSONComposer<OutputStream> |
composeTo(File f) |
JSONComposer<OutputStream> |
composeTo(OutputStream out) |
JSONComposer<OutputStream> |
composeTo(Writer w) |
JSONComposer<OutputStream> |
composeUsing(JsonGenerator gen) |
<T extends TreeNode> |
createArrayNode()
Convenience method, equivalent to:
|
<T extends TreeNode> |
createObjectNode()
Convenience method, equivalent to:
|
JsonParser |
createParser(Object source)
Factory method for opening a
JsonParser to read content from one of
following supported sources
InputStream
Reader
byte[]
char[]
String /CharSequence
URL
File
|
JsonFactory |
getStreamingFactory() |
TreeCodec |
getTreeCodec() |
boolean |
isEnabled(JSON.Feature f) |
List<Object> |
listFrom(Object source) |
<T> List<T> |
listOfFrom(Class<T> type,
Object source) |
Map<String,Object> |
mapFrom(Object source) |
<T> Map<String,T> |
mapOfFrom(Class<T> type,
Object source)
Read method for reading a
Map of type (usually POJO) values. |
<T extends TreeNode> |
treeFrom(Object source)
Method for reading content as a JSON Tree (of type that configured
TreeCodec , see with(TreeCodec) ) supports. |
<T extends TreeNode> |
treeSequenceFrom(Object source)
Method for creating
ValueIterator for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound as JSON Trees(of type that configured
TreeCodec , see with(TreeCodec) ) supports. |
Version |
version() |
JSON |
with(CollectionBuilder b)
Mutant factory for constructing an instance with specified
CollectionBuilder ,
and returning new instance (or, if there would be no change, this instance). |
JSON |
with(JSON.Feature... features)
Mutant factory for constructing an instance with specified features
enabled.
|
JSON |
with(JSON.Feature feature,
boolean state)
Mutant factory for constructing an instance with specified feature
enabled or disabled (depending on
state ), and returning
an instance with that setting; this may either be this instance (if feature
already had specified state), or a newly constructed instance. |
JSON |
with(JsonFactory f)
Deprecated.
Since 2.11 should not try changing underlying stream factory but create
a new instance if necessary: method will be removed from 3.0 at latest
|
JSON |
with(JSONReader r)
Deprecated.
Since 2.11 should try using builder (see
builder() and create
properly configured instance |
JSON |
with(JSONWriter w)
Deprecated.
Since 2.11 should try using builder (see
builder() and create
properly configured instance |
JSON |
with(MapBuilder b)
Mutant factory for constructing an instance with specified
MapBuilder ,
and returning new instance (or, if there would be no change, this instance). |
JSON |
with(PrettyPrinter pp)
Mutant factory for constructing an instance with specified
PrettyPrinter ,
and returning new instance (or, if there would be no change, this instance). |
JSON |
with(ReaderWriterProvider rwp)
Deprecated.
Since 2.11 should register using
JacksonJrExtension |
JSON |
with(TreeCodec c)
Deprecated.
Since 2.11 should try using builder (see
builder() and create
properly configured instance |
JSON |
without(JSON.Feature... features)
Mutant factory for constructing an instance with specified features
disabled.
|
void |
write(Object value,
File f) |
void |
write(Object value,
JsonGenerator gen) |
void |
write(Object value,
OutputStream out) |
void |
write(Object value,
Writer w) |
public static final int CACHE_FLAGS
public static final JSON std
String json = JSON.std.asString(map);
protected final JsonFactory _jsonFactory
protected final TreeCodec _treeCodec
protected final ValueReaderLocator _valueReaderLocator
protected final ValueWriterLocator _valueWriterLocator
protected final JSONReader _reader
protected final JSONWriter _writer
protected final int _features
protected final PrettyPrinter _prettyPrinter
public JSON()
public JSON(JsonFactory jsonF)
public JSON(JSON.Builder b)
JSON.Builder
.b
- Builder that has configured settings to use.protected JSON(JSON base, int features, JsonFactory jsonF, TreeCodec trees, JSONReader r, JSONWriter w, PrettyPrinter pp)
protected JSON(JSON base, ValueReaderLocator rloc, ValueWriterLocator wloc)
public static JSON.Builder builder()
public static JSON.Builder builder(JsonFactory streamFactory)
protected JSONReader _defaultReader()
protected JSONWriter _defaultWriter()
public ObjectCodec asCodec()
ObjectCodec
ObjectCodec
APIpublic JSON with(PrettyPrinter pp)
PrettyPrinter
,
and returning new instance (or, if there would be no change, this instance).pp
- PrettyPrinter
to use for pretty-printing output (of null
to disable
pretty-printing)PrettyPrinter
(if not same as currently configured);
this
otherwise.public JSON with(JSON.Feature feature, boolean state)
state
), and returning
an instance with that setting; this may either be this instance (if feature
already had specified state), or a newly constructed instance.public JSON with(JSON.Feature... features)
public JSON without(JSON.Feature... features)
protected final JSON _with(int features)
@Deprecated public JSON with(JsonFactory f)
JsonFactory
if different from currently configured one (if not, return this
as-is)f
- Jackson core format factory to use for low-level decoding/encodingthis
otherwise.@Deprecated public JSON with(TreeCodec c)
builder()
and create
properly configured instanceTreeCodec
,
and returning new instance (or, if there would be no change, this instance).c
- Tree codec to use for reading/writing of tree representationthis
otherwise.@Deprecated public JSON with(JSONReader r)
builder()
and create
properly configured instanceJSONReader
,
and returning new instance (or, if there would be no change, this instance).r
- Customized JSONReader
to use instead of standard oneJSONReader
(if not same as currently configured);
this
otherwise.@Deprecated public JSON with(JSONWriter w)
builder()
and create
properly configured instanceJSONWriter
,
and returning new instance (or, if there would be no change, this instance).w
- Customized JSONWriter
to use instead of standard oneJSONWriter
(if not same as currently configured);
this
otherwise.public JSON with(MapBuilder b)
MapBuilder
,
and returning new instance (or, if there would be no change, this instance).public JSON with(CollectionBuilder b)
CollectionBuilder
,
and returning new instance (or, if there would be no change, this instance).@Deprecated public JSON with(ReaderWriterProvider rwp)
JacksonJrExtension
ReaderWriterProvider
,
and returning new instance (or, if there would be no change, this instance).protected JSON _with(int features, JsonFactory jsonF, TreeCodec trees, JSONReader reader, JSONWriter writer, PrettyPrinter pp)
public TreeCodec getTreeCodec()
public JsonFactory getStreamingFactory()
public final boolean isEnabled(JSON.Feature f)
public JsonParser createParser(Object source) throws IOException, JSONObjectException
JsonParser
to read content from one of
following supported sources
InputStream
Reader
byte[]
char[]
String
/CharSequence
URL
File
Rules regarding closing of the underlying source follow rules
that JsonFactory
has for its createParser
method.
IOException
JSONObjectException
public String asString(Object value) throws IOException, JSONObjectException
IOException
JSONObjectException
public byte[] asBytes(Object value) throws IOException, JSONObjectException
IOException
JSONObjectException
public void write(Object value, JsonGenerator gen) throws IOException, JSONObjectException
IOException
JSONObjectException
public void write(Object value, OutputStream out) throws IOException, JSONObjectException
IOException
JSONObjectException
public void write(Object value, Writer w) throws IOException, JSONObjectException
IOException
JSONObjectException
public void write(Object value, File f) throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<OutputStream> composeUsing(JsonGenerator gen) throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<OutputStream> composeTo(OutputStream out) throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<OutputStream> composeTo(Writer w) throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<OutputStream> composeTo(File f) throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<String> composeString() throws IOException, JSONObjectException
IOException
JSONObjectException
public JSONComposer<byte[]> composeBytes() throws IOException, JSONObjectException
IOException
JSONObjectException
public CollectionComposer<?,List<Object>> composeList()
public <C extends Collection<Object>> CollectionComposer<?,C> composeCollection(C collection)
public MapComposer<?> composeMap()
public MapComposer<?> composeMap(Map<String,Object> map)
public List<Object> listFrom(Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public <T> List<T> listOfFrom(Class<T> type, Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public Object[] arrayFrom(Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public <T> T[] arrayOfFrom(Class<T> type, Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public Map<String,Object> mapFrom(Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public <T> Map<String,T> mapOfFrom(Class<T> type, Object source) throws IOException, JSONObjectException
Map
of type
(usually POJO) values.IOException
JSONObjectException
public <T> T beanFrom(Class<T> type, Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
public Object anyFrom(Object source) throws IOException
Map
for JSON Objects, List
for JSON Arrays, String
for JSON Strings, null for JSON null, Boolean
for JSON booleans
and Number
for JSON numbers.
Supported source types include:
InputStream
Reader
File
URL
String
byte[]
char[]
IOException
public <T extends TreeNode> T treeFrom(Object source) throws IOException, JSONObjectException
TreeCodec
, see with(TreeCodec)
) supports.IOException
JSONObjectException
public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source) throws IOException, JSONObjectException
ValueIterator
for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound to specific Bean (POJO) type.IOException
JSONObjectException
public ValueIterator<Object> anySequenceFrom(Object source) throws IOException, JSONObjectException
ValueIterator
for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound as "Simple" type: Map
,
List
, String
, Number
or Boolean
.IOException
JSONObjectException
public <T extends TreeNode> ValueIterator<T> treeSequenceFrom(Object source) throws IOException, JSONObjectException
ValueIterator
for reading
streaming JSON
content (specifically line-delimited and concatenated variants);
individual values are bound as JSON Trees(of type that configured
TreeCodec
, see with(TreeCodec)
) supports.IOException
JSONObjectException
public <T extends TreeNode> T createArrayNode()
getTreeCodec().createArrayNode();Note that for call to succeed a
TreeCodec
must have been
configured with this instance using with(TreeCodec)
method.public <T extends TreeNode> T createObjectNode()
getTreeCodec().createObjectNode();Note that for call to succeed a
TreeCodec
must have been
configured with this instance using with(TreeCodec)
method.protected final void _writeAndClose(Object value, JsonGenerator g) throws IOException
IOException
protected JSONWriter _writerForOperation(JsonGenerator gen)
protected JSONReader _readerForOperation(JsonParser p)
protected JsonParser _parser(Object source) throws IOException, JSONObjectException
IOException
JSONObjectException
protected JsonParser _initForReading(JsonParser p) throws IOException
IOException
protected JsonGenerator _config(JsonGenerator g)
protected JsonParser _config(JsonParser p)
protected <T> T _closeWithError(Closeable cl, Exception e) throws IOException
IOException
protected void _close(Closeable cl)
protected <T> T _throw(Exception e) throws IOException
IOException
protected void _noTreeCodec(String msg)
Copyright © 2020 FasterXML. All rights reserved.