Package org.apache.uima.json.impl
Class JsonContentHandlerJacksonWrapper
- java.lang.Object
-
- org.apache.uima.json.impl.JsonContentHandlerJacksonWrapper
-
- All Implemented Interfaces:
org.xml.sax.ContentHandler
public class JsonContentHandlerJacksonWrapper extends java.lang.Object implements org.xml.sax.ContentHandler
Utility class that generates JSON output for UIMA descriptors and CASs This class is built as a wrapper for a popular open-source implementation of JSON capabilities, "Jackson". Unusually, it implements the ContentHandler interface, so it can be use with the existing code in UIMA which uses content handlers for serialization. Because of this, it wraps the IOExceptions that the Jackson package throws, into SAXExceptions that ContentHandlers throw. Use: Create an instance, specifying the output as a Writer or OutputStream or File (These three forms are supported by the underlying Jackson impl) Specify also if doing pretty-printing Call from other serialization class that does walking (either MetaDataObject_impl or XmiCasSerializer), to this instance This class is exposes the Jackson "Generator" API for streaming, and instances of the Jackson Factory instance for configuring. The caller uses both this class and the Jackson Generator class. This class lets the underlying Jackson PrettyPrinter classes track the indent level. PrettyPrinting is implemented via customization of the Jackson PrettyPrinting classes
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
JsonContentHandlerJacksonWrapper.FixedSpaceIndenterWithNesting
private class
JsonContentHandlerJacksonWrapper.UimaJsonPrettyPrinter
-
Field Summary
Fields Modifier and Type Field Description private static char[]
BLANKS
private boolean
doNl
private static JsonContentHandlerJacksonWrapper.FixedSpaceIndenterWithNesting
FSIWN
private boolean
isFormattedOutput
private com.fasterxml.jackson.core.JsonGenerator
jg
static java.lang.String
SYSTEM_LINE_FEED
private JsonContentHandlerJacksonWrapper.UimaJsonPrettyPrinter
uimaPrettyPrinter
-
Constructor Summary
Constructors Constructor Description JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonFactory jsonFactory, java.lang.Object o)
Makes a Json content handler, using a specified JsonFactory instance that can be configured according to the Jackson implementation.JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonFactory jsonFactory, java.lang.Object o, boolean isFormattedOutput)
Makes a Json content handler, using a specified JsonFactory instance that can be configured according to the Jackson implementation, and specifies a prettyprinting boolean flag (default is no prettyprinting).JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonGenerator jsonGenerator)
Makes a Json content handler, using a specified JsonGenerator instanceJsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean isFormattedOutput)
Makes a Json content handler, using a specified JsonGenerator instanceJsonContentHandlerJacksonWrapper(java.lang.Object destination)
Makes a Json content handler that sends its output to the specified destinationJsonContentHandlerJacksonWrapper(java.lang.Object o, boolean isFormattedOutput)
Makes a Json content handler, and specifies a prettyprinting boolean flag (default is no prettyprinting).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
characters(char[] ch, int start, int length)
private static com.fasterxml.jackson.core.JsonGenerator
createGenerator(com.fasterxml.jackson.core.JsonFactory f, java.lang.Object o)
void
endDocument()
void
endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
void
endPrefixMapping(java.lang.String prefix)
com.fasterxml.jackson.core.JsonGenerator
getJsonGenerator()
void
ignorableWhitespace(char[] ch, int start, int length)
boolean
isFormattedOutput()
private void
outputAttrsAsProperties(org.xml.sax.Attributes atts)
Assumes a Json object has been started, and adds property fields to it This method allows reusing common code in the caller.void
processingInstruction(java.lang.String target, java.lang.String data)
void
setDocumentLocator(org.xml.sax.Locator locator)
void
skippedEntity(java.lang.String name)
void
startDocument()
void
startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
void
startPrefixMapping(java.lang.String prefix, java.lang.String uri)
private void
unsupported()
void
withNl()
void
withoutNl()
void
writeNlJustBeforeNext()
Call this to indicate that the prettyprinter should write a new line just before the next significant output.
-
-
-
Field Detail
-
BLANKS
private static final char[] BLANKS
-
SYSTEM_LINE_FEED
public static final java.lang.String SYSTEM_LINE_FEED
-
isFormattedOutput
private final boolean isFormattedOutput
-
jg
private final com.fasterxml.jackson.core.JsonGenerator jg
-
doNl
private boolean doNl
-
uimaPrettyPrinter
private final JsonContentHandlerJacksonWrapper.UimaJsonPrettyPrinter uimaPrettyPrinter
-
FSIWN
private static final JsonContentHandlerJacksonWrapper.FixedSpaceIndenterWithNesting FSIWN
-
-
Constructor Detail
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(java.lang.Object destination) throws org.xml.sax.SAXException
Makes a Json content handler that sends its output to the specified destination- Parameters:
destination
- - can be a File, an OutputStream, or a Writer- Throws:
org.xml.sax.SAXException
- wrapping an IOException
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonFactory jsonFactory, java.lang.Object o) throws org.xml.sax.SAXException
Makes a Json content handler, using a specified JsonFactory instance that can be configured according to the Jackson implementation. The resulting content handler will send its output to the specified destination- Parameters:
jsonFactory
- -o
- - where the output goes- Throws:
org.xml.sax.SAXException
- wrapping an IOException
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(java.lang.Object o, boolean isFormattedOutput) throws org.xml.sax.SAXException
Makes a Json content handler, and specifies a prettyprinting boolean flag (default is no prettyprinting). The resulting content handler will send its output to the specified destination- Parameters:
o
- - where the output goesisFormattedOutput
- -- Throws:
org.xml.sax.SAXException
- wrapping an IOException
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonFactory jsonFactory, java.lang.Object o, boolean isFormattedOutput) throws org.xml.sax.SAXException
Makes a Json content handler, using a specified JsonFactory instance that can be configured according to the Jackson implementation, and specifies a prettyprinting boolean flag (default is no prettyprinting). The resulting content handler will send its output to the specified destination- Parameters:
jsonFactory
- -o
- where the output goesisFormattedOutput
- - true for pretty printing- Throws:
org.xml.sax.SAXException
- wrapping an IOException
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonGenerator jsonGenerator)
Makes a Json content handler, using a specified JsonGenerator instance- Parameters:
jsonGenerator
- -
-
JsonContentHandlerJacksonWrapper
public JsonContentHandlerJacksonWrapper(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean isFormattedOutput)
Makes a Json content handler, using a specified JsonGenerator instance- Parameters:
jsonGenerator
- -isFormattedOutput
- - set to true for prettyprinting, default is false
-
-
Method Detail
-
isFormattedOutput
public boolean isFormattedOutput()
-
getJsonGenerator
public com.fasterxml.jackson.core.JsonGenerator getJsonGenerator()
-
createGenerator
private static com.fasterxml.jackson.core.JsonGenerator createGenerator(com.fasterxml.jackson.core.JsonFactory f, java.lang.Object o) throws org.xml.sax.SAXException
- Throws:
org.xml.sax.SAXException
-
writeNlJustBeforeNext
public void writeNlJustBeforeNext()
Call this to indicate that the prettyprinter should write a new line just before the next significant output. It won't do this before a "comma", and some other punctuation. Has no effect if no prettyprinting is being done.
-
outputAttrsAsProperties
private void outputAttrsAsProperties(org.xml.sax.Attributes atts)
Assumes a Json object has been started, and adds property fields to it This method allows reusing common code in the caller. The attr values can be arrays (expressed as a valid Json String including the separators, used by XmiCasSerializer only strings (which will be scanned for needed escaping and surrounded by necessary quotes other (which are assumed to not need surrounding quotes- Parameters:
atts
-
-
unsupported
private void unsupported()
-
characters
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
- Specified by:
characters
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
endDocument
public void endDocument() throws org.xml.sax.SAXException
- Specified by:
endDocument
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
endPrefixMapping
public void endPrefixMapping(java.lang.String prefix) throws org.xml.sax.SAXException
- Specified by:
endPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
ignorableWhitespace
public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException
- Specified by:
ignorableWhitespace
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
processingInstruction
public void processingInstruction(java.lang.String target, java.lang.String data) throws org.xml.sax.SAXException
- Specified by:
processingInstruction
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
setDocumentLocator
public void setDocumentLocator(org.xml.sax.Locator locator)
- Specified by:
setDocumentLocator
in interfaceorg.xml.sax.ContentHandler
-
skippedEntity
public void skippedEntity(java.lang.String name) throws org.xml.sax.SAXException
- Specified by:
skippedEntity
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
startDocument
public void startDocument() throws org.xml.sax.SAXException
- Specified by:
startDocument
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
startPrefixMapping
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws org.xml.sax.SAXException
- Specified by:
startPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
startElement
public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException
- Specified by:
startElement
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
endElement
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
- Specified by:
endElement
in interfaceorg.xml.sax.ContentHandler
- Throws:
org.xml.sax.SAXException
-
withoutNl
public void withoutNl()
-
withNl
public void withNl()
-
-