Skip to content

Commit

Permalink
refactor(json): make jsonite optional with build tags (gin-gonic#1026)
Browse files Browse the repository at this point in the history
* refactor(json): Restore gin support for app engine

Create new folder to support multiple json package.
restore gin support for app engine (disable jsonite through tags)

use jsoniter

$ go build -tags=jsoniter .

use default json

$ go build .

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* rename json file.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* docs(json): add build tags document.

* fix(docs): markdown format.

* fix(json): missing space.
  • Loading branch information
appleboy authored and javierprovecho committed Jul 18, 2017
1 parent 7180f2b commit ce670a6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
- [x] Battle tested
- [x] API frozen, new releases will not break your code.


## Start using it

1. Download and install it:
Expand Down Expand Up @@ -141,6 +140,14 @@ $ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/mai
$ go run main.go
```

## Build with [jsoniter](https://github.com/json-iterator/go)

Gin use `encoding/json` as default json package but you can change to [jsoniter](https://github.com/json-iterator/go) by build from other tags.

```sh
$ go build -tags=jsoniter .
```

## API Examples

### Using GET, POST, PUT, PATCH, DELETE and OPTIONS
Expand Down
3 changes: 1 addition & 2 deletions binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package binding
import (
"net/http"

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

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
EnableDecoderUseNumber = false
)

Expand Down
4 changes: 1 addition & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"fmt"
"reflect"

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

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type ErrorType uint64

const (
Expand Down
1 change: 1 addition & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"testing"

"github.com/gin-gonic/gin/json"
"github.com/stretchr/testify/assert"
)

Expand Down
17 changes: 17 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017 Bo-Yi Wu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

// +build !jsoniter

package json

import (
"encoding/json"
)

var (
Marshal = json.Marshal
MarshalIndent = json.MarshalIndent
NewDecoder = json.NewDecoder
)
18 changes: 18 additions & 0 deletions json/jsoniter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2017 Bo-Yi Wu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

// +build jsoniter

package json

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

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
Marshal = json.Marshal
MarshalIndent = json.MarshalIndent
NewDecoder = json.NewDecoder
)
4 changes: 1 addition & 3 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"bytes"
"net/http"

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

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type JSON struct {
Data interface{}
}
Expand Down

0 comments on commit ce670a6

Please sign in to comment.