Package com.fasterxml.jackson.jr.ob
Class JSON
- java.lang.Object
-
- com.fasterxml.jackson.jr.ob.JSON
-
- All Implemented Interfaces:
com.fasterxml.jackson.core.Versioned
public class JSON extends java.lang.Object implements com.fasterxml.jackson.core.VersionedMain entry point for functionality for reading and writing JSON and configuring details of reading and writing.Note that instances are fully immutable, and thereby thread-safe.
Note on source types: source to read is declared as
Objectbut covers following types:InputStreamReaderbyte[]char[]String/CharSequenceURLFile
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJSON.BuilderBuilder class that needs to be used for certain kind of "static" configuration (settings that can not vary on per-call basis forJSON), such as Extension registration.static classJSON.FeatureSimple on/off (enabled/disabled) features forJSON; used for simple configuration aspects.
-
Field Summary
Fields Modifier and Type Field Description protected int_featuresprotected com.fasterxml.jackson.core.JsonFactory_jsonFactoryUnderlying JSON factory used for creating Streaming parsers and generators.protected com.fasterxml.jackson.core.PrettyPrinter_prettyPrinterprotected JSONReader_readerBlueprint instance of the reader to use for reading JSON as simple Objects.protected com.fasterxml.jackson.core.TreeCodec_treeCodecOptional handler forTreeNodevalues: if defined, we can read and writeTreeNodeinstances that codec supports.protected ValueReaderLocator_valueReaderLocatorprotected ValueWriterLocator_valueWriterLocatorprotected JSONWriter_writerBlueprint instance of the writer to use for writing JSON given simple Objects.static intCACHE_FLAGSstatic JSONstdSingleton instance with standard, default configuration.
-
Constructor Summary
Constructors Modifier Constructor Description JSON()JSON(com.fasterxml.jackson.core.JsonFactory jsonF)JSON(JSON.Builder b)Constructor used when creating instance usingJSON.Builder.protectedJSON(JSON base, int features, com.fasterxml.jackson.core.JsonFactory jsonF, com.fasterxml.jackson.core.TreeCodec trees, JSONReader r, JSONWriter w, com.fasterxml.jackson.core.PrettyPrinter pp)protectedJSON(JSON base, ValueReaderLocator rloc, ValueWriterLocator wloc)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void_close(java.io.Closeable cl)protected <T> T_closeWithError(java.io.Closeable cl, java.lang.Exception e)protected com.fasterxml.jackson.core.JsonGenerator_config(com.fasterxml.jackson.core.JsonGenerator g)protected com.fasterxml.jackson.core.JsonParser_config(com.fasterxml.jackson.core.JsonParser p)protected JSONReader_defaultReader()protected JSONWriter_defaultWriter()protected com.fasterxml.jackson.core.JsonParser_initForReading(com.fasterxml.jackson.core.JsonParser p)protected void_noTreeCodec(java.lang.String msg)protected com.fasterxml.jackson.core.JsonParser_parser(java.lang.Object source)protected JSONReader_readerForOperation(com.fasterxml.jackson.core.JsonParser p)protected <T> T_throw(java.lang.Exception e)protected JSON_with(int features)Internal mutant factory method used for constructingprotected JSON_with(int features, com.fasterxml.jackson.core.JsonFactory jsonF, com.fasterxml.jackson.core.TreeCodec trees, JSONReader reader, JSONWriter writer, com.fasterxml.jackson.core.PrettyPrinter pp)protected void_writeAndClose(java.lang.Object value, com.fasterxml.jackson.core.JsonGenerator g)protected JSONWriter_writerForOperation(com.fasterxml.jackson.core.JsonGenerator gen)java.lang.ObjectanyFrom(java.lang.Object source)Read method that will take given JSON Source (of one of supported types), read contents and map it to one of simple mappings (Mapfor JSON Objects,Listfor JSON Arrays,Stringfor JSON Strings, null for JSON null,Booleanfor JSON booleans andNumberfor JSON numbers.ValueIterator<java.lang.Object>anySequenceFrom(java.lang.Object source)Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound as "Simple" type:Map,List,String,NumberorBoolean.java.lang.Object[]arrayFrom(java.lang.Object source)<T> T[]arrayOfFrom(java.lang.Class<T> type, java.lang.Object source)byte[]asBytes(java.lang.Object value)com.fasterxml.jackson.core.ObjectCodecasCodec()Convenience method for constructing an adapter that uses this instance as aObjectCodecjava.lang.StringasString(java.lang.Object value)<T> TbeanFrom(java.lang.Class<T> type, java.lang.Object source)<T> ValueIterator<T>beanSequenceFrom(java.lang.Class<T> type, java.lang.Object source)Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound to specific Bean (POJO) type.static JSON.Builderbuilder()static JSON.Builderbuilder(com.fasterxml.jackson.core.JsonFactory streamFactory)JSONComposer<byte[]>composeBytes()<C extends java.util.Collection<java.lang.Object>>
CollectionComposer<?,C>composeCollection(C collection)CollectionComposer<?,java.util.List<java.lang.Object>>composeList()MapComposer<?>composeMap()MapComposer<?>composeMap(java.util.Map<java.lang.String,java.lang.Object> map)JSONComposer<java.lang.String>composeString()JSONComposer<java.io.OutputStream>composeTo(java.io.File f)JSONComposer<java.io.OutputStream>composeTo(java.io.OutputStream out)JSONComposer<java.io.OutputStream>composeTo(java.io.Writer w)JSONComposer<java.io.OutputStream>composeUsing(com.fasterxml.jackson.core.JsonGenerator gen)<T extends com.fasterxml.jackson.core.TreeNode>
TcreateArrayNode()Convenience method, equivalent to:<T extends com.fasterxml.jackson.core.TreeNode>
TcreateObjectNode()Convenience method, equivalent to:com.fasterxml.jackson.core.JsonParsercreateParser(java.lang.Object source)Factory method for opening aJsonParserto read content from one of following supported sourcesInputStreamReaderbyte[]char[]String/CharSequenceURLFilecom.fasterxml.jackson.core.JsonFactorygetStreamingFactory()com.fasterxml.jackson.core.TreeCodecgetTreeCodec()booleanisEnabled(JSON.Feature f)java.util.List<java.lang.Object>listFrom(java.lang.Object source)<T> java.util.List<T>listOfFrom(java.lang.Class<T> type, java.lang.Object source)java.util.Map<java.lang.String,java.lang.Object>mapFrom(java.lang.Object source)<T> java.util.Map<java.lang.String,T>mapOfFrom(java.lang.Class<T> type, java.lang.Object source)Read method for reading aMapoftype(usually POJO) values.<T extends com.fasterxml.jackson.core.TreeNode>
TtreeFrom(java.lang.Object source)Method for reading content as a JSON Tree (of type that configuredTreeCodec, seewith(TreeCodec)) supports.<T extends com.fasterxml.jackson.core.TreeNode>
ValueIterator<T>treeSequenceFrom(java.lang.Object source)Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound as JSON Trees(of type that configuredTreeCodec, seewith(TreeCodec)) supports.com.fasterxml.jackson.core.Versionversion()JSONwith(com.fasterxml.jackson.core.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 latestJSONwith(com.fasterxml.jackson.core.PrettyPrinter pp)Mutant factory for constructing an instance with specifiedPrettyPrinter, and returning new instance (or, if there would be no change, this instance).JSONwith(com.fasterxml.jackson.core.TreeCodec c)Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceJSONwith(CollectionBuilder b)Mutant factory for constructing an instance with specifiedCollectionBuilder, and returning new instance (or, if there would be no change, this instance).JSONwith(MapBuilder b)Mutant factory for constructing an instance with specifiedMapBuilder, and returning new instance (or, if there would be no change, this instance).JSONwith(ReaderWriterProvider rwp)Deprecated.Since 2.11 should register usingJacksonJrExtensionJSONwith(JSONReader r)Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceJSONwith(JSONWriter w)Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceJSONwith(JSON.Feature... features)Mutant factory for constructing an instance with specified features enabled.JSONwith(JSON.Feature feature, boolean state)Mutant factory for constructing an instance with specified feature enabled or disabled (depending onstate), and returning an instance with that setting; this may either be this instance (if feature already had specified state), or a newly constructed instance.JSONwithout(JSON.Feature... features)Mutant factory for constructing an instance with specified features disabled.voidwrite(java.lang.Object value, com.fasterxml.jackson.core.JsonGenerator gen)voidwrite(java.lang.Object value, java.io.File f)voidwrite(java.lang.Object value, java.io.OutputStream out)voidwrite(java.lang.Object value, java.io.Writer w)
-
-
-
Field Detail
-
CACHE_FLAGS
public static final int CACHE_FLAGS
-
std
public static final JSON std
Singleton instance with standard, default configuration. May be used with direct references like:String json = JSON.std.asString(map);
-
_jsonFactory
protected final com.fasterxml.jackson.core.JsonFactory _jsonFactory
Underlying JSON factory used for creating Streaming parsers and generators.
-
_treeCodec
protected final com.fasterxml.jackson.core.TreeCodec _treeCodec
Optional handler forTreeNodevalues: if defined, we can read and writeTreeNodeinstances that codec supports.
-
_valueReaderLocator
protected final ValueReaderLocator _valueReaderLocator
-
_valueWriterLocator
protected final ValueWriterLocator _valueWriterLocator
-
_reader
protected final JSONReader _reader
Blueprint instance of the reader to use for reading JSON as simple Objects.
-
_writer
protected final JSONWriter _writer
Blueprint instance of the writer to use for writing JSON given simple Objects.
-
_features
protected final int _features
-
_prettyPrinter
protected final com.fasterxml.jackson.core.PrettyPrinter _prettyPrinter
-
-
Constructor Detail
-
JSON
public JSON()
-
JSON
public JSON(com.fasterxml.jackson.core.JsonFactory jsonF)
-
JSON
public JSON(JSON.Builder b)
Constructor used when creating instance usingJSON.Builder.- Parameters:
b- Builder that has configured settings to use.- Since:
- 2.11
-
JSON
protected JSON(JSON base, int features, com.fasterxml.jackson.core.JsonFactory jsonF, com.fasterxml.jackson.core.TreeCodec trees, JSONReader r, JSONWriter w, com.fasterxml.jackson.core.PrettyPrinter pp)
-
JSON
protected JSON(JSON base, ValueReaderLocator rloc, ValueWriterLocator wloc)
-
-
Method Detail
-
builder
public static JSON.Builder builder()
- Since:
- 2.11
-
builder
public static JSON.Builder builder(com.fasterxml.jackson.core.JsonFactory streamFactory)
- Since:
- 2.11
-
_defaultReader
protected JSONReader _defaultReader()
-
_defaultWriter
protected JSONWriter _defaultWriter()
-
asCodec
public com.fasterxml.jackson.core.ObjectCodec asCodec()
Convenience method for constructing an adapter that uses this instance as aObjectCodec- Returns:
- Wrapper over this object to adapt to
ObjectCodecAPI
-
version
public com.fasterxml.jackson.core.Version version()
- Specified by:
versionin interfacecom.fasterxml.jackson.core.Versioned
-
with
public JSON with(com.fasterxml.jackson.core.PrettyPrinter pp)
Mutant factory for constructing an instance with specifiedPrettyPrinter, and returning new instance (or, if there would be no change, this instance).- Parameters:
pp-PrettyPrinterto use for pretty-printing output (ofnullto disable pretty-printing)- Returns:
- New instance with specified
PrettyPrinter(if not same as currently configured);thisotherwise.
-
with
public JSON with(JSON.Feature feature, boolean state)
Mutant factory for constructing an instance with specified feature enabled or disabled (depending onstate), and returning an instance with that setting; this may either be this instance (if feature already had specified state), or a newly constructed instance.
-
with
public JSON with(JSON.Feature... features)
Mutant factory for constructing an instance with specified features enabled.
-
without
public JSON without(JSON.Feature... features)
Mutant factory for constructing an instance with specified features disabled.
-
_with
protected final JSON _with(int features)
Internal mutant factory method used for constructing
-
with
@Deprecated public JSON with(com.fasterxml.jackson.core.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 latestMutant factory method for constructing new instance with specifiedJsonFactoryif different from currently configured one (if not, returnthisas-is)- Parameters:
f- Jackson core format factory to use for low-level decoding/encoding- Returns:
- New instance with specified factory (if not same as currently configured);
thisotherwise.
-
with
@Deprecated public JSON with(com.fasterxml.jackson.core.TreeCodec c)
Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceMutant factory for constructing an instance with specifiedTreeCodec, and returning new instance (or, if there would be no change, this instance).- Parameters:
c- Tree codec to use for reading/writing of tree representation- Returns:
- New instance with specified codec (if not same as currently configured);
thisotherwise.
-
with
@Deprecated public JSON with(JSONReader r)
Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceMutant factory for constructing an instance with specifiedJSONReader, and returning new instance (or, if there would be no change, this instance).- Parameters:
r- CustomizedJSONReaderto use instead of standard one- Returns:
- New instance with specified
JSONReader(if not same as currently configured);thisotherwise.
-
with
@Deprecated public JSON with(JSONWriter w)
Deprecated.Since 2.11 should try using builder (seebuilder()and create properly configured instanceMutant factory for constructing an instance with specifiedJSONWriter, and returning new instance (or, if there would be no change, this instance).- Parameters:
w- CustomizedJSONWriterto use instead of standard one- Returns:
- New instance with specified
JSONWriter(if not same as currently configured);thisotherwise.
-
with
public JSON with(MapBuilder b)
Mutant factory for constructing an instance with specifiedMapBuilder, and returning new instance (or, if there would be no change, this instance).
-
with
public JSON with(CollectionBuilder b)
Mutant factory for constructing an instance with specifiedCollectionBuilder, and returning new instance (or, if there would be no change, this instance).
-
with
@Deprecated public JSON with(ReaderWriterProvider rwp)
Deprecated.Since 2.11 should register usingJacksonJrExtensionMutant factory for constructing an instance with specifiedReaderWriterProvider, and returning new instance (or, if there would be no change, this instance).
-
_with
protected JSON _with(int features, com.fasterxml.jackson.core.JsonFactory jsonF, com.fasterxml.jackson.core.TreeCodec trees, JSONReader reader, JSONWriter writer, com.fasterxml.jackson.core.PrettyPrinter pp)
-
getTreeCodec
public com.fasterxml.jackson.core.TreeCodec getTreeCodec()
-
getStreamingFactory
public com.fasterxml.jackson.core.JsonFactory getStreamingFactory()
-
isEnabled
public final boolean isEnabled(JSON.Feature f)
-
createParser
public com.fasterxml.jackson.core.JsonParser createParser(java.lang.Object source) throws java.io.IOException, JSONObjectExceptionFactory method for opening aJsonParserto read content from one of following supported sourcesInputStreamReaderbyte[]char[]String/CharSequenceURLFile
Rules regarding closing of the underlying source follow rules that
JsonFactoryhas for itscreateParsermethod.- Throws:
java.io.IOExceptionJSONObjectException- Since:
- 2.10
-
asString
public java.lang.String asString(java.lang.Object value) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
asBytes
public byte[] asBytes(java.lang.Object value) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
write
public void write(java.lang.Object value, com.fasterxml.jackson.core.JsonGenerator gen) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
write
public void write(java.lang.Object value, java.io.OutputStream out) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
write
public void write(java.lang.Object value, java.io.Writer w) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
write
public void write(java.lang.Object value, java.io.File f) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
composeUsing
public JSONComposer<java.io.OutputStream> composeUsing(com.fasterxml.jackson.core.JsonGenerator gen) throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeTo
public JSONComposer<java.io.OutputStream> composeTo(java.io.OutputStream out) throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeTo
public JSONComposer<java.io.OutputStream> composeTo(java.io.Writer w) throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeTo
public JSONComposer<java.io.OutputStream> composeTo(java.io.File f) throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeString
public JSONComposer<java.lang.String> composeString() throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeBytes
public JSONComposer<byte[]> composeBytes() throws java.io.IOException, JSONObjectException
- Throws:
java.io.IOExceptionJSONObjectException
-
composeList
public CollectionComposer<?,java.util.List<java.lang.Object>> composeList()
-
composeCollection
public <C extends java.util.Collection<java.lang.Object>> CollectionComposer<?,C> composeCollection(C collection)
-
composeMap
public MapComposer<?> composeMap()
-
composeMap
public MapComposer<?> composeMap(java.util.Map<java.lang.String,java.lang.Object> map)
-
listFrom
public java.util.List<java.lang.Object> listFrom(java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
listOfFrom
public <T> java.util.List<T> listOfFrom(java.lang.Class<T> type, java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
arrayFrom
public java.lang.Object[] arrayFrom(java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
arrayOfFrom
public <T> T[] arrayOfFrom(java.lang.Class<T> type, java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
mapFrom
public java.util.Map<java.lang.String,java.lang.Object> mapFrom(java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
mapOfFrom
public <T> java.util.Map<java.lang.String,T> mapOfFrom(java.lang.Class<T> type, java.lang.Object source) throws java.io.IOException, JSONObjectExceptionRead method for reading aMapoftype(usually POJO) values.- Throws:
java.io.IOExceptionJSONObjectException- Since:
- 2.10
-
beanFrom
public <T> T beanFrom(java.lang.Class<T> type, java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
anyFrom
public java.lang.Object anyFrom(java.lang.Object source) throws java.io.IOExceptionRead method that will take given JSON Source (of one of supported types), read contents and map it to one of simple mappings (Mapfor JSON Objects,Listfor JSON Arrays,Stringfor JSON Strings, null for JSON null,Booleanfor JSON booleans andNumberfor JSON numbers.Supported source types include:
InputStreamReaderFileURLStringbyte[]char[]
- Throws:
java.io.IOException
-
treeFrom
public <T extends com.fasterxml.jackson.core.TreeNode> T treeFrom(java.lang.Object source) throws java.io.IOException, JSONObjectExceptionMethod for reading content as a JSON Tree (of type that configuredTreeCodec, seewith(TreeCodec)) supports.- Throws:
java.io.IOExceptionJSONObjectException- Since:
- 2.8
-
beanSequenceFrom
public <T> ValueIterator<T> beanSequenceFrom(java.lang.Class<T> type, java.lang.Object source) throws java.io.IOException, JSONObjectException
Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound to specific Bean (POJO) type.- Throws:
java.io.IOExceptionJSONObjectException- Since:
- 2.10
-
anySequenceFrom
public ValueIterator<java.lang.Object> anySequenceFrom(java.lang.Object source) throws java.io.IOException, JSONObjectException
Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound as "Simple" type:Map,List,String,NumberorBoolean.- Throws:
java.io.IOExceptionJSONObjectException- Since:
- 2.10
-
treeSequenceFrom
public <T extends com.fasterxml.jackson.core.TreeNode> ValueIterator<T> treeSequenceFrom(java.lang.Object source) throws java.io.IOException, JSONObjectException
Method for creatingValueIteratorfor reading streaming JSON content (specifically line-delimited and concatenated variants); individual values are bound as JSON Trees(of type that configuredTreeCodec, seewith(TreeCodec)) supports.- Throws:
java.io.IOExceptionJSONObjectException
-
createArrayNode
public <T extends com.fasterxml.jackson.core.TreeNode> T createArrayNode()
Convenience method, equivalent to:getTreeCodec().createArrayNode();
Note that for call to succeed aTreeCodecmust have been configured with this instance usingwith(TreeCodec)method.- Since:
- 2.8
-
createObjectNode
public <T extends com.fasterxml.jackson.core.TreeNode> T createObjectNode()
Convenience method, equivalent to:getTreeCodec().createObjectNode();
Note that for call to succeed aTreeCodecmust have been configured with this instance usingwith(TreeCodec)method.- Since:
- 2.8
-
_writeAndClose
protected final void _writeAndClose(java.lang.Object value, com.fasterxml.jackson.core.JsonGenerator g) throws java.io.IOException- Throws:
java.io.IOException
-
_writerForOperation
protected JSONWriter _writerForOperation(com.fasterxml.jackson.core.JsonGenerator gen)
-
_readerForOperation
protected JSONReader _readerForOperation(com.fasterxml.jackson.core.JsonParser p)
-
_parser
protected com.fasterxml.jackson.core.JsonParser _parser(java.lang.Object source) throws java.io.IOException, JSONObjectException- Throws:
java.io.IOExceptionJSONObjectException
-
_initForReading
protected com.fasterxml.jackson.core.JsonParser _initForReading(com.fasterxml.jackson.core.JsonParser p) throws java.io.IOException- Throws:
java.io.IOException
-
_config
protected com.fasterxml.jackson.core.JsonGenerator _config(com.fasterxml.jackson.core.JsonGenerator g)
-
_config
protected com.fasterxml.jackson.core.JsonParser _config(com.fasterxml.jackson.core.JsonParser p)
-
_closeWithError
protected <T> T _closeWithError(java.io.Closeable cl, java.lang.Exception e) throws java.io.IOException- Throws:
java.io.IOException
-
_close
protected void _close(java.io.Closeable cl)
-
_throw
protected <T> T _throw(java.lang.Exception e) throws java.io.IOException- Throws:
java.io.IOException
-
_noTreeCodec
protected void _noTreeCodec(java.lang.String msg)
-
-