Skip to content

Fix the incompatible encoding #336 #337

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
Jan 14, 2019

Conversation

denverdino
Copy link
Contributor

Fix #336

@codecov
Copy link

codecov bot commented Jan 3, 2019

Codecov Report

Merging #337 into master will increase coverage by 0.19%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #337      +/-   ##
==========================================
+ Coverage   81.48%   81.68%   +0.19%     
==========================================
  Files          41       41              
  Lines        5013     5012       -1     
==========================================
+ Hits         4085     4094       +9     
+ Misses        807      798       -9     
+ Partials      121      120       -1
Impacted Files Coverage Δ
reflect_marshaler.go 80.64% <100%> (-0.16%) ⬇️
reflect_struct_decoder.go 47.91% <0%> (+0.83%) ⬆️
stream.go 90% <0%> (+4%) ⬆️

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 d05f387...e4aa2ec. Read the comment docs.

@thockin
Copy link
Collaborator

thockin commented Jan 4, 2019

I am not very familiar with the code in question, but I don't immediately see what's changing here. Why is this better? Can you explain in the first PR comment?

@taowen can you please take a look? I don't want to have to carry a fork in Kubernetes :(

@denverdino
Copy link
Contributor Author

@thockin

FYI,

In the "encoding/json", for the object with Marshaler interface, it will have extra step for encoding rather than use the MarshalJSON() result directly. That is why the incompatible behavior happens.

encoding/json/encode.go

func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
	if v.Kind() == reflect.Ptr && v.IsNil() {
		e.WriteString("null")
		return
	}
	m, ok := v.Interface().(Marshaler)
	if !ok {
		e.WriteString("null")
		return
	}
	b, err := m.MarshalJSON()
	if err == nil {
		// copy JSON into buffer, checking validity.
		err = compact(&e.Buffer, b, opts.escapeHTML)
	}
	if err != nil {
		e.error(&MarshalerError{v.Type(), err})
	}
}

@taowen taowen merged commit 2d42ff7 into json-iterator:master Jan 14, 2019
zhenzou pushed a commit to zhenzou/jsoniter that referenced this pull request Feb 2, 2022
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.

3 participants