Return rows as JSON

This commit is contained in:
Laiteux
2025-11-01 20:32:01 +04:00
parent 90360d2fdf
commit c20c4a5395

View File

@@ -10,6 +10,8 @@ import java.util.LinkedHashMap;
import java.util.Set; import java.util.Set;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList;
import java.util.Base64;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.time.Instant; import java.time.Instant;
@@ -169,7 +171,11 @@ public class LpaBridgeProvider extends ContentProvider
} }
} }
return projectColumns(rows, projection, new String[] { "error" }); rows = projectColumns(rows, projection, new String[] { "error" });
String rowsJson = rowsToJson(rows);
return row("rows", rowsJson);
} }
// region Mandatory Overrides // region Mandatory Overrides
@@ -719,62 +725,61 @@ public class LpaBridgeProvider extends ContentProvider
private static MatrixCursor projectColumns(MatrixCursor rows, String[] projection, String[] preserve) private static MatrixCursor projectColumns(MatrixCursor rows, String[] projection, String[] preserve)
{ {
String[] rowCols = rows.getColumnNames(); var rowCols = new LinkedHashSet<String>(Arrays.asList(rows.getColumnNames()));
Set<String> available = new LinkedHashSet<>(Arrays.asList(rowCols)); var outCols = new LinkedHashSet<String>();
var cols = new LinkedHashSet<String>();
if (projection != null && projection.length > 0) if (projection != null && projection.length > 0)
cols.addAll(Arrays.asList(projection)); outCols.addAll(Arrays.asList(projection));
else else
cols.addAll(available); outCols.addAll(rowCols);
if (preserve != null && preserve.length > 0) if (preserve != null && preserve.length > 0)
{ {
for (String col : preserve) Stream.of(preserve)
{ .filter(rowCols::contains)
Stream.of(preserve) .forEach(outCols::add);
.filter(available::contains)
.forEach(cols::add);
}
} }
if (cols.isEmpty()) if (outCols.isEmpty())
return rows; return rows;
var outCols = cols.toArray(new String[0]); var outColsArray = outCols.toArray(new String[0]);
var outRows = new MatrixCursor(outCols); var outRows = new MatrixCursor(outColsArray);
rows.moveToPosition(-1);
while (rows.moveToNext()) while (rows.moveToNext())
{ {
var row = new Object[outCols.length]; var row = new Object[outColsArray.length];
for (int i = 0; i < outCols.length; i++) for (int rowIndex = 0; rowIndex < outColsArray.length; rowIndex++)
{ {
int index = rows.getColumnIndex(outCols[i]); String colName = outColsArray[rowIndex];
int colIndex = rows.getColumnIndex(colName);
if (index < 0) if (colIndex < 0)
{ {
row[i] = null; row[rowIndex] = null;
continue; continue;
} }
switch (rows.getType(index)) switch (rows.getType(colIndex))
{ {
case Cursor.FIELD_TYPE_NULL: case Cursor.FIELD_TYPE_NULL:
row[i] = null; row[rowIndex] = null;
break; break;
case Cursor.FIELD_TYPE_INTEGER: case Cursor.FIELD_TYPE_INTEGER:
row[i] = rows.getLong(index); row[rowIndex] = rows.getLong(colIndex);
break; break;
case Cursor.FIELD_TYPE_FLOAT: case Cursor.FIELD_TYPE_FLOAT:
row[i] = rows.getDouble(index); row[rowIndex] = rows.getDouble(colIndex);
break; break;
case Cursor.FIELD_TYPE_BLOB: case Cursor.FIELD_TYPE_BLOB:
row[i] = rows.getBlob(index); row[rowIndex] = rows.getBlob(colIndex);
break; break;
case Cursor.FIELD_TYPE_STRING: case Cursor.FIELD_TYPE_STRING:
default: default:
row[i] = rows.getString(index); row[rowIndex] = rows.getString(colIndex);
break; break;
} }
} }
@@ -785,6 +790,55 @@ public class LpaBridgeProvider extends ContentProvider
return outRows; return outRows;
} }
private static String rowsToJson(MatrixCursor rows)
{
String[] rowCols = rows.getColumnNames();
var outRows = new ArrayList<Map<String, Object>>();
rows.moveToPosition(-1);
while (rows.moveToNext())
{
var row = new LinkedHashMap<String, Object>();
for (String colName : rowCols)
{
int colIndex = rows.getColumnIndex(colName);
switch (rows.getType(colIndex))
{
case Cursor.FIELD_TYPE_NULL:
row.put(colName, null);
break;
case Cursor.FIELD_TYPE_INTEGER:
row.put(colName, rows.getLong(colIndex));
break;
case Cursor.FIELD_TYPE_FLOAT:
row.put(colName, rows.getDouble(colIndex));
break;
case Cursor.FIELD_TYPE_BLOB:
byte[] blob = rows.getBlob(colIndex);
String blobBase64 = Base64.getEncoder().encodeToString(blob);
row.put(colName, blobBase64);
break;
case Cursor.FIELD_TYPE_STRING:
default:
row.put(colName, rows.getString(colIndex));
break;
}
}
outRows.add(row);
}
String json = new GsonBuilder()
.serializeNulls()
.create()
.toJson(outRows);
return json;
}
// endregion // endregion
// region HTTP Helpers // region HTTP Helpers