Search in sources :

Example 1 with DBDDocument

use of org.jkiss.dbeaver.model.data.DBDDocument in project dbeaver by serge-rider.

the class DataExporterJSON method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    if (rowNum > 0) {
        out.write(",\n");
    }
    rowNum++;
    if (isJsonDocumentResults(session.getProgressMonitor(), row)) {
        DBDDocument document = (DBDDocument) row[0];
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        document.serializeDocument(session.getProgressMonitor(), buffer, StandardCharsets.UTF_8);
        String jsonText = buffer.toString(StandardCharsets.UTF_8.name());
        out.write(jsonText);
    } else {
        out.write("\t{\n");
        for (int i = 0; i < columns.length; i++) {
            DBDAttributeBinding column = columns[i];
            String columnName = column.getLabel();
            if (CommonUtils.isEmpty(columnName)) {
                columnName = column.getName();
            }
            out.write("\t\t\"" + JSONUtils.escapeJsonString(columnName) + "\" : ");
            Object cellValue = row[column.getOrdinalPosition()];
            if (DBUtils.isNullValue(cellValue)) {
                writeTextCell(null);
            } else if (cellValue instanceof DBDContent) {
                // Content
                // Inline textual content and handle binaries in some special way
                DBDContent content = (DBDContent) cellValue;
                try {
                    DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                    if (cs != null) {
                        if (ContentUtils.isTextContent(content)) {
                            try (Reader in = cs.getContentReader()) {
                                out.write("\"");
                                writeCellValue(in);
                                out.write("\"");
                            }
                        } else {
                            getSite().writeBinaryData(cs);
                        }
                    }
                } finally {
                    content.release();
                }
            } else {
                if (cellValue instanceof Number || cellValue instanceof Boolean) {
                    out.write(cellValue.toString());
                } else if (cellValue instanceof Date && formatDateISO) {
                    writeTextCell(JSONUtils.formatDate((Date) cellValue));
                } else {
                    writeTextCell(super.getValueDisplayString(column, cellValue));
                }
            }
            if (i < columns.length - 1) {
                out.write(",");
            }
            out.write("\n");
        }
        out.write("\t}");
    }
}
Also used : Reader(java.io.Reader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date) DBDDocument(org.jkiss.dbeaver.model.data.DBDDocument) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) PrintWriter(java.io.PrintWriter)

Example 2 with DBDDocument

use of org.jkiss.dbeaver.model.data.DBDDocument in project dbeaver by dbeaver.

the class DataExporterJSON method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    if (rowNum > 0) {
        out.write(",\n");
    }
    rowNum++;
    if (isJsonDocumentResults(session.getProgressMonitor(), row)) {
        DBDDocument document = (DBDDocument) row[0];
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        document.serializeDocument(session.getProgressMonitor(), buffer, StandardCharsets.UTF_8);
        String jsonText = buffer.toString(StandardCharsets.UTF_8.name());
        out.write(jsonText);
    } else {
        out.write("\t{\n");
        for (int i = 0; i < columns.length; i++) {
            DBDAttributeBinding column = columns[i];
            String columnName = column.getLabel();
            if (CommonUtils.isEmpty(columnName)) {
                columnName = column.getName();
            }
            out.write("\t\t\"" + JSONUtils.escapeJsonString(columnName) + "\" : ");
            Object cellValue = row[column.getOrdinalPosition()];
            if (DBUtils.isNullValue(cellValue)) {
                writeTextCell(null);
            } else if (cellValue instanceof DBDContent) {
                // Content
                // Inline textual content and handle binaries in some special way
                DBDContent content = (DBDContent) cellValue;
                try {
                    DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                    if (cs != null) {
                        if (ContentUtils.isTextContent(content)) {
                            try (Reader in = cs.getContentReader()) {
                                out.write("\"");
                                writeCellValue(in);
                                out.write("\"");
                            }
                        } else {
                            getSite().writeBinaryData(cs);
                        }
                    }
                } finally {
                    content.release();
                }
            } else {
                if (cellValue instanceof Number || cellValue instanceof Boolean) {
                    out.write(cellValue.toString());
                } else if (cellValue instanceof Date && formatDateISO) {
                    writeTextCell(JSONUtils.formatDate((Date) cellValue));
                } else {
                    writeTextCell(super.getValueDisplayString(column, cellValue));
                }
            }
            if (i < columns.length - 1) {
                out.write(",");
            }
            out.write("\n");
        }
        out.write("\t}");
    }
}
Also used : Reader(java.io.Reader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date) DBDDocument(org.jkiss.dbeaver.model.data.DBDDocument) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintWriter (java.io.PrintWriter)2 Reader (java.io.Reader)2 Date (java.util.Date)2 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)2 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)2 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)2 DBDDocument (org.jkiss.dbeaver.model.data.DBDDocument)2