package sqlite3
import (
"encoding/json"
"strconv"
"github.com/ncruces/go-sqlite3/internal/util"
)
func JSON (value any ) any {
return util .JSON {Value : value }
}
func (ctx Context ) ResultJSON (value any ) {
err := json .NewEncoder (callbackWriter (func (p []byte ) (int , error ) {
ctx .ResultRawText (p [:len (p )-1 ])
return 0 , nil
})).Encode (value )
if err != nil {
ctx .ResultError (err )
return
}
}
func (s *Stmt ) BindJSON (param int , value any ) error {
return json .NewEncoder (callbackWriter (func (p []byte ) (int , error ) {
return 0 , s .BindRawText (param , p [:len (p )-1 ])
})).Encode (value )
}
func (s *Stmt ) ColumnJSON (col int , ptr any ) error {
var data []byte
switch s .ColumnType (col ) {
case NULL :
data = []byte ("null" )
case TEXT :
data = s .ColumnRawText (col )
case BLOB :
data = s .ColumnRawBlob (col )
case INTEGER :
data = strconv .AppendInt (nil , s .ColumnInt64 (col ), 10 )
case FLOAT :
data = util .AppendNumber (nil , s .ColumnFloat (col ))
default :
panic (util .AssertErr ())
}
return json .Unmarshal (data , ptr )
}
func (v Value ) JSON (ptr any ) error {
var data []byte
switch v .Type () {
case NULL :
data = []byte ("null" )
case TEXT :
data = v .RawText ()
case BLOB :
data = v .RawBlob ()
case INTEGER :
data = strconv .AppendInt (nil , v .Int64 (), 10 )
case FLOAT :
data = util .AppendNumber (nil , v .Float ())
default :
panic (util .AssertErr ())
}
return json .Unmarshal (data , ptr )
}
type callbackWriter func (p []byte ) (int , error )
func (fn callbackWriter ) Write (p []byte ) (int , error ) { return fn (p ) }
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .