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

Another JSON encoder support like gin support jsoniter #1698

Closed
3 tasks done
jeyldii opened this issue Nov 27, 2020 · 5 comments · Fixed by #1880
Closed
3 tasks done

Another JSON encoder support like gin support jsoniter #1698

jeyldii opened this issue Nov 27, 2020 · 5 comments · Fixed by #1880

Comments

@jeyldii
Copy link

jeyldii commented Nov 27, 2020

I wonder gin has jsoniter support. Let's integrate jsoniter like gin. Jsoniter better than encoding/json

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

go build -tags=jsoniter .

Actual behaviour

No support another json encoder

Steps to reproduce

Let's discuss about this

@jeyldii jeyldii changed the title Another JSON lib support like gin support jsoniter Another JSON encoder support like gin support jsoniter Nov 27, 2020
@aldas
Copy link
Contributor

aldas commented Nov 27, 2020

meanwhile you can create utility functions

func asJSON(c echo.Context, code int, i interface{}) error {
	response := c.Response()
	header := response.Header()
	if header.Get(echo.HeaderContentType) == "" {
		header.Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
	}
	response.Status = code
	return jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(response.Writer).Encode(i)
}

which is pretty much same what echo does in echo.context.json()

and respond in handler

	data := struct{ ID int64 }{ID: 1}
	return asJSON(c, http.StatusOK, data)

for request data binding:

	if err := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(c.Request().Body).Decode(&payload); err != nil {
		return err
	}

just to be annoying. This is interesting go podcast about json with standard library json package maintainer https://changelog.com/gotime/141 they talk about std encoding/json and other packages, some insights what encoding/json is and what it is not.

@lammel
Copy link
Contributor

lammel commented Dec 7, 2020

This comes up from time to time. See also #1394 and #1177.

@jeyldii What is the reason behind your request? Do you see performance issues for your real world usage of echo?

We just made the experience that JSON serialization is usually not the bottleneck for real applications (mostly databases and object stores are). But your mileage may vary.

@jeyldii
Copy link
Author

jeyldii commented Dec 8, 2020

This comes up from time to time. See also #1394 and #1177.

@jeyldii What is the reason behind your request? Do you see performance issues for your real world usage of echo?

We just made the experience that JSON serialization is usually not the bottleneck for real applications (mostly databases and object stores are). But your mileage may vary.

Hi,
I have not seen an explicit bottleneck, but in other cases where lots of marshal/unmarshal, jsoniter better than encoding/json in performance. For this reason, I wanted compatibility with jsoniter, because there can be just as many cases in http cases marshal/unmarshal.

@lammel lammel added the v5 label Dec 12, 2020
@lammel
Copy link
Contributor

lammel commented Dec 12, 2020

Understood.

I'm not sure adding complexity is worth it, but let's keep the discussion open and see if we want to add that for v5

@hoshsadiq
Copy link
Contributor

@lammel I'd also like to be able to customise the JSON implementation. In my case it's not jsoniter but google/jsonapi. This is unrelated to bottlenecks for me, and more to the aesthetics of the API.

I'd be happy to raise a PR. I'm thinking an implementation similar to how the binder is done (i.e. use an JSONEncoder/JSONDecoder and assign when creating the echo.Echo instance, with a default implementation of encoding/json). I obviously can use a helper method, but honestly, c.JSON() is a nice and simple API.

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

Successfully merging a pull request may close this issue.

4 participants