package run
import (
"runtime"
"unsafe"
"github.com/goccy/go-json/internal/encoder"
"github.com/goccy/go-json/internal/encoder/vm"
"github.com/goccy/go-json/internal/encoder/vm_color"
"github.com/goccy/go-json/internal/encoder/vm_color_indent"
"github.com/goccy/go-json/internal/encoder/vm_indent"
"github.com/goccy/go-json/internal/jsonstring"
)
func init() {
encoder .RunHook = Code
}
type emptyInterface struct {
typ unsafe .Pointer
ptr unsafe .Pointer
}
func Encode (ctx *encoder .RuntimeContext , v any ) ([]byte , error ) {
b := ctx .Buf [:0 ]
if v == nil {
b = encoder .AppendNull (ctx , b )
b = encoder .AppendComma (ctx , b )
return b , nil
}
header := (*emptyInterface )(unsafe .Pointer (&v ))
typ := header .typ
typeptr := uintptr (typ )
codeSet , err := encoder .CompileToGetCodeSet (ctx , typeptr )
if err != nil {
return b , err
}
if ctx .Option .Flag &encoder .MarshalFuncsOption != 0 && header .ptr != nil {
if pointee := encoder .PointeeOf (ctx , codeSet ); pointee != nil {
codeSet = pointee
}
}
p := ctx .ValueAddr (codeSet , header .ptr )
if p == nil {
b = encoder .AppendNull (ctx , b )
b = encoder .AppendComma (ctx , b )
return b , nil
}
ctx .Init (p , codeSet .CodeLength )
if ctx .Outer != nil {
ctx .BaseIndent = ctx .ValueDepth
if err := encoder .EnterNested (ctx , ctx .Outer , codeSet , p ); err != nil {
return b , err
}
}
buf , err := Code (ctx , b , codeSet )
runtime .KeepAlive (v )
if err != nil {
return buf , err
}
ctx .Buf = buf
return buf , nil
}
func EncodeIndent (ctx *encoder .RuntimeContext , v any , prefix , indent string ) ([]byte , error ) {
b := ctx .Buf [:0 ]
if v == nil {
b = encoder .AppendNull (ctx , b )
b = encoder .AppendCommaIndent (ctx , b )
return b , nil
}
header := (*emptyInterface )(unsafe .Pointer (&v ))
typ := header .typ
typeptr := uintptr (typ )
codeSet , err := encoder .CompileToGetCodeSet (ctx , typeptr )
if err != nil {
return b , err
}
p := ctx .ValueAddr (codeSet , header .ptr )
if p == nil {
b = encoder .AppendNull (ctx , b )
b = encoder .AppendCommaIndent (ctx , b )
return b , nil
}
ctx .Init (p , codeSet .CodeLength )
ctx .Option .Flag |= encoder .IndentOption
buf , err := IndentCode (ctx , b , codeSet , prefix , indent )
runtime .KeepAlive (v )
if err != nil {
return buf , err
}
ctx .Buf = buf
return buf , nil
}
func Code (ctx *encoder .RuntimeContext , b []byte , codeSet *encoder .OpcodeSet ) ([]byte , error ) {
if ctx .Option .Flag &encoder .IndentOption != 0 {
return indentRun (ctx , b , codeSet )
}
if flags := ctx .Option .Flag ; flags != ctx .Option .EscaperFlags {
if flags &encoder .RejectInvalidUTF8Option != 0 {
ctx .SetStrictEscaper ()
} else {
ctx .Option .Escaper , ctx .Option .EscaperFlags = jsonstring .EscaperOf (uint (flags )), flags
}
}
if (ctx .Option .Flag & encoder .DebugOption ) != 0 {
if (ctx .Option .Flag & encoder .ColorizeOption ) != 0 {
return vm_color .DebugRun (ctx , b , codeSet )
}
return vm .DebugRun (ctx , b , codeSet )
}
if (ctx .Option .Flag & encoder .ColorizeOption ) != 0 {
return vm_color .Run (ctx , b , codeSet )
}
return vm .Run (ctx , b , codeSet )
}
func IndentCode (ctx *encoder .RuntimeContext , b []byte , codeSet *encoder .OpcodeSet , prefix , indent string ) ([]byte , error ) {
ctx .Prefix = []byte (prefix )
ctx .IndentStr = []byte (indent )
return indentRun (ctx , b , codeSet )
}
func indentRun(ctx *encoder .RuntimeContext , b []byte , codeSet *encoder .OpcodeSet ) ([]byte , error ) {
ctx .SetEscaper ()
if (ctx .Option .Flag & encoder .DebugOption ) != 0 {
if (ctx .Option .Flag & encoder .ColorizeOption ) != 0 {
return vm_color_indent .DebugRun (ctx , b , codeSet )
}
return vm_indent .DebugRun (ctx , b , codeSet )
}
if (ctx .Option .Flag & encoder .ColorizeOption ) != 0 {
return vm_color_indent .Run (ctx , b , codeSet )
}
return vm_indent .Run (ctx , b , codeSet )
}
The pages are generated with Golds v0.8.7 . (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 .