Search in sources :

Example 1 with CookJsonParser

use of org.yuanheng.cookjson.CookJsonParser in project jaqy by Teradata.

the class HelperConfigUtils method load.

public static void load(HelperManager manager, String json) throws IOException {
    JsonValue v;
    CookJsonParser p = new TextJsonParser(new StringReader(json), json.length());
    p.next();
    v = p.getValue();
    p.close();
    load(manager, (JsonArray) v);
}
Also used : TextJsonParser(org.yuanheng.cookjson.TextJsonParser) JsonValue(javax.json.JsonValue) StringReader(java.io.StringReader) CookJsonParser(org.yuanheng.cookjson.CookJsonParser)

Example 2 with CookJsonParser

use of org.yuanheng.cookjson.CookJsonParser in project jaqy by Teradata.

the class JsonUtils method print.

public static long print(Globals globals, CookJsonGenerator g, JaqyResultSet rs, long limit) throws SQLException {
    JaqyResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();
    String[] headers = new String[columns];
    boolean[] jsonCheck = new boolean[columns];
    for (int i = 0; i < columns; ++i) {
        headers[i] = metaData.getColumnLabel(i + 1);
        jsonCheck[i] = metaData.isJson(i + 1);
    }
    g.writeStartArray();
    long count = 0;
    if (limit == 0)
        limit = Long.MAX_VALUE;
    while (rs.next() && count < limit) {
        ++count;
        g.writeStartObject();
        for (int i = 0; i < columns; ++i) {
            Object obj = rs.getObject(i + 1);
            if (obj != null && jsonCheck[i]) {
                globals.log(Level.INFO, "Column " + (i + 1) + " is a JSON column: " + obj.getClass());
                String str = null;
                // We only deal with textual form of JSON here.
                if (obj instanceof Clob) {
                    Clob clob = (Clob) obj;
                    str = clob.getSubString(1, (int) clob.length());
                    clob.free();
                } else {
                    str = obj.toString();
                }
                if (str != null) {
                    // Now convert str to JsonValue
                    try {
                        CookJsonParser p = new TextJsonParser(new StringReader(str));
                        p.next();
                        obj = p.getValue();
                        p.close();
                    } catch (Exception ex) {
                        globals.log(Level.INFO, ex);
                    }
                }
            }
            print(g, headers[i], obj);
        }
        g.writeEnd();
    }
    g.writeEnd();
    g.flush();
    return count;
}
Also used : TextJsonParser(org.yuanheng.cookjson.TextJsonParser) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) StringReader(java.io.StringReader) JsonString(javax.json.JsonString) CookJsonParser(org.yuanheng.cookjson.CookJsonParser)

Aggregations

StringReader (java.io.StringReader)2 CookJsonParser (org.yuanheng.cookjson.CookJsonParser)2 TextJsonParser (org.yuanheng.cookjson.TextJsonParser)2 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)1 JsonString (javax.json.JsonString)1 JsonValue (javax.json.JsonValue)1