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
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
}

View File

@ -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)