...

Source file src/github.com/json-iterator/go/value_tests/invalid_test.go

Documentation: github.com/json-iterator/go/value_tests

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"github.com/json-iterator/go"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"io"
    10  	"testing"
    11  )
    12  
    13  func Test_missing_object_end(t *testing.T) {
    14  	should := require.New(t)
    15  	type TestObject struct {
    16  		Metric string                 `json:"metric"`
    17  		Tags   map[string]interface{} `json:"tags"`
    18  	}
    19  	obj := TestObject{}
    20  	should.NotNil(jsoniter.UnmarshalFromString(`{"metric": "sys.777","tags": {"a":"123"}`, &obj))
    21  }
    22  
    23  func Test_missing_array_end(t *testing.T) {
    24  	should := require.New(t)
    25  	should.NotNil(jsoniter.UnmarshalFromString(`[1,2,3`, &[]int{}))
    26  }
    27  
    28  func Test_invalid_any(t *testing.T) {
    29  	should := require.New(t)
    30  	any := jsoniter.Get([]byte("[]"))
    31  	should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType())
    32  	// is nil correct ?
    33  	should.Equal(nil, any.Get(0.3).GetInterface())
    34  
    35  	any = any.Get(0.3)
    36  	should.Equal(false, any.ToBool())
    37  	should.Equal(int(0), any.ToInt())
    38  	should.Equal(int32(0), any.ToInt32())
    39  	should.Equal(int64(0), any.ToInt64())
    40  	should.Equal(uint(0), any.ToUint())
    41  	should.Equal(uint32(0), any.ToUint32())
    42  	should.Equal(uint64(0), any.ToUint64())
    43  	should.Equal(float32(0), any.ToFloat32())
    44  	should.Equal(float64(0), any.ToFloat64())
    45  	should.Equal("", any.ToString())
    46  
    47  	should.Equal(jsoniter.InvalidValue, any.Get(0.1).Get(1).ValueType())
    48  }
    49  
    50  func Test_invalid_struct_input(t *testing.T) {
    51  	should := require.New(t)
    52  	type TestObject struct{}
    53  	input := []byte{54, 141, 30}
    54  	obj := TestObject{}
    55  	should.NotNil(jsoniter.Unmarshal(input, &obj))
    56  }
    57  
    58  func Test_invalid_slice_input(t *testing.T) {
    59  	should := require.New(t)
    60  	type TestObject struct{}
    61  	input := []byte{93}
    62  	obj := []string{}
    63  	should.NotNil(jsoniter.Unmarshal(input, &obj))
    64  }
    65  
    66  func Test_invalid_array_input(t *testing.T) {
    67  	should := require.New(t)
    68  	type TestObject struct{}
    69  	input := []byte{93}
    70  	obj := [0]string{}
    71  	should.NotNil(jsoniter.Unmarshal(input, &obj))
    72  }
    73  
    74  func Test_invalid_float(t *testing.T) {
    75  	inputs := []string{
    76  		`1.e1`, // dot without following digit
    77  		`1.`,   // dot can not be the last char
    78  		``,     // empty number
    79  		`01`,   // extra leading zero
    80  		`-`,    // negative without digit
    81  		`--`,   // double negative
    82  		`--2`,  // double negative
    83  	}
    84  	for _, input := range inputs {
    85  		t.Run(input, func(t *testing.T) {
    86  			should := require.New(t)
    87  			iter := jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
    88  			iter.Skip()
    89  			should.NotEqual(io.EOF, iter.Error)
    90  			should.NotNil(iter.Error)
    91  			v := float64(0)
    92  			should.NotNil(json.Unmarshal([]byte(input), &v))
    93  			iter = jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
    94  			iter.ReadFloat64()
    95  			should.NotEqual(io.EOF, iter.Error)
    96  			should.NotNil(iter.Error)
    97  			iter = jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
    98  			iter.ReadFloat32()
    99  			should.NotEqual(io.EOF, iter.Error)
   100  			should.NotNil(iter.Error)
   101  		})
   102  	}
   103  }
   104  
   105  func Test_chan(t *testing.T) {
   106  	type TestObject struct {
   107  		MyChan  chan bool
   108  		MyField int
   109  	}
   110  
   111  	obj := TestObject{}
   112  
   113  	t.Run("Encode channel", func(t *testing.T) {
   114  		should := require.New(t)
   115  		str, err := jsoniter.Marshal(obj)
   116  		should.NotNil(err)
   117  		should.Nil(str)
   118  	})
   119  
   120  	t.Run("Encode channel using compatible configuration", func(t *testing.T) {
   121  		should := require.New(t)
   122  		str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
   123  		should.NotNil(err)
   124  		should.Nil(str)
   125  	})
   126  }
   127  
   128  func Test_invalid_in_map(t *testing.T) {
   129  	testMap := map[string]interface{}{"chan": make(chan interface{})}
   130  
   131  	t.Run("Encode map with invalid content", func(t *testing.T) {
   132  		should := require.New(t)
   133  		str, err := jsoniter.Marshal(testMap)
   134  		should.NotNil(err)
   135  		should.Nil(str)
   136  	})
   137  
   138  	t.Run("Encode map with invalid content using compatible configuration", func(t *testing.T) {
   139  		should := require.New(t)
   140  		str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testMap)
   141  		should.NotNil(err)
   142  		should.Nil(str)
   143  	})
   144  }
   145  
   146  func Test_invalid_number(t *testing.T) {
   147  	type Message struct {
   148  		Number int `json:"number"`
   149  	}
   150  	obj := Message{}
   151  	decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
   152  	err := decoder.Decode(&obj)
   153  	invalidStr := err.Error()
   154  	result, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(invalidStr)
   155  	should := require.New(t)
   156  	should.Nil(err)
   157  	result2, err := json.Marshal(invalidStr)
   158  	should.Nil(err)
   159  	should.Equal(string(result2), string(result))
   160  }
   161  
   162  func Test_valid(t *testing.T) {
   163  	should := require.New(t)
   164  	should.True(jsoniter.Valid([]byte(`{}`)))
   165  	should.False(jsoniter.Valid([]byte(`{`)))
   166  }
   167  
   168  func Test_nil_pointer(t *testing.T) {
   169  	should := require.New(t)
   170  	data := []byte(`{"A":0}`)
   171  	type T struct {
   172  		X int
   173  	}
   174  	var obj *T
   175  	err := jsoniter.Unmarshal(data, obj)
   176  	should.NotNil(err)
   177  }
   178  
   179  func Test_func_pointer_type(t *testing.T) {
   180  	type TestObject2 struct {
   181  		F func()
   182  	}
   183  	type TestObject1 struct {
   184  		Obj *TestObject2
   185  	}
   186  	t.Run("encode null is valid", func(t *testing.T) {
   187  		should := require.New(t)
   188  		output, err := json.Marshal(TestObject1{})
   189  		should.Nil(err)
   190  		should.Equal(`{"Obj":null}`, string(output))
   191  		output, err = jsoniter.Marshal(TestObject1{})
   192  		should.Nil(err)
   193  		should.Equal(`{"Obj":null}`, string(output))
   194  	})
   195  	t.Run("encode not null is invalid", func(t *testing.T) {
   196  		should := require.New(t)
   197  		_, err := json.Marshal(TestObject1{Obj: &TestObject2{}})
   198  		should.NotNil(err)
   199  		_, err = jsoniter.Marshal(TestObject1{Obj: &TestObject2{}})
   200  		should.NotNil(err)
   201  	})
   202  	t.Run("decode null is valid", func(t *testing.T) {
   203  		should := require.New(t)
   204  		var obj TestObject1
   205  		should.Nil(json.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
   206  		should.Nil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
   207  	})
   208  	t.Run("decode not null is invalid", func(t *testing.T) {
   209  		should := require.New(t)
   210  		var obj TestObject1
   211  		should.NotNil(json.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
   212  		should.NotNil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
   213  	})
   214  }
   215  
   216  func TestEOF(t *testing.T) {
   217  	var s string
   218  	err := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
   219  	assert.Equal(t, io.EOF, err)
   220  }
   221  
   222  func TestDecodeErrorType(t *testing.T) {
   223  	should := require.New(t)
   224  	var err error
   225  	should.Nil(jsoniter.Unmarshal([]byte("null"), &err))
   226  	should.NotNil(jsoniter.Unmarshal([]byte("123"), &err))
   227  }
   228  
   229  func Test_decode_slash(t *testing.T) {
   230  	should := require.New(t)
   231  	var obj interface{}
   232  	should.NotNil(json.Unmarshal([]byte("\\"), &obj))
   233  	should.NotNil(jsoniter.UnmarshalFromString("\\", &obj))
   234  }
   235  
   236  func Test_NilInput(t *testing.T) {
   237  	var jb []byte // nil
   238  	var out string
   239  	err := jsoniter.Unmarshal(jb, &out)
   240  	if err == nil {
   241  		t.Errorf("Expected error")
   242  	}
   243  }
   244  
   245  func Test_EmptyInput(t *testing.T) {
   246  	jb := []byte("")
   247  	var out string
   248  	err := jsoniter.Unmarshal(jb, &out)
   249  	if err == nil {
   250  		t.Errorf("Expected error")
   251  	}
   252  }
   253  
   254  type Foo struct {
   255  	A jsoniter.Any
   256  }
   257  
   258  func Test_nil_any(t *testing.T) {
   259  	should := require.New(t)
   260  	data, _ := jsoniter.Marshal(&Foo{})
   261  	should.Equal(`{"A":null}`, string(data))
   262  }
   263  

View as plain text