mirror of
https://github.com/minio/minio.git
synced 2025-11-10 05:59:43 -05:00
Use jstream to serialize records to JSON format in S3Select (#7318)
- Also, switch to jstream to generate internal record representation from CSV/JSON readers - This fixes a bug in which JSON output objects have their keys reversed from the order they are specified in the Select columns. - Also includes a fix for tests.
This commit is contained in:
committed by
Harshavardhana
parent
f97a33a63f
commit
f4879ed96d
@@ -19,10 +19,11 @@ package csv
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/bcicen/jstream"
|
||||
"github.com/minio/minio/pkg/s3select/sql"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// Record - is CSV record.
|
||||
@@ -78,20 +79,11 @@ func (r *Record) MarshalCSV(fieldDelimiter rune) ([]byte, error) {
|
||||
|
||||
// MarshalJSON - encodes to JSON data.
|
||||
func (r *Record) MarshalJSON() ([]byte, error) {
|
||||
data := "{}"
|
||||
|
||||
var err error
|
||||
for i := len(r.columnNames) - 1; i >= 0; i-- {
|
||||
if i >= len(r.csvRecord) {
|
||||
continue
|
||||
}
|
||||
|
||||
if data, err = sjson.Set(data, r.columnNames[i], r.csvRecord[i]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var kvs jstream.KVS = make([]jstream.KV, len(r.columnNames))
|
||||
for i := 0; i < len(r.columnNames); i++ {
|
||||
kvs[i] = jstream.KV{Key: r.columnNames[i], Value: r.csvRecord[i]}
|
||||
}
|
||||
|
||||
return []byte(data), nil
|
||||
return json.Marshal(kvs)
|
||||
}
|
||||
|
||||
// NewRecord - creates new CSV record.
|
||||
|
||||
Reference in New Issue
Block a user