Skip to content

Commit a6d3337

Browse files
authored
Merge pull request #583 from Fenny/master
🚀 Improve JSON response
2 parents a8ad545 + 11b4ae3 commit a6d3337

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

ctx.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (ctx *Ctx) Format(body interface{}) {
377377
ctx.Send(body) // Fallback
378378
log.Println("Format: error serializing xml ", err)
379379
} else {
380-
ctx.SendString(getString(raw))
380+
ctx.Fasthttp.Response.SetBodyRaw(raw)
381381
}
382382
default:
383383
ctx.SendString(b)
@@ -515,8 +515,8 @@ func (ctx *Ctx) JSON(data interface{}) error {
515515
}
516516
// Set http headers
517517
ctx.Fasthttp.Response.Header.SetContentType(MIMEApplicationJSON)
518-
ctx.SendString(getString(raw))
519-
// Success!
518+
ctx.Fasthttp.Response.SetBodyRaw(raw)
519+
520520
return nil
521521
}
522522

ctx_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1472,3 +1472,31 @@ func Benchmark_Ctx_SendBytes_With_SetBodyRaw(b *testing.B) {
14721472
}
14731473
utils.AssertEqual(b, body, c.Fasthttp.Response.Body())
14741474
}
1475+
1476+
// go test -v -run=^$ -bench=Benchmark_Ctx_SendString_B -benchmem -count=4
1477+
func Benchmark_Ctx_SendString_B(b *testing.B) {
1478+
app := New()
1479+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1480+
defer app.ReleaseCtx(c)
1481+
body := "Hello, world!"
1482+
b.ReportAllocs()
1483+
b.ResetTimer()
1484+
for n := 0; n < b.N; n++ {
1485+
c.SendString(body)
1486+
}
1487+
utils.AssertEqual(b, []byte("Hello, world!"), c.Fasthttp.Response.Body())
1488+
}
1489+
1490+
// go test -v -run=^$ -bench=Benchmark_Ctx_SendBytes_B -benchmem -count=4
1491+
func Benchmark_Ctx_SendBytes_B(b *testing.B) {
1492+
app := New()
1493+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1494+
defer app.ReleaseCtx(c)
1495+
body := []byte("Hello, world!")
1496+
b.ReportAllocs()
1497+
b.ResetTimer()
1498+
for n := 0; n < b.N; n++ {
1499+
c.SendBytes(body)
1500+
}
1501+
utils.AssertEqual(b, []byte("Hello, world!"), c.Fasthttp.Response.Body())
1502+
}

0 commit comments

Comments
 (0)