@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -269,6 +270,15 @@ func (r queryResult) MarshalJSON() ([]byte, error) {
|
||||
if value.Rows == nil {
|
||||
value.Rows = [][]any{}
|
||||
}
|
||||
data, err := json.Marshal(value)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
rows, changed := normalizeNonFiniteQueryRows(value.Rows)
|
||||
if !changed {
|
||||
return nil, err
|
||||
}
|
||||
value.Rows = rows
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
@@ -295,9 +305,44 @@ func (r queryPageResult) MarshalJSON() ([]byte, error) {
|
||||
if value.Rows == nil {
|
||||
value.Rows = [][]any{}
|
||||
}
|
||||
data, err := json.Marshal(value)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
rows, changed := normalizeNonFiniteQueryRows(value.Rows)
|
||||
if !changed {
|
||||
return nil, err
|
||||
}
|
||||
value.Rows = rows
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func normalizeNonFiniteQueryRows(rows [][]any) ([][]any, bool) {
|
||||
result := rows
|
||||
changed := false
|
||||
for rowIndex, row := range rows {
|
||||
var normalizedRow []any
|
||||
for columnIndex, value := range row {
|
||||
floatValue, ok := value.(float64)
|
||||
if !ok || (!math.IsNaN(floatValue) && !math.IsInf(floatValue, 0)) {
|
||||
continue
|
||||
}
|
||||
if normalizedRow == nil {
|
||||
normalizedRow = append([]any(nil), row...)
|
||||
}
|
||||
normalizedRow[columnIndex] = fmt.Sprint(floatValue)
|
||||
}
|
||||
if normalizedRow != nil {
|
||||
if !changed {
|
||||
result = append([][]any(nil), rows...)
|
||||
changed = true
|
||||
}
|
||||
result[rowIndex] = normalizedRow
|
||||
}
|
||||
}
|
||||
return result, changed
|
||||
}
|
||||
|
||||
type querySession struct {
|
||||
rows *sql.Rows
|
||||
columns []string
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
@@ -245,6 +246,28 @@ func TestNormalizeValueKeepsNonBinaryBytesAsText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryResultsMarshalNonFiniteFloatsAsStrings(t *testing.T) {
|
||||
result := queryResult{Rows: [][]any{{math.NaN(), math.Inf(1), math.Inf(-1), 1234.56}}}
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("non-finite Oracle floats must remain JSON-safe: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), `[["NaN","+Inf","-Inf",1234.56]]`) {
|
||||
t.Fatalf("unexpected query result JSON: %s", data)
|
||||
}
|
||||
if !math.IsNaN(result.Rows[0][0].(float64)) {
|
||||
t.Fatalf("marshaling must not mutate the original rows: %#v", result.Rows)
|
||||
}
|
||||
|
||||
data, err = json.Marshal(queryPageResult{Rows: [][]any{{math.NaN()}}})
|
||||
if err != nil {
|
||||
t.Fatalf("paged non-finite Oracle floats must remain JSON-safe: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), `[["NaN"]]`) {
|
||||
t.Fatalf("unexpected query page JSON: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeValueFormatsOracleTimezoneLessDateTimesAsWallClock(t *testing.T) {
|
||||
value := time.Date(2026, time.July, 23, 13, 42, 13, 123456000, time.FixedZone("CST", 8*60*60))
|
||||
tests := []string{
|
||||
|
||||
Reference in New Issue
Block a user