package jsontext
import (
"unsafe"
"github.com/goccy/go-json/internal/options"
"github.com/goccy/go-json/internal/textcoder"
)
func init() {
textcoder .Attach = attachEncoder
textcoder .Detach = detachEncoder
textcoder .Position = encoderPosition
textcoder .Place = encoderPlace
textcoder .Configure = configureEncoder
textcoder .Fail = func (enc any , err error ) {
if e := &enc .(*Encoder ).e ; e .attached && e .failErr == nil {
e .failErr = err
}
}
textcoder .Invalidate = func (enc any ) {
e := &enc .(*Encoder ).e
e .invalid = true
e .vs .st = nil
}
textcoder .UnmarshalPlace = unmarshalPlace
textcoder .TakeSkipper = takeSkipper
textcoder .ReleaseSkipper = releaseSkipper
textcoder .ValidValue = validValue
}
func encoderPosition(enc any , levels []textcoder .Level ) ([]textcoder .Level , int64 ) {
e := &enc .(*Encoder ).e
if !e .ready () {
e .setUp ()
}
st := &e .st
if st .outer != nil {
st = st .full ()
}
for i := range st .levels {
l := &st .levels [i ]
tl := textcoder .Level {Object : l .object , Count : l .count }
if i < len (st .levels )-1 {
tl .Count --
}
if l .object && l .last >= l .first {
if tl .Name = st .names .get (l .last ); tl .Name == nil {
tl .Name = []byte {}
}
}
levels = append (levels , tl )
}
_ , _ , offset , _ , _ := encoderPlace (enc )
return levels , offset
}
func encoderPlace(enc any ) (textcoder .Level , bool , int64 , textcoder .Outer , int ) {
e := &enc .(*Encoder ).e
if !e .ready () {
e .setUp ()
}
l := e .st .last ()
k := KindNull
if l .needName () {
k = KindString
}
inner := textcoder .Level {Object : l .object , Count : l .count }
return inner , e .st .depth () == 0 , e .base + int64 (len (e .buf )) + int64 (e .delimLen (k )), e .st .outer , e .st .depth () + e .st .base
}
func configureEncoder(enc any , opts options .Options ) func () {
e := &enc .(*Encoder ).e
saved := e .cfg
e .cfg = config {}
opts .ApplyTo (&e .cfg )
e .cfg .Value = e .cfg .Value &^(escapeForHTML |escapeForJS ) | preserveRawStrings
e .derive ()
return func () {
e .cfg = saved
e .derive ()
}
}
func attachEncoder(enc unsafe .Pointer , p *textcoder .Attachment ) {
e := &(*Encoder )(enc ).e
st := &e .st
e .closedPast , e .failErr = false , nil
if p .Same && e .attached && e .ready () && cap (st .levels ) >= 2 {
e .invalid = false
if p .Top {
st .levels = st .levels [:1 ]
st .levels [0 ] = level {count : p .Count }
} else {
st .levels = st .levels [:2 ]
st .levels [0 ] = level {count : 1 }
st .levels [1 ] = level {object : p .Object , count : p .Count , last : -1 }
}
if n := &st .names ; len (n .ends ) != 0 || len (n .indexes ) != 0 {
n .reset ()
}
vs := &e .vs
vs .write , vs .keep = true , false
vs .run = 0
vs .wsFrom , vs .wsEnd = 0 , 0
if vs .in != nil || vs .out != nil {
vs .in , vs .out = nil , nil
}
} else {
e .cfg = *p .Opts
e .w , e .attached = nil , false
e .init (nil )
e .attached = true
e .vs .ws , e .vs .spaced = whitespace {}, false
if !p .Top {
_ = st .push (p .Object )
}
st .last ().count = p .Count
st .outer = p .Outer
}
st .outerDepth , st .outerCount = len (st .levels )-1 , p .Count
st .base = p .OuterDepth - st .outerDepth
e .setOutput (p .Out , p .Base )
p .Skip , p .Depth = 0 , len (st .levels )-1
if len (p .Out ) == 0 && !p .Top {
p .Skip = e .skipDelim ()
}
}
func (e *encoder ) setOutput (out []byte , base int64 ) {
if unsafe .SliceData (e .buf ) == unsafe .SliceData (out ) && cap (e .buf ) == cap (out ) {
e .buf = e .buf [:len (out )]
} else {
e .buf = out
}
e .base , e .maxValue = base , 0
}
func (e *encoder ) skipDelim () int {
k := KindNull
if e .st .last ().needName () {
k = KindString
}
skip := e .delimLen (k )
e .base -= int64 (skip )
return skip
}
func detachEncoder(enc unsafe .Pointer , p *textcoder .Attachment ) {
e := &(*Encoder )(enc ).e
p .Out , p .Depth , p .Count , p .Failed = e .buf , e .st .depth (), e .st .last ().count , e .failErr
e .failErr = nil
if e .closedPast {
p .Depth --
}
}
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 .