JSONToWriter streams query results as pretty-printed JSON directly to the writer.
(w io.Writer, result *v1pb.QueryResult)
| 17 | |
| 18 | // JSONToWriter streams query results as pretty-printed JSON directly to the writer. |
| 19 | func JSONToWriter(w io.Writer, result *v1pb.QueryResult) error { |
| 20 | records := make([]map[string]any, 0, len(result.Rows)) |
| 21 | for _, row := range result.Rows { |
| 22 | record := make(map[string]any, len(result.ColumnNames)) |
| 23 | for i, value := range row.Values { |
| 24 | record[result.ColumnNames[i]] = convertValueToJSONValue(value) |
| 25 | } |
| 26 | records = append(records, record) |
| 27 | } |
| 28 | |
| 29 | jsonBytes, err := json.MarshalIndent(records, "", " ") |
| 30 | if err != nil { |
| 31 | return errors.Errorf("failed to encode JSON: %v", err) |
| 32 | } |
| 33 | if _, err := w.Write(jsonBytes); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | func convertValueToJSONValue(value *v1pb.RowValue) any { |
| 40 | if value == nil || value.Kind == nil { |
no test coverage detected