//go:build !goexperiment.jsonv2

package sqlite3

import (
	
	

	
)

// 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 {
	return util.JSON{Value: }
}

// ResultJSON sets the result of the function to the JSON encoding of value.
//
// https://sqlite.org/c3ref/result_blob.html
func ( Context) ( any) {
	 := json.NewEncoder(callbackWriter(func( []byte) (int, error) {
		.ResultRawText([:len()-1]) // remove the newline
		return 0, 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.html
func ( *Stmt) ( int,  any) error {
	return json.NewEncoder(callbackWriter(func( []byte) (int, error) {
		return 0, .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.html
func ( *Stmt) ( int,  any) error {
	var  []byte
	switch .ColumnType() {
	case NULL:
		 = []byte("null")
	case TEXT:
		 = .ColumnRawText()
	case BLOB:
		 = .ColumnRawBlob()
	case INTEGER:
		 = strconv.AppendInt(nil, .ColumnInt64(), 10)
	case FLOAT:
		 = util.AppendNumber(nil, .ColumnFloat())
	default:
		panic(util.AssertErr())
	}
	return json.Unmarshal(, )
}

// JSON parses a JSON-encoded value
// and stores the result in the value pointed to by ptr.
func ( Value) ( any) error {
	var  []byte
	switch .Type() {
	case NULL:
		 = []byte("null")
	case TEXT:
		 = .RawText()
	case BLOB:
		 = .RawBlob()
	case INTEGER:
		 = strconv.AppendInt(nil, .Int64(), 10)
	case FLOAT:
		 = util.AppendNumber(nil, .Float())
	default:
		panic(util.AssertErr())
	}
	return json.Unmarshal(, )
}

type callbackWriter func(p []byte) (int, error)

func ( callbackWriter) ( []byte) (int, error) { return () }