-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
45 lines (36 loc) · 1.09 KB
/
json.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
40
41
42
43
44
45
package grpc
import (
"encoding/json"
"io"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
)
// JSONMarshaler is a type which satisfies the grpc-gateway Marshaler interface
type JSONMarshaler struct{}
// ContentType implements gwruntime.Marshaler.
func (j *JSONMarshaler) ContentType() string {
return "application/json"
}
// Marshal implements gwruntime.Marshaler.
func (j *JSONMarshaler) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// NewDecoder implements gwruntime.Marshaler.
func (j *JSONMarshaler) NewDecoder(r io.Reader) gwruntime.Decoder {
return json.NewDecoder(r)
}
// NewEncoder implements gwruntime.Marshaler.
func (j *JSONMarshaler) NewEncoder(w io.Writer) gwruntime.Encoder {
return json.NewEncoder(w)
}
// Unmarshal implements gwruntime.Marshaler.
func (j *JSONMarshaler) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// MustMarshal is a convenience function to marshal an object successfully or panic
func MustMarshal(v interface{}) []byte {
bytes, err := json.Marshal(v)
if err != nil {
panic(err)
}
return bytes
}