Some checks failed
Tests and linters / Lint (pull_request) Failing after 27s
DCO action / DCO (pull_request) Failing after 51s
Tests and linters / Tests (1.19) (pull_request) Successful in 50s
Tests and linters / Tests (1.20) (pull_request) Successful in 1m3s
Tests and linters / Tests with -race (pull_request) Successful in 1m7s
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
37 lines
913 B
Go
37 lines
913 B
Go
package encoding
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"google.golang.org/grpc/encoding"
|
|
)
|
|
|
|
// JSONCodec is easyjson codec used for code generated by protogen.
|
|
// It is binary-level compatible with the standard protojson format, thus uses the same name.
|
|
type JSONCodec struct{}
|
|
|
|
var _ encoding.Codec = JSONCodec{}
|
|
|
|
// Name implements the encoding.Codec interface.
|
|
func (JSONCodec) Name() string { return "json" }
|
|
|
|
// Marshal implements the encoding.Codec interface.
|
|
func (JSONCodec) Marshal(v any) ([]byte, error) {
|
|
switch v := v.(type) {
|
|
case json.Marshaler:
|
|
return json.Marshal(v)
|
|
default:
|
|
return nil, fmt.Errorf("unknown message type: %T", v)
|
|
}
|
|
}
|
|
|
|
// Unmarshal implements the encoding.Codec interface.
|
|
func (JSONCodec) Unmarshal(data []byte, v any) error {
|
|
switch v := v.(type) {
|
|
case json.Unmarshaler:
|
|
return json.Unmarshal(data, v)
|
|
default:
|
|
return fmt.Errorf("unknown message type: %T", v)
|
|
}
|
|
}
|