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();
}
}
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();
}
}
}
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;
}
Aggregations