JSON encoding fix.

This commit is contained in:
Nuno Cruces
2023-10-19 16:46:58 +01:00
parent a9dddaa86c
commit 4db18a7b9a
4 changed files with 15 additions and 8 deletions

View File

@@ -128,7 +128,7 @@ func (c Context) ResultTime(value time.Time, format TimeFormat) {
}
func (c Context) resultRFC3339Nano(value time.Time) {
const maxlen = uint64(len(time.RFC3339Nano))
const maxlen = uint64(len(time.RFC3339Nano)) + 5
ptr := c.new(maxlen)
buf := util.View(c.mod, ptr, maxlen)

View File

@@ -5,6 +5,8 @@ import (
"strconv"
"time"
"unsafe"
"github.com/ncruces/go-sqlite3/internal/util"
)
// JSON returns:
@@ -29,8 +31,7 @@ func (j jsonValue) UnmarshalJSON(data []byte) error {
}
func (j jsonValue) Scan(value any) error {
var mem [40]byte
buf := mem[:0]
var buf []byte
switch v := value.(type) {
case []byte:
@@ -47,6 +48,8 @@ func (j jsonValue) Scan(value any) error {
buf = append(buf, '"')
case nil:
buf = append(buf, "null"...)
default:
panic(util.AssertErr())
}
return j.UnmarshalJSON(buf)

View File

@@ -237,7 +237,7 @@ func (s *Stmt) BindTime(param int, value time.Time, format TimeFormat) error {
}
func (s *Stmt) bindRFC3339Nano(param int, value time.Time) error {
const maxlen = uint64(len(time.RFC3339Nano))
const maxlen = uint64(len(time.RFC3339Nano)) + 5
ptr := s.c.new(maxlen)
buf := util.View(s.c.mod, ptr, maxlen)
@@ -430,8 +430,10 @@ func (s *Stmt) ColumnJSON(col int, ptr any) error {
var data []byte
switch s.ColumnType(col) {
case NULL:
data = []byte("null")
case TEXT, BLOB:
data = append(data, "null"...)
case TEXT:
data = s.ColumnRawText(col)
case BLOB:
data = s.ColumnRawBlob(col)
case INTEGER:
data = strconv.AppendInt(nil, s.ColumnInt64(col), 10)

View File

@@ -132,8 +132,10 @@ func (v Value) JSON(ptr any) error {
var data []byte
switch v.Type() {
case NULL:
data = []byte("null")
case TEXT, BLOB:
data = append(data, "null"...)
case TEXT:
data = v.RawText()
case BLOB:
data = v.RawBlob()
case INTEGER:
data = strconv.AppendInt(nil, v.Int64(), 10)