Skip to content

Commit

Permalink
compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Mar 13, 2024
1 parent 69959f0 commit de1cbba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ast/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (self *Parser) skip() (int, types.ParsingError) {

func (self *Node) encodeInterface(buf *[]byte) error {
//WARN: NOT compatible with json.Encoder
return encoder.EncodeInto(buf, self.packAny(), 0)
return encoder.EncodeInto(buf, self.packAny(), encoder.NoEncoderNewline)
}

func (self *Parser) skipFast() (int, types.ParsingError) {
Expand Down
5 changes: 4 additions & 1 deletion ast/api_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
)

func TestSortNodeTwitter(t *testing.T) {
if encoder.EnableFallback {
return
}
root, err := NewSearcher(_TwitterJson).GetByPath()
if err != nil {
t.Fatal(err)
Expand All @@ -40,7 +43,7 @@ func TestSortNodeTwitter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
exp, err := encoder.Encode(obj, encoder.SortMapKeys)
exp, err := encoder.Encode(obj, encoder.SortMapKeys|encoder.NoEncoderNewline)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions encoder/encoder_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
`github.com/bytedance/sonic/internal/encoder`
)

// EnableFallback indicates if encoder use fallback
const EnableFallback = false

// Encoder represents a specific set of encoder configurations.
type Encoder = encoder.Encoder
Expand Down
27 changes: 17 additions & 10 deletions encoder/encoder_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ import (
)

func init() {
println("WARNING: sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
println("WARNING:(encoder) sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
}

// EnableFallback indicates if encoder use fallback
const EnableFallback = true

// Options is a set of encoding options.
type Options uint64

Expand Down Expand Up @@ -188,15 +191,19 @@ func Encode(val interface{}, opts Options) ([]byte, error) {
// EncodeInto is like Encode but uses a user-supplied buffer instead of allocating
// a new one.
func EncodeInto(buf *[]byte, val interface{}, opts Options) error {
if buf == nil {
panic("user-supplied buffer buf is nil")
}
w := bytes.NewBuffer(*buf)
enc := json.NewEncoder(w)
enc.SetEscapeHTML((opts & EscapeHTML) != 0)
err := enc.Encode(val)
*buf = w.Bytes()
return err
if buf == nil {
panic("user-supplied buffer buf is nil")
}
w := bytes.NewBuffer(*buf)
enc := json.NewEncoder(w)
enc.SetEscapeHTML((opts & EscapeHTML) != 0)
err := enc.Encode(val)
*buf = w.Bytes()
l := len(*buf)
if l > 0 && (opts & NoEncoderNewline != 0) && (*buf)[l-1] == '\n' {
*buf = (*buf)[:l-1]
}
return err
}

// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
Expand Down

0 comments on commit de1cbba

Please sign in to comment.