diff --git a/context.go b/context.go index abee4ec..7caf586 100644 --- a/context.go +++ b/context.go @@ -177,12 +177,15 @@ func (ctx Context) ResultPointer(ptr any) { // // https://sqlite.org/c3ref/result_blob.html func (ctx Context) ResultJSON(value any) { - data, err := json.Marshal(value) + err := json.NewEncoder(callbackWriter(func(p []byte) (int, error) { + ctx.ResultRawText(p[:len(p)-1]) // remove the newline + return 0, nil + })).Encode(value) + if err != nil { ctx.ResultError(err) return // notest } - ctx.ResultRawText(data) } // ResultValue sets the result of the function to a copy of [Value]. diff --git a/stmt.go b/stmt.go index 7fa2a50..363b45a 100644 --- a/stmt.go +++ b/stmt.go @@ -367,11 +367,9 @@ func (s *Stmt) BindPointer(param int, ptr any) error { // // https://sqlite.org/c3ref/bind_blob.html func (s *Stmt) BindJSON(param int, value any) error { - data, err := json.Marshal(value) - if err != nil { - return err - } - return s.BindRawText(param, data) + return json.NewEncoder(callbackWriter(func(p []byte) (int, error) { + return 0, s.BindRawText(param, p[:len(p)-1]) // remove the newline + })).Encode(value) } // BindValue binds a copy of value to the prepared statement. @@ -751,3 +749,7 @@ func (s *Stmt) columns(count int64) ([]byte, ptr_t, error) { return util.View(s.c.mod, typePtr, count), dataPtr, nil } + +type callbackWriter func(p []byte) (int, error) + +func (fn callbackWriter) Write(p []byte) (int, error) { return fn(p) }