Skip to content
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

use encode replace json marshal increase json encoder speed #1546

Merged
merged 10 commits into from May 21, 2019

Conversation

itcloudy
Copy link
Contributor

@itcloudy itcloudy commented Sep 14, 2018

func WriteJSON(w http.ResponseWriter, obj interface{}) error {
	writeContentType(w, jsonContentType)
	jsonBytes, err := json.Marshal(obj)
	if err != nil {
		return err
	}
	w.Write(jsonBytes)
	return nil
}

func WriteJSON(w http.ResponseWriter, obj interface{}) error {
	writeContentType(w, jsonContentType)
	encoder := json.NewEncoder(w)
	err := encoder.Encode(&obj)
	if err != nil {
		return err
	}
	return nil
}

@thinkerou
Copy link
Member

@itcloudy please fix unit test error, and can you give one link or doc about compare encode with marshal? thanks!

@chainhelen
Copy link
Contributor

Marshal => String
Encode => Stream

go doc Encoder.Encode

@appleboy
Copy link
Member

@itcloudy build fail

@codecov
Copy link

codecov bot commented May 21, 2019

Codecov Report

Merging #1546 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1546      +/-   ##
==========================================
- Coverage   98.74%   98.74%   -0.01%     
==========================================
  Files          38       38              
  Lines        2145     2143       -2     
==========================================
- Hits         2118     2116       -2     
  Misses         15       15              
  Partials       12       12
Impacted Files Coverage Δ
render/json.go 87.34% <100%> (-0.32%) ⬇️

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 b1d607a...e0e6409. Read the comment docs.

@appleboy appleboy added this to the 1.5 milestone May 21, 2019
@thinkerou thinkerou merged commit 0cbf290 into gin-gonic:master May 21, 2019
@lpa
Copy link

lpa commented Sep 24, 2019

Hello, I'm sticking to Gin master revision since last week (to benefit from a dependency issue fix in master using gin + zerolog) and I agree more speed is good, but ... am I the only one shocked to have to cope with the additionnal "\n" that the use of Encode introduce or did I miss a way to remove it ? I mean it's the first time I see a framework forcing additional "\n" at the end of any JSON body data in responses (not that I've used many though ;) ) if we follow the documentation examples. I could use c.indentedJSON of c.asciiJSON but I don't want extra features, just plain JSON as I had before with c.JSON

@ghost
Copy link

ghost commented Dec 11, 2019

This felt like an early Xmas present. Between the disappearance of the gin/json package and a surprise newline character in my JSON payloads I really don't know which is more exciting.

But seriously breaking changes on this project need to be much better controlled especially in minor revisions.

@jpninanjohn
Copy link

jpninanjohn commented Feb 7, 2020

How does using encoder increase speed? If I follow the code of encode, it calls marshal() which is the same as what Marshal calls. Am I missing something here?
The only difference is that encode converts from/to a stream where as marshal converts from/to a string.

@AlexanderYastrebov
Copy link

How does using encoder increase speed? If I follow the code of encode, it calls marshal() which is the same as what Marshal calls. Am I missing something here?

Marshal creates a copy https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/encoding/json/encode.go;l=167
while Encode writes internal buffer Bytes() https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/encoding/json/stream.go;l=222 to the writer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants