Files
wehub-resource-sync 6cf6f95f58
CodeQL / Analyze (push) Has been cancelled
Sync to Gitee / Run (push) Has been cancelled
Go / build & test (1.25.x) (push) Has been cancelled
Go / build & test (1.26.x) (push) Has been cancelled
Lint / resolve module (push) Has been cancelled
Lint / lint module (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:43 +08:00

43 lines
694 B
Go

package json
import (
"encoding/json"
"github.com/go-kratos/kratos/v3/encoding"
)
// Name is the name registered for the json codec.
const Name = "json"
func init() {
encoding.RegisterCodec(codec{})
}
// codec is a Codec implementation with json.
type codec struct{}
func (codec) Marshal(v any) ([]byte, error) {
switch m := v.(type) {
case json.Marshaler:
return m.MarshalJSON()
default:
return json.Marshal(m)
}
}
func (codec) Unmarshal(data []byte, v any) error {
if len(data) == 0 {
return nil
}
switch m := v.(type) {
case json.Unmarshaler:
return m.UnmarshalJSON(data)
default:
return json.Unmarshal(data, m)
}
}
func (codec) Name() string {
return Name
}