From f79f63967e9d95c2d9e43d6eba98e49d0bb1c428 Mon Sep 17 00:00:00 2001 From: hbc Date: Sat, 7 Oct 2023 22:17:08 +0800 Subject: [PATCH] feat: use json2 --- go.mod | 1 + go.sum | 2 ++ server.go | 5 ++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 78c4c62..ff72ca7 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/go-chi/chi/v5 v5.0.10 github.com/go-chi/cors v1.2.1 + github.com/go-json-experiment/json v0.0.0-20230922184908-dc36ffcf8533 github.com/go-logr/logr v1.2.4 github.com/go-logr/zapr v1.2.4 github.com/golang-jwt/jwt v3.2.2+incompatible diff --git a/go.sum b/go.sum index 8661419..72f83d2 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-json-experiment/json v0.0.0-20230922184908-dc36ffcf8533 h1:1SRqDZauC9fz6vMIDLCUOULPNfOnZ0rmvZo8quraoy4= +github.com/go-json-experiment/json v0.0.0-20230922184908-dc36ffcf8533/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= diff --git a/server.go b/server.go index 3ffb3fc..e9f9878 100644 --- a/server.go +++ b/server.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/json" "errors" "fmt" "net/http" @@ -14,6 +13,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/cors" + "github.com/go-json-experiment/json" "github.com/go-logr/logr" "github.com/jmoiron/sqlx" "github.com/spf13/cobra" @@ -151,8 +151,7 @@ func (server *dbServer) responseError(w http.ResponseWriter, err error) { func (server *dbServer) responseData(w http.ResponseWriter, data interface{}, statusCode int) { w.WriteHeader(statusCode) - enc := json.NewEncoder(w) - if encodeErr := enc.Encode(data); encodeErr != nil { + if encodeErr := json.MarshalWrite(w, data); encodeErr != nil { server.logger.Error(encodeErr, "failed to write response") w.WriteHeader(http.StatusInternalServerError) return