-
Notifications
You must be signed in to change notification settings - Fork 134
/
json_marshaller.go
39 lines (29 loc) · 1010 Bytes
/
json_marshaller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package protoutil
import (
"encoding/json"
"io"
"github.com/coreos/pkg/httputil"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
)
// Minimal implementation of marshaller using default json package, this assumes all proto messages can be nicely marshalled with it
// For kubernetes objects this works much better then default JSONpb
// To handle one of feature of protocol bufers custom json marshaller is required
type JSONMarshaller struct{}
func (*JSONMarshaller) ContentType() string {
return httputil.JSONContentType
}
func (j *JSONMarshaller) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (j *JSONMarshaller) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (j *JSONMarshaller) NewDecoder(r io.Reader) gwruntime.Decoder {
return json.NewDecoder(r)
}
func (j *JSONMarshaller) NewEncoder(w io.Writer) gwruntime.Encoder {
return json.NewEncoder(w)
}
func (*JSONMarshaller) Delimiter() []byte {
return []byte("\n")
}