1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

#23 hide unexported fields by default

This commit is contained in:
Tao Wen
2017-05-24 09:39:11 +08:00
parent 5fbe4e387d
commit 6126a6d3ca
3 changed files with 113 additions and 84 deletions

View File

@ -103,3 +103,22 @@ func Test_customize_field_by_extension(t *testing.T) {
t.Fatal(obj.field1)
}
}
func Test_unexported_fields(t *testing.T) {
EnableUnexportedStructFieldsSupport()
should := require.New(t)
type TestObject struct {
field1 string
field2 string `json:"field-2"`
}
obj := TestObject{}
obj.field1 = "hello"
should.Nil(UnmarshalFromString(`{}`, &obj))
should.Equal("hello", obj.field1)
should.Nil(UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
should.Equal("world", obj.field1)
should.Equal("abc", obj.field2)
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"field1":"world","field-2":"abc"}`, str)
}