简体   繁体   中英

How to detect JSON column type from Database

Have some code that transforms a database ResultSet into JSON. Right now, it handles numeric values and Strings, and works just fine and dandy. However, when it gets a JSON value, it uses toString on it, instead of treating it as JSON, which means when extracted, it has escaped double quotes, etc., and cannot be re-serialized into JSON.

How can I detect a JSON column type from a PostgreSQL database?

EDIT: Here's the solution, as suggested by @f1sh. @f1sh, if you want to submit your comment as an answer, I'll mark it as the accepted solution.

private JsonNode resultSetToJson(ResultSet rs) throws SQLException {

    ObjectNode node = DataFileTool.OBJECT_MAPPER.createObjectNode();

    ResultSetMetaData mData = rs.getMetaData();
    int columnCount = mData.getColumnCount();

    for (int i = 1; i <= columnCount; i++) {

        Object obj = rs.getObject(i);
        String columnName = mData.getColumnLabel(i);
        String columnNameToUse = dbColumnNameToJsonKey.containsKey(columnName)
                ? dbColumnNameToJsonKey.get(columnName) : columnName;

        if (mData.getColumnType(i) == Types.NUMERIC) {
            if (obj == null) {
                node.putNull(columnNameToUse);
            } else if (obj instanceof BigDecimal) {
                node.put(columnNameToUse, (BigDecimal) obj);
            } else if (obj instanceof Integer) {
                node.put(columnNameToUse, (Integer) obj);
            } else if (obj instanceof Double) {
                node.put(columnNameToUse, (Double) obj);
            } else if (obj instanceof Long) {
                node.put(columnNameToUse, (Long) obj);
            } else if (obj instanceof Float) {
                node.put(columnNameToUse, (Float) obj);
            } else if (obj instanceof Short) {
                node.put(columnNameToUse, (Short) obj);
            } else if (obj instanceof BigInteger) {
                node.set(columnNameToUse, BigIntegerNode.valueOf((BigInteger) obj));
            } else if (obj instanceof Byte) {
                node.put(columnNameToUse, (Byte) obj);
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj instanceof PGobject) {
            PGobject pg = (PGobject) obj;

            if (pg.getType().equalsIgnoreCase("json")) {
                JsonNode pgNode;

                try {
                    pgNode = DataFileTool.OBJECT_MAPPER.readTree(pg.getValue());
                    node.set(columnNameToUse, pgNode);
                } catch (IOException e) {
                    LOGGER.error("An exception occurred while attempting to parse JSON PGObject to JsonNode: {}",
                            obj, e);
                    node.put(columnNameToUse, obj.toString());
                }
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj == null) {
            node.put(columnNameToUse, StringUtils.EMPTY);
        } else {
            node.put(columnNameToUse, obj.toString());
        }
    }

    return node;
}
if (obj instanceof PGobject) {
        PGobject pg = (PGobject) obj;

        if (pg.getType().equalsIgnoreCase("json")) {
            JsonNode pgNode;

            try {
                pgNode = DataFileTool.OBJECT_MAPPER.readTree(pg.getValue());
                node.set(columnNameToUse, pgNode);
            } catch (IOException e) {
                LOGGER.error("An exception occurred while attempting to parse JSON PGObject to JsonNode: {}",
                        obj, e);
                node.put(columnNameToUse, obj.toString());
            }
        } else {
            node.put(columnNameToUse, obj.toString());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM