Skip to content

Commit

Permalink
Using json-iterator
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Mar 15, 2018
1 parent 62d3587 commit 20ac716
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
[[constraint]]
branch = "master"
name = "golang.org/x/crypto"

[[constraint]]
name = "github.com/json-iterator/go"
version = "1.1.2"
13 changes: 9 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package echo

import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
Expand All @@ -13,6 +12,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/json-iterator/go"
)

type (
Expand Down Expand Up @@ -206,6 +207,10 @@ const (
indexPage = "index.html"
)

var (
jsoni = jsoniter.ConfigCompatibleWithStandardLibrary
)

func (c *context) Request() *http.Request {
return c.request
}
Expand Down Expand Up @@ -402,15 +407,15 @@ func (c *context) JSON(code int, i interface{}) (err error) {
if c.echo.Debug || pretty {
return c.JSONPretty(code, i, " ")
}
b, err := json.Marshal(i)
b, err := jsoni.Marshal(i)
if err != nil {
return
}
return c.JSONBlob(code, b)
}

func (c *context) JSONPretty(code int, i interface{}, indent string) (err error) {
b, err := json.MarshalIndent(i, "", indent)
b, err := jsoni.MarshalIndent(i, "", indent)
if err != nil {
return
}
Expand All @@ -422,7 +427,7 @@ func (c *context) JSONBlob(code int, b []byte) (err error) {
}

func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
b, err := json.Marshal(i)
b, err := jsoni.Marshal(i)
if err != nil {
return
}
Expand Down
15 changes: 5 additions & 10 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ type (
Binder Binder
Validator Validator
Renderer Renderer
// Mutex sync.RWMutex
Logger Logger
Logger Logger
}

// Route contains a handler and information for matching against requests.
Expand Down Expand Up @@ -554,22 +553,18 @@ func (e *Echo) ReleaseContext(c Context) {

// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Acquire lock
// e.Mutex.RLock()
// defer e.Mutex.RUnlock()

// Acquire context
c := e.pool.Get().(*context)
c.Reset(r, w)

// Middleware
h := func(c Context) error {
method := r.Method
rpath := r.URL.RawPath // Raw path
if rpath == "" {
rpath = r.URL.Path
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
e.router.Find(method, rpath, c)
e.router.Find(method, path, c)
h := c.Handler()
for i := len(e.middleware) - 1; i >= 0; i-- {
h = e.middleware[i](h)
Expand Down

0 comments on commit 20ac716

Please sign in to comment.