@Override
    public void endElement(final String uri, final String localName, final String qName) throws SAXException {
        try {
            final boolean inMap = JSONParser.XML_ELEMENT_MAP.equals(elements.peek());

            if (jsonGenerator != null) {
                if (localName.equals(JSONParser.XML_ELEMENT_MAP)) {
                    jsonGenerator.writeEndObject();
                    elements.pop();
                } else if (localName.equals(JSONParser.XML_ELEMENT_ARRAY)) {
                    jsonGenerator.writeEndArray();
                    elements.pop();
                } else if (localName.equals(JSONParser.XML_ELEMENT_STRING)) {
                    final String value = content.toString();
                    if (!value.isEmpty()) {
                        if (inMap) {
                            if (writeKey()) {
                                jsonGenerator.writeString(value);
                            }
                        } else {
                            jsonGenerator.writeString(value);
                        }
                    }
                } else if (localName.equals(JSONParser.XML_ELEMENT_NUMBER)) {
                    final String value = content.toString();
                    if (!value.isEmpty()) {
                        if (inMap) {
                            if (writeKey()) {
                                jsonGenerator.writeNumber(value);
                            }
                        } else {
                            jsonGenerator.writeNumber(value);
                        }
                    }
                } else if (localName.equals(JSONParser.XML_ELEMENT_BOOLEAN)) {
                    final String value = content.toString();
                    if (!value.isEmpty()) {
                        if (inMap) {
                            if (writeKey()) {
                                jsonGenerator.writeBoolean(Boolean.parseBoolean(value));
                            }
                        } else {
                            jsonGenerator.writeBoolean(Boolean.parseBoolean(value));
                        }
                    }
                } else if (localName.equals(JSONParser.XML_ELEMENT_NULL)) {
                    if (inMap) {
                        if (writeKey()) {
                            jsonGenerator.writeNull();
                        }
                    } else {
                        jsonGenerator.writeNull();
                    }
                }
            }

            // Decrease the element depth
            depth--;

            // If depth = 0 then we have finished an event.
            if (depth == 0) {
                try {
                    // Insert root value separator if we are adding a trailing
                    // one, otherwise the jsonGenerator will have been
                    // configured to add one itself.
                    if (addTrailingRootValueSeparator) {
                        if (jsonGenerator != null) {
                            jsonGenerator.writeRaw(rootValueSeparator);
                        }
                    }

                    if (jsonGenerator != null) {
                        jsonGenerator.flush();
                    }
                    returnDestinations();
                } catch (final IOException e) {
                    throw new SAXException(e);
                }
            }

        } catch (final IOException | RuntimeException e) {
            error(e);
        } finally {
            clearKey();
            content.clear();
            super.endElement(uri, localName, qName);
        }
    }
@param uri       the Namespace URI, or the empty string if the element has no
                 Namespace URI or if Namespace processing is not being
                 performed
@param localName the local name (without prefix), or the empty string if
                 Namespace processing is not being performed
@param qName     the qualified XML name (with prefix), or the empty string if
                 qualified names are not available
@throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exception
@see stroom.pipeline.filter.AbstractXMLFilter#endElement(java.lang.String,
java.lang.String, java.lang.String)
stroom-pipeline/src/main/java/stroom/pipeline/writer/JSONWriter.java