diff --git a/nullable_marshaljson.go b/nullable_marshaljson.go index 0d3e199..0b478ef 100644 --- a/nullable_marshaljson.go +++ b/nullable_marshaljson.go @@ -2,6 +2,7 @@ package nul import ( "encoding/json" + "reflect" "github.com/reiver/go-erorr" ) @@ -15,7 +16,9 @@ func (receiver Nullable[T]) MarshalJSON() ([]byte, error) { case bool, string,json.Marshaler: // these are OK. default: - return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Marshaler’", receiver, receiver.value) + if reflect.Struct != reflect.TypeOf(receiver.value).Kind() { + return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Marshaler’", receiver, receiver.value) + } } if receiver.isnothing() { diff --git a/nullable_unmarshaljson.go b/nullable_unmarshaljson.go index 8aec384..2b9db7c 100644 --- a/nullable_unmarshaljson.go +++ b/nullable_unmarshaljson.go @@ -2,6 +2,7 @@ package nul import ( "encoding/json" + "reflect" "github.com/reiver/go-erorr" ) @@ -21,7 +22,9 @@ func (receiver *Nullable[T]) UnmarshalJSON(data []byte) error { case bool,string,json.Unmarshaler: // these are OK. default: - return erorr.Errorf("nul: cannot unmarshal into something of type %T from JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Unmarshaler’", receiver, receiver.value) + if reflect.Struct != reflect.TypeOf(receiver.value).Kind() { + return erorr.Errorf("nul: cannot unmarshal into something of type %T from JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Unmarshaler’", receiver, receiver.value) + } } if 4 == len(data) && 'n' == data[0] && 'u' == data[1] && 'l' == data[2] && 'l' == data[3] {