vertx-sql-client icon indicating copy to clipboard operation
vertx-sql-client copied to clipboard

Simple way convert row to JsonObject?

Open daihy8759 opened this issue 5 years ago • 7 comments

Dear Authors,

Is there has Simple way to convert JsonObject from row?

daihy8759 avatar Jul 09 '19 15:07 daihy8759

you mean a json object with row names as keys ?

vietj avatar Jul 09 '19 16:07 vietj

not yet but there will be some mapping to JsonObject / DataObject in Vert.x 4 at least

vietj avatar Jul 09 '19 16:07 vietj

yeah,The conversion method is a bit complicated now, some data types I have not done yet.

List<JsonObject> list = new ArrayList<>();
for (Row row : rows) {
    int size = row.size();
    JsonObject jsonObject = new JsonObject();
    for (int i = 0; i < size; i++) {
        String columnName = row.getColumnName(i);
        Object value = row.getValue(i);
        if(value != null){
            if(value instanceof LocalDateTime){
                jsonObject.put(columnName, ((LocalDateTime)value).atZone(
                        ZoneId.systemDefault()).toInstant());
            } else {
                jsonObject.put(columnName, row.getValue(i));
            }
        }
    }
    list.add(jsonObject);
}
return list;

daihy8759 avatar Jul 09 '19 22:07 daihy8759

In many cases, json serialization of the resultset/resultsets goes outside of the app server (e.g. to web client or service consumer) without any mangling.

IMHO, In these cases, it may be feasible JsonGenerator to be used for direct row serialization to the stream.

(Sorry if this is already aranged in vert.x, I not have learned the platform yet)

decalek avatar Jul 15 '19 17:07 decalek

yeah,The conversion method is a bit complicated now, some data types I have not done yet. ...

This is presently what I am doing as well:

fun RowSet<Row>.toJson(): List<JsonObject> = asSequence().map { row ->
    val json = JsonObject()
    columnsNames().asSequence().forEach {
        json.put(it, toPgType(row.getValue(it)))
    }
    json
}.toList()

private fun toPgType(value: Any?): Any? = when (value) {
    is String -> value
    is LocalDate -> value.format(DateTimeFormatter.ISO_DATE)
    is LocalDateTime -> value.format(DateTimeFormatter.ISO_DATE_TIME)
    else -> value
}

rgmz avatar Nov 11 '19 19:11 rgmz

in Vert.x 4 we will try to improve this so one can store a date directly in JSON Object

vietj avatar Nov 12 '19 10:11 vietj

yeah,The conversion method is a bit complicated now, some data types I have not done yet. ...

This is presently what I am doing as well:

fun RowSet<Row>.toJson(): List<JsonObject> = asSequence().map { row ->
    val json = JsonObject()
    columnsNames().asSequence().forEach {
        json.put(it, toPgType(row.getValue(it)))
    }
    json
}.toList()

private fun toPgType(value: Any?): Any? = when (value) {
    is String -> value
    is LocalDate -> value.format(DateTimeFormatter.ISO_DATE)
    is LocalDateTime -> value.format(DateTimeFormatter.ISO_DATE_TIME)
    else -> value
}

If you use postgres TIMESTAMP WITH TIMEZONE type, you also need this type mapping:

        is OffsetDateTime -> value.format(DateTimeFormatter.ISO_DATE_TIME)

tobiasmuehl avatar Nov 29 '19 08:11 tobiasmuehl

Outdated, tracked by #754 and solved in 4.0.0

tsegismont avatar Nov 14 '23 15:11 tsegismont