github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/compat_test.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sonic
    18  
    19  import (
    20      `bytes`
    21      `encoding/json`
    22      `reflect`
    23      `testing`
    24  
    25      `github.com/bytedance/sonic/option`
    26      `github.com/stretchr/testify/require`
    27  )
    28  
    29  func TestCompatUnmarshalStd(t *testing.T) {
    30      var sobj = map[string]interface{}{}
    31      var jobj = map[string]interface{}{}
    32      var data = []byte(`{"a":1.00000001E-10}`)
    33      var str = string(data)
    34      serr := ConfigStd.UnmarshalFromString(str, &sobj)
    35      jerr := json.Unmarshal(data, &jobj)
    36      require.Equal(t, jerr, serr)
    37      require.Equal(t, jobj, sobj)
    38      data[2] = '0'
    39      require.Equal(t, jobj, sobj)
    40  
    41      sobj = map[string]interface{}{}
    42      jobj = map[string]interface{}{}
    43      data = []byte(`{"a":1}`)
    44      cfg := Config{
    45          UseNumber: true,
    46      }.Froze()
    47      serr = cfg.Unmarshal(data, &sobj)
    48      dec := json.NewDecoder(bytes.NewBuffer(data))
    49      dec.UseNumber()
    50      jerr = dec.Decode(&jobj)
    51      require.Equal(t, jerr, serr)
    52      require.Equal(t, jobj, sobj)
    53  
    54      x := struct{
    55          A json.Number
    56          B json.Number
    57      }{}
    58      y := struct{
    59          A json.Number
    60          B json.Number
    61      }{}
    62      data = []byte(`{"A":"1", "C":-1, "B":1}`)
    63      cfg = Config{
    64          DisallowUnknownFields: true,
    65      }.Froze()
    66      serr = cfg.Unmarshal(data, &x)
    67      dec = json.NewDecoder(bytes.NewBuffer(data))
    68      dec.UseNumber()
    69      dec.DisallowUnknownFields()
    70      jerr = dec.Decode(&y)
    71      require.Equal(t, jerr, serr)
    72      // require.Equal(t, y, x)
    73  }
    74  
    75  func TestCompatMarshalStd(t *testing.T) {
    76      t.Parallel()
    77      var obj = map[string]interface{}{
    78          "c": json.RawMessage(" [ \"<&>\" ] "),
    79          "b": json.RawMessage(" [ ] "),
    80      }
    81      sout, serr := ConfigStd.Marshal(obj)
    82      jout, jerr := json.Marshal(obj)
    83      require.Equal(t, jerr, serr)
    84      require.Equal(t, string(jout), string(sout))
    85  
    86      obj = map[string]interface{}{
    87          "a": json.RawMessage(" [} "),
    88      }
    89      sout, serr = ConfigStd.Marshal(obj)
    90      jout, jerr = json.Marshal(obj)
    91      require.NotNil(t, jerr)
    92      require.NotNil(t, serr)
    93      require.Equal(t, string(jout), string(sout))
    94  
    95      obj = map[string]interface{}{
    96          "a": json.RawMessage("1"),
    97      }
    98      sout, serr = ConfigStd.MarshalIndent(obj, "xxxx", "  ")
    99      jout, jerr = json.MarshalIndent(obj, "xxxx", "  ")
   100      require.Equal(t, jerr, serr)
   101      require.Equal(t, string(jout), string(sout))
   102  }
   103  
   104  func TestCompatEncoderStd(t *testing.T) {
   105      var o = map[string]interface{}{
   106          "a": "<>",
   107          "b": json.RawMessage(" [ ] "),
   108      }
   109      var w1 = bytes.NewBuffer(nil)
   110      var w2 = bytes.NewBuffer(nil)
   111      var enc1 = json.NewEncoder(w1)
   112      var enc2 = ConfigStd.NewEncoder(w2)
   113  
   114      require.Nil(t, enc1.Encode(o))
   115      require.Nil(t, enc2.Encode(o))
   116      require.Equal(t, w1.String(), w2.String())
   117  
   118      enc1.SetEscapeHTML(true)
   119      enc2.SetEscapeHTML(true)
   120      enc1.SetIndent("\n", "  ")
   121      enc2.SetIndent("\n", "  ")
   122      require.Nil(t, enc1.Encode(o))
   123      require.Nil(t, enc2.Encode(o))
   124      require.Equal(t, w1.String(), w2.String())
   125  
   126      enc1.SetEscapeHTML(false)
   127      enc2.SetEscapeHTML(false)
   128      enc1.SetIndent("", "")
   129      enc2.SetIndent("", "")
   130      require.Nil(t, enc1.Encode(o))
   131      require.Nil(t, enc2.Encode(o))
   132      require.Equal(t, w1.String(), w2.String())
   133  }
   134  
   135  func TestCompatDecoderStd(t *testing.T) {
   136      var o1 = map[string]interface{}{}
   137      var o2 = map[string]interface{}{}
   138      var s = `{"a":"b"} {"1":"2"} a {}`
   139      var w1 = bytes.NewBuffer([]byte(s))
   140      var w2 = bytes.NewBuffer([]byte(s))
   141      var enc1 = json.NewDecoder(w1)
   142      var enc2 = ConfigStd.NewDecoder(w2)
   143  
   144      require.Equal(t, enc1.More(), enc2.More())
   145      require.Nil(t, enc1.Decode(&o1))
   146      require.Nil(t, enc2.Decode(&o2))
   147      require.Equal(t, w1.String(), w2.String())
   148  
   149      require.Equal(t, enc1.More(), enc2.More())
   150      require.Nil(t, enc1.Decode(&o1))
   151      require.Nil(t, enc2.Decode(&o2))
   152      require.Equal(t, w1.String(), w2.String())
   153  
   154      require.Equal(t, enc1.More(), enc2.More())
   155      require.NotNil(t, enc1.Decode(&o1))
   156      require.NotNil(t, enc2.Decode(&o2))
   157      require.Equal(t, w1.String(), w2.String())
   158  }
   159  
   160  func TestPretouch(t *testing.T) {
   161      var v map[string]interface{}
   162      if err := Pretouch(reflect.TypeOf(v)); err != nil {
   163          t.Errorf("err:%v", err)
   164      }
   165  
   166      if err := Pretouch(reflect.TypeOf(v),
   167         option.WithCompileRecursiveDepth(1),
   168         option.WithCompileMaxInlineDepth(2),
   169      ); err != nil {
   170          t.Errorf("err:%v", err)
   171      }
   172  }
   173  
   174  func TestGet(t *testing.T) {
   175      var data = `{"a":"b"}`
   176      r, err := GetFromString(data, "a")
   177      if err != nil {
   178          t.Fatal(err)
   179      }
   180      v, err := r.String()
   181      if err != nil {
   182          t.Fatal(err)
   183      }
   184      if v != "b" {
   185          t.Fatal(v)
   186      }
   187  }
   188