Search in sources :

Example 1 with JSONParser

use of org.teiid.json.simple.JSONParser in project teiid by teiid.

the class JSONFunctionMethods method jsonParse.

@TeiidFunction(category = FunctionCategoryConstants.JSON)
public static ClobType jsonParse(BlobType val, boolean wellformed) throws SQLException, IOException, ParseException {
    InputStreamReader r = XMLSystemFunctions.getJsonReader(val);
    try {
        if (!wellformed) {
            JSONParser parser = new JSONParser();
            parser.parse(r, validatingContentHandler);
        }
        ClobImpl clobImpl = new ClobImpl();
        clobImpl.setStreamFactory(new InputStreamFactory.BlobInputStreamFactory(val.getReference()));
        clobImpl.setEncoding(r.getEncoding());
        ClobType ct = new ClobType(clobImpl);
        ct.setType(Type.JSON);
        return ct;
    } finally {
        r.close();
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) InputStreamReader(java.io.InputStreamReader) JSONParser(org.teiid.json.simple.JSONParser) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) ClobImpl(org.teiid.core.types.ClobImpl)

Example 2 with JSONParser

use of org.teiid.json.simple.JSONParser in project teiid by teiid.

the class JSONFunctionMethods method jsonParse.

@TeiidFunction(category = FunctionCategoryConstants.JSON)
public static ClobType jsonParse(ClobType val, boolean wellformed) throws SQLException, IOException, ParseException {
    Reader r = null;
    if (val.getType() == Type.JSON) {
        return val;
    }
    if (!wellformed) {
        r = val.getCharacterStream();
    }
    try {
        if (!wellformed) {
            JSONParser parser = new JSONParser();
            parser.parse(r, validatingContentHandler);
        }
        ClobType ct = new ClobType(val.getReference());
        ct.setType(Type.JSON);
        return ct;
    } finally {
        if (r != null) {
            r.close();
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JSONParser(org.teiid.json.simple.JSONParser)

Example 3 with JSONParser

use of org.teiid.json.simple.JSONParser in project teiid by teiid.

the class XMLSystemFunctions method jsonToXml.

private static SQLXML jsonToXml(CommandContext context, final String rootName, final Reader r, boolean stream) throws TeiidComponentException, TeiidProcessingException {
    JSONParser parser = new JSONParser();
    final JsonToXmlContentHandler reader = new JsonToXmlContentHandler(rootName, r, parser, threadLocalEventtFactory.get());
    SQLXMLImpl sqlXml = null;
    if (stream) {
        try {
            // jre 1.7 event logic does not set a dummy location and throws an NPE in StAXSource, so we explicitly set a location
            // the streaming result will be directly consumed, so there's no danger that we're stepping on another location
            reader.eventFactory.setLocation(dummyLocation);
            sqlXml = new StAXSQLXML(new StAXSource(reader));
        } catch (XMLStreamException e) {
            throw new TeiidProcessingException(e);
        }
    } else {
        sqlXml = XMLSystemFunctions.saveToBufferManager(context.getBufferManager(), new XMLTranslator() {

            @Override
            public void translate(Writer writer) throws TransformerException, IOException {
                try {
                    XMLOutputFactory factory = getOutputFactory();
                    final XMLEventWriter streamWriter = factory.createXMLEventWriter(writer);
                    streamWriter.add(reader);
                    // woodstox needs a flush rather than a close
                    streamWriter.flush();
                } catch (XMLStreamException e) {
                    throw new TransformerException(e);
                } finally {
                    try {
                        r.close();
                    } catch (IOException e) {
                    }
                }
            }
        }, context);
    }
    XMLType result = new XMLType(sqlXml);
    result.setType(Type.DOCUMENT);
    return result;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StAXSource(javax.xml.transform.stax.StAXSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) JSONParser(org.teiid.json.simple.JSONParser) XMLEventWriter(javax.xml.stream.XMLEventWriter) TransformerException(javax.xml.transform.TransformerException) StAXSQLXML(org.teiid.util.StAXSQLXML)

Aggregations

JSONParser (org.teiid.json.simple.JSONParser)3 InputStreamReader (java.io.InputStreamReader)2 ClobType (org.teiid.core.types.ClobType)2 Reader (java.io.Reader)1 XMLEventWriter (javax.xml.stream.XMLEventWriter)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TransformerException (javax.xml.transform.TransformerException)1 StAXSource (javax.xml.transform.stax.StAXSource)1 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 ClobImpl (org.teiid.core.types.ClobImpl)1 InputStreamFactory (org.teiid.core.types.InputStreamFactory)1 StAXSQLXML (org.teiid.util.StAXSQLXML)1