From 39f08beb809a062431142d97105ec23d9eb11192 Mon Sep 17 00:00:00 2001 From: Fenny Date: Thu, 9 Jan 2020 03:33:09 +0100 Subject: [PATCH] Faster JSON serializing --- context.go | 14 ++++++++------ router.go | 7 +++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/context.go b/context.go index 25176234..0f0743cd 100644 --- a/context.go +++ b/context.go @@ -1,17 +1,18 @@ package fiber import ( - "bytes" "encoding/base64" "mime" "mime/multipart" "path/filepath" "strings" - "github.com/pquerna/ffjson/ffjson" + "github.com/json-iterator/go" "github.com/valyala/fasthttp" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Next : func (ctx *Ctx) Next() { ctx.next = true @@ -217,11 +218,12 @@ func (ctx *Ctx) Get(key string) string { // Json : func (ctx *Ctx) Json(v interface{}) error { + raw, err := json.Marshal(&v) + if err != nil { + return err + } ctx.Set("Content-Type", "application/json") - b := bytes.NewBuffer(nil) - enc := ffjson.NewEncoder(b) - err := enc.Encode(v) - ctx.Send(b.Bytes()) + ctx.Send(b2s(raw)) return err } diff --git a/router.go b/router.go index 7a019812..2e29aa3a 100644 --- a/router.go +++ b/router.go @@ -12,11 +12,12 @@ import ( "time" "github.com/fatih/color" + // ParseJSON "github.com/tidwall/gjson" "github.com/valyala/fasthttp" ) const ( - Version = `v0.3.0` + Version = `v0.3.1` banner = ` _____ _ _ | __|_| |_ ___ ___ | __| | . | -_| _| @@ -66,7 +67,6 @@ type Settings struct { // Fiber : type Fiber struct { routes []*route - methods []string Settings *Settings } @@ -316,7 +316,7 @@ func (r *Fiber) Listen(args ...interface{}) { r.Settings.NoDefaultServerHeader = false } server := &fasthttp.Server{ - // Express custom handler + // Fiber custom handler Handler: r.handler, // Server settings Name: r.Settings.Name, @@ -355,7 +355,6 @@ func (r *Fiber) Listen(args ...interface{}) { if !r.Settings.HideBanner { fmt.Printf(color.HiCyanString(banner), color.GreenString(":"+port), color.HiBlackString("("+Version+")")) } - // fmt.Printf(banner, Version) if r.Settings.TLSEnable { if err := server.ListenAndServeTLS(fmt.Sprintf("%s:%s", addr, port), r.Settings.CertFile, r.Settings.CertKey); err != nil { panic(err)