mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-19 15:01:22 +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:
60
pkg/json/special.go
Normal file
60
pkg/json/special.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
|
||||
func isIntFloat(t reflect.Kind) bool {
|
||||
return t >= reflect.Bool && t <= reflect.Float64
|
||||
}
|
||||
|
||||
func appendStringRepr(v reflect.Value, buf []byte) []byte { // for json tag `string`
|
||||
kind := v.Kind()
|
||||
if isIntFloat(kind) {
|
||||
marshalFunc, _ := marshalFuncByKind[kind]
|
||||
buf = append(buf, '"')
|
||||
buf = marshalFunc(v, buf)
|
||||
buf = append(buf, '"')
|
||||
return buf
|
||||
}
|
||||
switch vv := v.Interface().(type) {
|
||||
case fmt.Stringer:
|
||||
buf = AppendString(buf, vv.String())
|
||||
case encoding.TextMarshaler:
|
||||
buf = append(buf, must(vv.MarshalText())...)
|
||||
case encoding.TextAppender:
|
||||
buf = must(vv.AppendText(buf))
|
||||
default:
|
||||
panic(fmt.Errorf("tag %q used but type is non-stringable: %s", tagString, v.Type()))
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
func appendTime(v reflect.Value, buf []byte) []byte {
|
||||
buf = append(buf, '"')
|
||||
buf = strutils.AppendTime(v.Interface().(time.Time), buf)
|
||||
return append(buf, '"')
|
||||
}
|
||||
|
||||
func appendDuration(v reflect.Value, buf []byte) []byte {
|
||||
buf = append(buf, '"')
|
||||
buf = strutils.AppendDuration(v.Interface().(time.Duration), buf)
|
||||
return append(buf, '"')
|
||||
}
|
||||
|
||||
func appendByteSize(v reflect.Value, buf []byte) []byte {
|
||||
buf = append(buf, '"')
|
||||
buf = strutils.AppendByteSize(v.Interface().(uint64), buf)
|
||||
return append(buf, '"')
|
||||
}
|
||||
|
||||
func appendUnixTime(v reflect.Value, buf []byte) []byte {
|
||||
buf = append(buf, '"')
|
||||
buf = strutils.AppendTime(time.Unix(v.Interface().(int64), 0), buf)
|
||||
return append(buf, '"')
|
||||
}
|
||||
Reference in New Issue
Block a user