-
Notifications
You must be signed in to change notification settings - Fork 47
Factory Configuration
StAXON comes with JSON-specific implementations of StAX' javax.xml.XMLInputFactory
and javax.xml.XMLOutputFactory
,
which provide various methods for reader and writer creation. When using StAXON, it is not unlikely, that these are
the only classes from StAXON you will reference directly:
-
JsonXMLInputFactory extends XMLInputFactory
- factory to create instances ofXMLStreamReader
andXMLEventReader
-
JsonXMLOutputFactory extends XMLOutputFactory
- factory to create instances ofXMLStreamWriter
andXMLEventWriter
These factories are configured with their setProperty(String, Object)
method. Beyond the standard properties defined by
XMLInputFactory and
XMLOutputFactory, StAXON's factories accept some additional, JSON-specific properties.
The JsonXMLInputFactory
class supports the following properties, defined by static string constants:
-
PROP_MULTIPLE_PI
(boolean) - whether to report the<? xml-multiple ... ?>
processing instruction (default istrue
) -
PROP_VIRTUAL_ROOT
(QName) - if set, insert root element with the given name (default isnull
) -
PROP_NAMESPACE_SEPARATOR
(char) - character used as namespace prefix separator (default is':'
) -
PROP_NAMESPACE_MAPPINGS
(Map) - associate namespace URIs with prefixes (default isnull
)
The JsonXMLOutputFactory
class supports the following properties, defined by static string constants:
-
PROP_AUTO_ARRAY
(boolean) - whether to automatically add JSON array boundaries (default isfalse
) -
PROP_AUTO_PRIMITIVE
(boolean) - whether to automatically convert element text to JSON primitives (default isfalse
) -
PROP_MULTIPLE_PI
(boolean) - whether to honor the<? xml-multiple ... ?>
processing instruction (default istrue
) -
PROP_VIRTUAL_ROOT
(QName) - if set, remove root element with the given name (default isnull
) -
PROP_PRETTY_PRINT
(boolean) - whether to format output for better readability (default isfalse
) -
PROP_NAMESPACE_SEPARATOR
(char) - character used as namespace prefix separator (default is':'
) -
PROP_NAMESPACE_DECLARATIONS
(boolean) - whether to write namespace declarations (default istrue
) -
PROP_NAMESPACE_MAPPINGS
(Map) - associate namespace URIs with prefixes (default isnull
)
Some of the properties are shared by input and output factories. For convenience, the JsonXMLConfig
interface provides a unified way to configure both - input and output factories - with a single configuration
object. The JsonXMLConfigBuilder
class provides a "fluid" API to create such a configuration:
JsonXMLConfig config = new JsonXMLConfigBuilder()
.virtualRoot("customer")
.prettyPrint(true)
.build();
XMLInputFactory inputFactory = new JsonXMLInputFactory(config);
...
XMLOutputFactory outputFactory = new JsonXMLOutputFactory(config);
...
StAXON's input/output factories use an instance of JsonStreamFactory
as their JSON "backend", i.e. to create
objects that do the actual JSON reading/writing.
Apart from the ability to implement your own, StAXON provides the following stream factory implementations:
-
StreamFactoryImpl
- StAXON's default JSON I/O implementation, contained in requiresstaxon-<version>.jar
-
JsonProcessingStreamFactory
- delegate JSON I/O to JSON Processing API (JSR-353), requiresstaxon-jsr353-<version>.jar
-
GsonStreamFactory
- delegate JSON I/O to Gson, requiresstaxon-gson-<version>.jar
-
JacksonStreamFactory
- delegate JSON I/O to Jackson, requiresstaxon-jackson-<version>.jar
Unless you want to avoid a dependency to an external JSON processor, it is recommended to choose one of those as backend.
If you use the default input/output factory constructors, StAXON performs the following lookup procedure to determine the stream factory class to instantiate:
- Use the Services API (as detailed in the JAR specification). If a resource with the name
of
META-INF/services/de.odysseus.staxon.json.stream.JsonStreamFactory
exists, then its first line, if present, is used as the UTF-8 encoded name of the implementation class. - Use the properties file
lib/staxon.properties
in the JRE directory. If this file exists and is readable by thejava.util.Properties.load(InputStream)
method, and it contains an entry whose key isde.odysseus.staxon.json.stream.JsonStreamFactory
, then the value of that entry is used as the name of the implementation class. - Use the
de.odysseus.staxon.json.stream.JsonStreamFactory
system property. If a system property with this name is defined, then its value is used as the name of the implementation class. - Use platform default:
de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl
.
The staxon-jsr353-<version>.jar
, staxon-gson-<version>.jar
and staxon-jackson-<version>.jar
modules use the Services API to
register their stream factory class. That is, if you have one of those on your classpath, StAXON
will automatically use the corresponding stream factory implementation.
As an alternative, you may always explicitly pass a stream factory instance to your input/output factory at construction time.