Faster JSON serializing

pull/6/head
Fenny 2020-01-09 03:33:09 +01:00
parent 435d4b78ef
commit 39f08beb80
2 changed files with 11 additions and 10 deletions

View File

@ -1,17 +1,18 @@
package fiber package fiber
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"mime" "mime"
"mime/multipart" "mime/multipart"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/pquerna/ffjson/ffjson" "github.com/json-iterator/go"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Next : // Next :
func (ctx *Ctx) Next() { func (ctx *Ctx) Next() {
ctx.next = true ctx.next = true
@ -217,11 +218,12 @@ func (ctx *Ctx) Get(key string) string {
// Json : // Json :
func (ctx *Ctx) Json(v interface{}) error { func (ctx *Ctx) Json(v interface{}) error {
raw, err := json.Marshal(&v)
if err != nil {
return err
}
ctx.Set("Content-Type", "application/json") ctx.Set("Content-Type", "application/json")
b := bytes.NewBuffer(nil) ctx.Send(b2s(raw))
enc := ffjson.NewEncoder(b)
err := enc.Encode(v)
ctx.Send(b.Bytes())
return err return err
} }

View File

@ -12,11 +12,12 @@ import (
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
// ParseJSON "github.com/tidwall/gjson"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
const ( const (
Version = `v0.3.0` Version = `v0.3.1`
banner = ` _____ _ _ banner = ` _____ _ _
| __|_| |_ ___ ___ | __|_| |_ ___ ___
| __| | . | -_| _| | __| | . | -_| _|
@ -66,7 +67,6 @@ type Settings struct {
// Fiber : // Fiber :
type Fiber struct { type Fiber struct {
routes []*route routes []*route
methods []string
Settings *Settings Settings *Settings
} }
@ -316,7 +316,7 @@ func (r *Fiber) Listen(args ...interface{}) {
r.Settings.NoDefaultServerHeader = false r.Settings.NoDefaultServerHeader = false
} }
server := &fasthttp.Server{ server := &fasthttp.Server{
// Express custom handler // Fiber custom handler
Handler: r.handler, Handler: r.handler,
// Server settings // Server settings
Name: r.Settings.Name, Name: r.Settings.Name,
@ -355,7 +355,6 @@ func (r *Fiber) Listen(args ...interface{}) {
if !r.Settings.HideBanner { if !r.Settings.HideBanner {
fmt.Printf(color.HiCyanString(banner), color.GreenString(":"+port), color.HiBlackString("("+Version+")")) fmt.Printf(color.HiCyanString(banner), color.GreenString(":"+port), color.HiBlackString("("+Version+")"))
} }
// fmt.Printf(banner, Version)
if r.Settings.TLSEnable { if r.Settings.TLSEnable {
if err := server.ListenAndServeTLS(fmt.Sprintf("%s:%s", addr, port), r.Settings.CertFile, r.Settings.CertKey); err != nil { if err := server.ListenAndServeTLS(fmt.Sprintf("%s:%s", addr, port), r.Settings.CertFile, r.Settings.CertKey); err != nil {
panic(err) panic(err)