Skip to content

Fix custom marshaler for enum types #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2017

Conversation

olegshaldybin
Copy link
Contributor

When MarshalJSON was defined on a pointer receiver custom enum type
marshaling/unmarshaling was panicing since the underlying primitive type
was treated as a pointer.

Since method set for pointer receivers includes value receiver methods
we don't really need optionalEncoder and can just use marshalEncoder
directly.

--- FAIL: Test_custom_marshaler_on_enum (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1 pc=0x133d7a6]

goroutine 6 [running]:
testing.tRunner.func1(0xc42007b1e0)
	/usr/local/go/src/testing/testing.go:622 +0x29d
panic(0x13eb880, 0x1647d90)
	/usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/olegshaldybin/go.(*MyEnum).MarshalJSON(0x1, 0x13ec960, 0x1, 0x179b0e0, 0x1, 0xc4200c39e0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/jsoniter_enum_marshaler_test.go:18 +0x26
github.com/olegshaldybin/go.(*marshalerEncoder).Encode(0xc4200f1300, 0x1, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect_native.go:665 +0x6b
github.com/olegshaldybin/go.(*optionalEncoder).Encode(0xc42010e6a0, 0xc42010e5d0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect.go:124 +0x4f
github.com/olegshaldybin/go.WriteToStream(0x13c81e0, 0xc42010e5d0, 0xc420018b40, 0x1622ce0, 0xc42010e6a0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect.go:49 +0x10b
github.com/olegshaldybin/go.(*optionalEncoder).EncodeInterface(0xc42010e6a0, 0x13c81e0, 0xc42010e5d0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect.go:129 +0x55
github.com/olegshaldybin/go.(*Stream).WriteVal(0xc420018b40, 0x13c81e0, 0xc42010e5d0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect.go:234 +0xe4
github.com/olegshaldybin/go.(*emptyInterfaceCodec).Encode(0x1670410, 0xc42010e5e0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect_native.go:368 +0x41
github.com/olegshaldybin/go.(*structFieldEncoder).Encode(0xc4200f1240, 0xc42010e5e0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect_object.go:118 +0x5c
github.com/olegshaldybin/go.(*structEncoder).Encode(0xc4200f12c0, 0xc42010e5e0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect_object.go:155 +0xc2
github.com/olegshaldybin/go.(*structEncoder).EncodeInterface(0xc4200f12c0, 0x13f8e80, 0xc42010e5e0, 0xc420018b40)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect_object.go:175 +0x145
github.com/olegshaldybin/go.(*Stream).WriteVal(0xc420018b40, 0x13f8e80, 0xc42010e5e0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_reflect.go:234 +0xe4
github.com/olegshaldybin/go.(*frozenConfig).Marshal(0xc4200bcc40, 0x13f8e80, 0xc42010e5e0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_config.go:257 +0xdb
github.com/olegshaldybin/go.Marshal(0x13f8e80, 0xc42010e5e0, 0x13f8e80, 0xc42010e5e0, 0x1, 0x3, 0xc420102340)
	/Users/oleg/go/src/github.com/olegshaldybin/go/feature_adapter.go:43 +0x49
github.com/olegshaldybin/go.Test_custom_marshaler_on_enum(0xc42007b1e0)
	/Users/oleg/go/src/github.com/olegshaldybin/go/jsoniter_enum_marshaler_test.go:42 +0xd8
testing.tRunner(0xc42007b1e0, 0x1471208)
	/usr/local/go/src/testing/testing.go:657 +0x96
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:697 +0x2ca
exit status 2

When MarshalJSON was defined on a pointer receiver custom enum type
marshaling/unmarshaling was panicing since the underlying primitive type
was treated as a pointer.

Since method set for pointer receivers includes value receiver methods
we don't really need optionalEncoder and can just use marshalEncoder
directly.
@codecov
Copy link

codecov bot commented Sep 15, 2017

Codecov Report

Merging #170 into master will decrease coverage by 0.23%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #170      +/-   ##
==========================================
- Coverage   84.78%   84.55%   -0.24%     
==========================================
  Files          41       41              
  Lines        5200     5199       -1     
==========================================
- Hits         4409     4396      -13     
- Misses        631      639       +8     
- Partials      160      164       +4
Impacted Files Coverage Δ
feature_reflect_native.go 64.81% <ø> (ø) ⬆️
feature_reflect.go 77.53% <ø> (-0.05%) ⬇️
feature_iter_object.go 67.12% <0%> (-5.48%) ⬇️
feature_iter_skip.go 74.69% <0%> (-3.62%) ⬇️
feature_iter_string.go 88.81% <0%> (-0.7%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eef35e5...ae57d16. Read the comment docs.

@taowen taowen merged commit 9277257 into json-iterator:master Sep 15, 2017
zhenzou pushed a commit to zhenzou/jsoniter that referenced this pull request Feb 2, 2022
…pointer

Fix custom marshaler for enum types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants