github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/internal/util/json.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  	"unsafe"
     8  )
     9  
    10  type JSON struct{ Value any }
    11  
    12  func (j JSON) Scan(value any) error {
    13  	var buf []byte
    14  
    15  	switch v := value.(type) {
    16  	case []byte:
    17  		buf = v
    18  	case string:
    19  		buf = unsafe.Slice(unsafe.StringData(v), len(v))
    20  	case int64:
    21  		buf = strconv.AppendInt(nil, v, 10)
    22  	case float64:
    23  		buf = strconv.AppendFloat(nil, v, 'g', -1, 64)
    24  	case time.Time:
    25  		buf = append(buf, '"')
    26  		buf = v.AppendFormat(buf, time.RFC3339Nano)
    27  		buf = append(buf, '"')
    28  	case nil:
    29  		buf = append(buf, "null"...)
    30  	default:
    31  		panic(AssertErr())
    32  	}
    33  
    34  	return json.Unmarshal(buf, j.Value)
    35  }