Use jsoniter to improve performance

This commit is contained in:
Eduard Urbach 2018-07-02 09:39:25 +09:00
parent bb1ce79d95
commit 082e16fb42
4 changed files with 9 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package auth
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -11,6 +10,7 @@ import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
jsoniter "github.com/json-iterator/go"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/facebook" "golang.org/x/oauth2/facebook"
) )
@ -88,7 +88,7 @@ func InstallFacebookAuth(app *aero.Application) {
// Construct a FacebookUser object // Construct a FacebookUser object
fbUser := FacebookUser{} fbUser := FacebookUser{}
err = json.Unmarshal(body, &fbUser) err = jsoniter.Unmarshal(body, &fbUser)
if err != nil { if err != nil {
return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err) return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err)

View File

@ -2,7 +2,6 @@ package auth
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -11,6 +10,7 @@ import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
jsoniter "github.com/json-iterator/go"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
) )
@ -94,7 +94,7 @@ func InstallGoogleAuth(app *aero.Application) {
// Construct a GoogleUser object // Construct a GoogleUser object
var googleUser GoogleUser var googleUser GoogleUser
err = json.Unmarshal(data, &googleUser) err = jsoniter.Unmarshal(data, &googleUser)
if err != nil { if err != nil {
return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err) return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err)

View File

@ -1,7 +1,6 @@
package fullpage package fullpage
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"sort" "sort"
@ -9,6 +8,7 @@ import (
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
jsoniter "github.com/json-iterator/go"
) )
var organization map[string]interface{} var organization map[string]interface{}
@ -17,8 +17,8 @@ var organizationString string
func init() { func init() {
// Load structured data // Load structured data
organizationBytes, _ := ioutil.ReadFile("organization.json") organizationBytes, _ := ioutil.ReadFile("organization.json")
json.Unmarshal(organizationBytes, &organization) jsoniter.Unmarshal(organizationBytes, &organization)
organizationBytes, _ = json.Marshal(organization) organizationBytes, _ = jsoniter.Marshal(organization)
organizationString = string(organizationBytes) organizationString = string(organizationBytes)
} }

View File

@ -1,9 +1,9 @@
package utils package utils
import "encoding/json" import jsoniter "github.com/json-iterator/go"
// ToJSON converts an object to a JSON string, ignoring errors. // ToJSON converts an object to a JSON string, ignoring errors.
func ToJSON(v interface{}) string { func ToJSON(v interface{}) string {
str, _ := json.Marshal(v) str, _ := jsoniter.Marshal(v)
return string(str) return string(str)
} }