mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-22 08:48:43 +02:00
feat: custom json marshaling implementation, replace json and yaml library (#89)
* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic * fix: yaml unmarshal panic * feat: custom json marshaler implementation * chore: fix import and err marshal handling --------- Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"encoding/json"
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
)
|
||||
|
||||
// baseError is an immutable wrapper around an error.
|
||||
@@ -49,6 +49,6 @@ func (err *baseError) Error() string {
|
||||
return err.Err.Error()
|
||||
}
|
||||
|
||||
func (err *baseError) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(err.Err)
|
||||
func (err *baseError) MarshalJSONTo(buf []byte) []byte {
|
||||
return json.MarshalTo(err.Err, buf)
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"encoding/json"
|
||||
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
|
||||
)
|
||||
|
||||
@@ -94,7 +93,7 @@ func (err *withSubject) Error() string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (err *withSubject) MarshalJSON() ([]byte, error) {
|
||||
func (err *withSubject) MarshalJSONTo(buf []byte) []byte {
|
||||
subjects := slices.Clone(err.Subjects)
|
||||
slices.Reverse(subjects)
|
||||
|
||||
@@ -102,5 +101,5 @@ func (err *withSubject) MarshalJSON() ([]byte, error) {
|
||||
"subjects": subjects,
|
||||
"err": err.Err,
|
||||
}
|
||||
return json.Marshal(reversed)
|
||||
return json.MarshalTo(reversed, buf)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"encoding/json"
|
||||
stdJSON "encoding/json"
|
||||
)
|
||||
|
||||
func newError(message string) error {
|
||||
@@ -73,7 +73,7 @@ func IsJSONMarshallable(err error) bool {
|
||||
case *baseError:
|
||||
return IsJSONMarshallable(err.Err)
|
||||
default:
|
||||
var v json.Marshaler
|
||||
var v stdJSON.Marshaler
|
||||
return errors.As(err, &v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user