Skip to content

Commit

Permalink
Merge branch 'main' into feat/arm_ast
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY authored Sep 17, 2023
2 parents 862dd9b + 73a97ab commit 971ba72
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 33 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 }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ err := sonic.Unmarshal(output, &data)
```

### Streaming IO
Sonic supports decoding json from `io.Reader` or encoding objects into `io.`Writer`, aims at handling multiple values as well as reducing memory consumption.
Sonic supports decoding json from `io.Reader` or encoding objects into `io.Writer`, aims at handling multiple values as well as reducing memory consumption.
- encoder
```go
var o1 = map[string]interface{}{
Expand Down
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
6 changes: 3 additions & 3 deletions ast/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ func (j *SortableNode) MarshalJSON() ([]byte, error) {
}

func TestMarshalSort(t *testing.T) {
var data = `{"d":3,"a":{"c":1,"b":2}}`
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}`, string(out))
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}`, string(out))
require.Equal(t, `{"a":{"b":2,"c":1},"d":3,"e":null}`, string(out))
}

func BenchmarkEncodeRaw_Sonic(b *testing.B) {
Expand Down
3 changes: 2 additions & 1 deletion ast/node.go
Original file line number Diff line number Diff line change
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 @@ -866,7 +867,7 @@ func (self *Node) SortKeys(recurse bool) error {
} 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
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
6 changes: 3 additions & 3 deletions internal/rt/int48.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package rt

const (
MinInt48 = -(1 << 47)
MaxInt48 = +(1 << 47) - 1
MinInt48 int64 = -(1 << 47)
MaxInt48 int64 = +(1 << 47) - 1
)

func PackInt(v int) uint64 {
if u := uint64(v); v < MinInt48 || v > MaxInt48 {
if u := uint64(v); int64(v) < MinInt48 || int64(v) > MaxInt48 {
panic("int48 out of range")
} else {
return ((u >> 63) << 47) | (u & 0x00007fffffffffff)
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 971ba72

Please sign in to comment.