Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 1b0fdd5

Browse files
committed
goccy/go-json
1 parent dd2a346 commit 1b0fdd5

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

webapp/go/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
)
1515

1616
require (
17+
github.com/goccy/go-json v0.10.2 // indirect
1718
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
1819
github.com/gorilla/context v1.1.1 // indirect
1920
github.com/gorilla/securecookie v1.1.2 // indirect

webapp/go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
55
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
66
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
7+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
8+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
79
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
810
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
911
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=

webapp/go/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/jmoiron/sqlx"
1717
"github.com/labstack/echo/v4"
1818

19+
"github.com/goccy/go-json"
1920
"github.com/gorilla/sessions"
2021
"github.com/labstack/echo-contrib/session"
2122
echolog "github.com/labstack/gommon/log"
@@ -118,6 +119,23 @@ func initializeHandler(c echo.Context) error {
118119
})
119120
}
120121

122+
type JSONSerializer struct{}
123+
124+
func (j *JSONSerializer) Serialize(c echo.Context, i interface{}, indent string) error {
125+
enc := json.NewEncoder(c.Response())
126+
return enc.Encode(i)
127+
}
128+
129+
func (j *JSONSerializer) Deserialize(c echo.Context, i interface{}) error {
130+
err := json.NewDecoder(c.Request().Body).Decode(i)
131+
if ute, ok := err.(*json.UnmarshalTypeError); ok {
132+
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset)).SetInternal(err)
133+
} else if se, ok := err.(*json.SyntaxError); ok {
134+
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error())).SetInternal(err)
135+
}
136+
return err
137+
}
138+
121139
func main() {
122140
e := echo.New()
123141
e.Debug = true
@@ -128,6 +146,8 @@ func main() {
128146
e.Use(session.Middleware(cookieStore))
129147
// e.Use(middleware.Recover())
130148

149+
e.JSONSerializer = &JSONSerializer{}
150+
131151
// 初期化
132152
e.POST("/api/initialize", initializeHandler)
133153

0 commit comments

Comments
 (0)