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) } }