Skip to content

Commit

Permalink
Merge branch 'main' into fix_compile_gox86_windows
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 authored Sep 12, 2023
2 parents 7b004e0 + 9cc03f3 commit 1d64627
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 30 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/license-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ jobs:
uses: apache/skywalking-eyes/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check Branch
run: ./scripts/check_branch_name.sh ${{ github.head_ref }}
7 changes: 6 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ type Config struct {

// ValidateString indicates decoder and encoder to valid string values: decoder will return errors
// when unescaped control chars(\u0000-\u001f) in the string value of JSON.
ValidateString bool
ValidateString bool

// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler bool
}

var (
Expand All @@ -87,6 +91,7 @@ var (
// ConfigFastest is the fastest config of APIs, aiming at speed.
ConfigFastest = Config{
NoQuoteTextMarshaler: true,
NoValidateJSONMarshaler: true,
}.Froze()
)

Expand Down
30 changes: 30 additions & 0 deletions ast/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,36 @@ func TestEncodeNode(t *testing.T) {
}
}

type SortableNode struct {
sorted bool
*Node
}

func (j *SortableNode) UnmarshalJSON(data []byte) (error) {
j.Node = new(Node)
return j.Node.UnmarshalJSON(data)
}

func (j *SortableNode) MarshalJSON() ([]byte, error) {
if !j.sorted {
j.Node.SortKeys(true)
j.sorted = true
}
return j.Node.MarshalJSON()
}

func TestMarshalSort(t *testing.T) {
var data = `{"d":3,"a":{"c":1,"b":2},"e":null}`
var obj map[string]*SortableNode
require.NoError(t, json.Unmarshal([]byte(data), &obj))
out, err := json.Marshal(obj)
require.NoError(t, err)
require.Equal(t, `{"a":{"b":2,"c":1},"d":3,"e":null}`, string(out))
out, err = json.Marshal(obj)
require.NoError(t, err)
require.Equal(t, `{"a":{"b":2,"c":1},"d":3,"e":null}`, string(out))
}

func BenchmarkEncodeRaw_Sonic(b *testing.B) {
data := _TwitterJson
root, e := NewSearcher(data).GetByPath()
Expand Down
10 changes: 6 additions & 4 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type Node struct {
// UnmarshalJSON is just an adapter to json.Unmarshaler.
// If you want better performance, use Searcher.GetByPath() directly
func (self *Node) UnmarshalJSON(data []byte) (err error) {
*self, err = NewSearcher(string(data)).GetByPath()
return
*self = NewRaw(string(data))
return self.Check()
}

/** Node Type Accessor **/
Expand Down Expand Up @@ -844,6 +844,7 @@ func (self *Node) MapUseNode() (map[string]Node, error) {
// return self.toGenericObjectUsePair()
// }

//go:nocheckptr
func (self *Node) unsafeMap() (*linkedPairs, error) {
if err := self.skipAllKey(); err != nil {
return nil, err
Expand All @@ -857,15 +858,16 @@ func (self *Node) unsafeMap() (*linkedPairs, error) {
// SortKeys sorts children of a V_OBJECT node in ascending key-order.
// If recurse is true, it recursively sorts children's children as long as a V_OBJECT node is found.
func (self *Node) SortKeys(recurse bool) error {
if err := self.Check(); err != nil {
// check raw node first
if err := self.checkRaw(); err != nil {
return err
}
if self.itype() == types.V_OBJECT {
return self.sortKeys(recurse)
} else {
var err error
err2 := self.ForEach(func(path Sequence, node *Node) bool {
it := self.itype()
it := node.itype()
if it == types.V_ARRAY || it == types.V_OBJECT {
err = node.SortKeys(recurse)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions encoder/encoder_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const (
// before encoding it into JSON.
ValidateString Options = encoder.ValidateString

// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = encoder.NoValidateJSONMarshaler

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = encoder.CompatibleWithStd
)
Expand Down
14 changes: 14 additions & 0 deletions encoder/encoder_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
bitNoQuoteTextMarshaler
bitNoNullSliceOrMap
bitValidateString
bitNoValidateJSONMarshaler

// used for recursive compile
bitPointerValue = 63
Expand Down Expand Up @@ -72,6 +73,10 @@ const (
// ValidateString indicates that encoder should validate the input string
// before encoding it into JSON.
ValidateString Options = 1 << bitValidateString

// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
Expand Down Expand Up @@ -116,6 +121,15 @@ func (self *Encoder) SetValidateString(f bool) {
}
}

// SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens
func (self *Encoder) SetNoValidateJSONMarshaler(f bool) {
if f {
self.Opts |= NoValidateJSONMarshaler
} else {
self.Opts &= ^NoValidateJSONMarshaler
}
}

// SetCompactMarshaler specifies if option CompactMarshaler opens
func (self *Encoder) SetCompactMarshaler(f bool) {
if f {
Expand Down
1 change: 0 additions & 1 deletion internal/decoder/asm_stubs_amd64_go121.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (self *_Assembler) WriteRecNotAX(i int, ptr obj.Addr, rec obj.Addr, saveDI
self.Emit("MOVQ", ptr, jit.Ptr(_R11, 0))
self.Emit("MOVQ", rec, _AX)
self.Emit("MOVQ", _AX, jit.Ptr(_R11, 8))
self.load(_R11)
if saveAX {
self.load(_AX, _R11)
} else {
Expand Down
14 changes: 14 additions & 0 deletions internal/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
bitNoQuoteTextMarshaler
bitNoNullSliceOrMap
bitValidateString
bitNoValidateJSONMarshaler

// used for recursive compile
bitPointerValue = 63
Expand Down Expand Up @@ -71,6 +72,10 @@ const (
// ValidateString indicates that encoder should validate the input string
// before encoding it into JSON.
ValidateString Options = 1 << bitValidateString

// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
Expand Down Expand Up @@ -115,6 +120,15 @@ func (self *Encoder) SetValidateString(f bool) {
}
}

// SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens
func (self *Encoder) SetNoValidateJSONMarshaler(f bool) {
if f {
self.Opts |= NoValidateJSONMarshaler
} else {
self.Opts &= ^NoValidateJSONMarshaler
}
}

// SetCompactMarshaler specifies if option CompactMarshaler opens
func (self *Encoder) SetCompactMarshaler(f bool) {
if f {
Expand Down
6 changes: 4 additions & 2 deletions internal/encoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ func encodeJsonMarshaler(buf *[]byte, val json.Marshaler, opt Options) error {
if opt & CompactMarshaler != 0 {
return compact(buf, ret)
}
if ok, s := Valid(ret); !ok {
return error_marshaler(ret, s)
if opt & NoValidateJSONMarshaler == 0 {
if ok, s := Valid(ret); !ok {
return error_marshaler(ret, s)
}
}
*buf = append(*buf, ret...)
return nil
Expand Down
41 changes: 22 additions & 19 deletions sonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (cfg Config) Froze() API {
if cfg.ValidateString {
api.encoderOpts |= encoder.ValidateString
}
if cfg.NoValidateJSONMarshaler {
api.encoderOpts |= encoder.NoValidateJSONMarshaler
}

// configure decoder options:
if cfg.UseInt64 {
Expand Down Expand Up @@ -139,23 +142,23 @@ func (cfg frozenConfig) Valid(data []byte) bool {
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
// a compile option to set the depth of recursive compile for the nested struct type.
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
if err := encoder.Pretouch(vt, opts...); err != nil {
return err
}
if err := decoder.Pretouch(vt, opts...); err != nil {
return err
}
// to pretouch the corresponding pointer type as well
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
} else {
vt = reflect.PtrTo(vt)
}
if err := encoder.Pretouch(vt, opts...); err != nil {
return err
}
if err := decoder.Pretouch(vt, opts...); err != nil {
return err
}
return nil
if err := encoder.Pretouch(vt, opts...); err != nil {
return err
}
if err := decoder.Pretouch(vt, opts...); err != nil {
return err
}
// to pretouch the corresponding pointer type as well
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
} else {
vt = reflect.PtrTo(vt)
}
if err := encoder.Pretouch(vt, opts...); err != nil {
return err
}
if err := decoder.Pretouch(vt, opts...); err != nil {
return err
}
return nil
}

0 comments on commit 1d64627

Please sign in to comment.