1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-30 23:23:49 +02:00

Allow null booleans

Make sure we do the same thing as stdlib with null booleans by not
touching the original value and discarding the null.

Another somewhat related change is nulling out null interface values in
the original structure. This also matches stdlib behavior.
This commit is contained in:
Oleg Shaldybin
2017-09-14 16:20:27 -07:00
parent 0fdf883ac0
commit 18a241d40b
3 changed files with 75 additions and 2 deletions

View File

@ -3,9 +3,10 @@ package jsoniter
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
func Test_write_array_of_interface(t *testing.T) {
@ -313,3 +314,40 @@ func Test_unmarshal_ptr_to_interface(t *testing.T) {
should.Nil(err)
should.Equal("&{value}", fmt.Sprintf("%v", obj))
}
func Test_nil_out_null_interface(t *testing.T) {
type TestData struct {
Field interface{} `json:"field"`
}
should := require.New(t)
var boolVar bool
obj := TestData{
Field: &boolVar,
}
data1 := []byte(`{"field": true}`)
err := Unmarshal(data1, &obj)
should.Equal(nil, err)
should.Equal(true, *(obj.Field.(*bool)))
data2 := []byte(`{"field": null}`)
err = Unmarshal(data2, &obj)
should.Equal(nil, err)
should.Equal(nil, obj.Field)
// Checking stdlib behavior matches.
obj2 := TestData{
Field: &boolVar,
}
err = json.Unmarshal(data1, &obj2)
should.Equal(nil, err)
should.Equal(true, *(obj2.Field.(*bool)))
err = json.Unmarshal(data2, &obj2)
should.Equal(nil, err)
should.Equal(nil, obj2.Field)
}