Skip to content

Commit 01d2ef8

Browse files
author
Samuel Oechsler
committed
Add custom json un-/marshal functions
1 parent af5fcc8 commit 01d2ef8

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# vNext
22

3-
# v.1.3.0
3+
# v1.3.1
4+
5+
- (feature) Add custom json un-/marshall functions
6+
- (internal) Use goccy/go-json when Architecture is ARM64 instead of bytedance/sonic
7+
8+
# v1.3.0
49

510
- (feature) Add bytedance/sonic as default fiber json en-/decoder
611
- (feature) Make json en-/decoder configurable with `FIBER_JSON_ENCODER` and `FIBER_JSON_DECODER`

fiber/fiber.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ package fiber
22

33
import (
44
"context"
5-
"encoding/json"
65
"time"
76

87
"github.com/Becklyn/go-wire-core/app"
98
"github.com/Becklyn/go-wire-core/env"
9+
"github.com/Becklyn/go-wire-core/json"
1010
"github.com/Becklyn/go-wire-core/metrics"
11-
"github.com/bytedance/sonic"
1211
"github.com/fraym/golog"
1312
"github.com/gofiber/fiber/v2"
1413
"github.com/gofiber/fiber/v2/middleware/cors"
1514
fiberlog "github.com/gofiber/fiber/v2/middleware/logger"
16-
"github.com/gofiber/fiber/v2/utils"
1715
)
1816

1917
type MiddlewareHandlerMap map[string][]fiber.Handler
@@ -28,23 +26,10 @@ type NewFiberOptions struct {
2826
}
2927

3028
func NewFiber(options *NewFiberOptions) *fiber.App {
31-
jsonEncoder := func() utils.JSONMarshal {
32-
if env.StringWithDefault("FIBER_JSON_ENCODER", "sonic") == "sonic" {
33-
return sonic.Marshal
34-
}
35-
return json.Marshal
36-
}()
37-
jsonDecoder := func() utils.JSONUnmarshal {
38-
if env.StringWithDefault("FIBER_JSON_DECODER", "sonic") == "sonic" {
39-
return sonic.Unmarshal
40-
}
41-
return json.Unmarshal
42-
}()
43-
4429
app := fiber.New(fiber.Config{
4530
BodyLimit: env.IntWithDefault("HTTP_REQUEST_BODY_LIMIT", 4) * 1024 * 1024,
46-
JSONEncoder: jsonEncoder,
47-
JSONDecoder: jsonDecoder,
31+
JSONEncoder: json.Marshal,
32+
JSONDecoder: json.Unmarshal,
4833
})
4934

5035
app.Use(fiberlog.New(fiberlog.Config{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
2424
github.com/chenzhuoyu/iasm v0.9.0 // indirect
2525
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/goccy/go-json v0.10.2 // indirect
2627
github.com/golang/protobuf v1.5.2 // indirect
2728
github.com/google/uuid v1.3.0 // indirect
2829
github.com/klauspost/compress v1.16.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
8787
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
8888
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
8989
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
90+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
91+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
9092
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
9193
github.com/gofiber/adaptor/v2 v2.1.27 h1:I3Xjn1qaH6W09shPi8F/kz98AncwbVYWFBV4niBBC0Q=
9294
github.com/gofiber/adaptor/v2 v2.1.27/go.mod h1:xcc+UqppJ+XfYVcqxp2fHyoqKctQxxvZ/IyUybNSIlQ=

json/marshal.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package json
2+
3+
import (
4+
stdjson "encoding/json"
5+
"runtime"
6+
7+
"github.com/Becklyn/go-wire-core/env"
8+
"github.com/bytedance/sonic"
9+
"github.com/goccy/go-json"
10+
)
11+
12+
func Marshal(v interface{}) ([]byte, error) {
13+
if env.StringWithDefault("JSON_ENCODER", "std") == "std" {
14+
return stdjson.Marshal(v)
15+
}
16+
17+
if runtime.GOARCH == "amd64" {
18+
return sonic.Marshal(v)
19+
}
20+
21+
return json.Marshal(v)
22+
}

json/unmarshal.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package json
2+
3+
import (
4+
stdjson "encoding/json"
5+
"runtime"
6+
7+
"github.com/Becklyn/go-wire-core/env"
8+
"github.com/bytedance/sonic"
9+
"github.com/goccy/go-json"
10+
)
11+
12+
func Unmarshal(data []byte, v interface{}) error {
13+
if env.StringWithDefault("JSON_DECODER", "std") == "std" {
14+
return stdjson.Unmarshal(data, v)
15+
}
16+
17+
if runtime.GOARCH == "amd64" {
18+
return sonic.Unmarshal(data, v)
19+
}
20+
21+
return json.Unmarshal(data, v)
22+
}

0 commit comments

Comments
 (0)