//go:build !goexperiment.jsonv2package sqlite3import ()// JSON returns a value that can be used as an argument to// [database/sql.DB.Exec], [database/sql.Row.Scan] and similar methods to// store value as JSON, or decode JSON into value.// JSON should NOT be used with [Stmt.BindJSON], [Stmt.ColumnJSON],// [Value.JSON], or [Context.ResultJSON].func ( any) any {returnutil.JSON{Value: }}// ResultJSON sets the result of the function to the JSON encoding of value.//// If you return JSON from application-defined functions,// consider registering those functions with [RESULT_SUBTYPE]// and calling [Context.ResultSubtype] with 'J'.//// https://sqlite.org/c3ref/result_blob.htmlfunc ( Context) ( any) { := json.NewEncoder(callbackWriter(func( []byte) (int, error) { .ResultRawText([:len()-1]) // remove the newlinereturn0, nil })).Encode()if != nil { .ResultError()return// notest }}// BindJSON binds the JSON encoding of value to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, any) error {returnjson.NewEncoder(callbackWriter(func( []byte) (int, error) {return0, .BindRawText(, [:len()-1]) // remove the newline })).Encode()}// ColumnJSON parses the JSON-encoded value of the result column// and stores it in the value pointed to by ptr.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int, any) error {var []byteswitch .ColumnType() {caseNULL: = []byte("null")caseTEXT: = .ColumnRawText()caseBLOB: = .ColumnRawBlob()caseINTEGER: = strconv.AppendInt(nil, .ColumnInt64(), 10)caseFLOAT: = util.AppendNumber(nil, .ColumnFloat())default:panic(errutil.AssertErr()) }returnjson.Unmarshal(, )}// JSON parses a JSON-encoded value// and stores the result in the value pointed to by ptr.func ( Value) ( any) error {var []byteswitch .Type() {caseNULL: = []byte("null")caseTEXT: = .RawText()caseBLOB: = .RawBlob()caseINTEGER: = strconv.AppendInt(nil, .Int64(), 10)caseFLOAT: = util.AppendNumber(nil, .Float())default:panic(errutil.AssertErr()) }returnjson.Unmarshal(, )}type callbackWriter func(p []byte) (int, error)func ( callbackWriter) ( []byte) (int, error) { return () }
The pages are generated with Goldsv0.8.4. (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.