Class 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
    • 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 goes
        isFormattedOutput - -
        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 goes
        isFormattedOutput - - 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 interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • endDocument

        public void endDocument()
                         throws org.xml.sax.SAXException
        Specified by:
        endDocument in interface org.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 interface org.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 interface org.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 interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • setDocumentLocator

        public void setDocumentLocator​(org.xml.sax.Locator locator)
        Specified by:
        setDocumentLocator in interface org.xml.sax.ContentHandler
      • skippedEntity

        public void skippedEntity​(java.lang.String name)
                           throws org.xml.sax.SAXException
        Specified by:
        skippedEntity in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • startDocument

        public void startDocument()
                           throws org.xml.sax.SAXException
        Specified by:
        startDocument in interface org.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 interface org.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 interface org.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 interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • withoutNl

        public void withoutNl()
      • withNl

        public void withNl()