-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
63 lines (51 loc) · 1.37 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package jsonpb
import (
"github.com/hopeio/cherry/utils/encoding/json"
responsei "github.com/hopeio/cherry/utils/net/http"
"google.golang.org/protobuf/types/known/wrapperspb"
"io"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hopeio/cherry/protobuf/response"
)
var JsonPb = &JSONPb{}
type JSONPb struct {
}
func (*JSONPb) ContentType(_ interface{}) string {
return "application/json"
}
func (j *JSONPb) Marshal(v any) ([]byte, error) {
if _, ok := v.(error); ok {
return json.Marshal(v)
}
if msg, ok := v.(*wrapperspb.StringValue); ok {
v = msg.Value
}
return json.Marshal(&responsei.ResAnyData{
Code: 0,
Message: "OK",
Details: v,
})
}
func (j *JSONPb) Name() string {
return "jsonpb"
}
func (j *JSONPb) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (j *JSONPb) Delimiter() []byte {
return []byte("\n")
}
// NewDecoder returns a runtime.Decoder which reads JSON stream from "r".
func (j *JSONPb) NewDecoder(r io.Reader) runtime.Decoder {
return json.NewDecoder(r)
}
// NewEncoder returns an Encoder which writes JSON stream into "w".
func (j *JSONPb) NewEncoder(w io.Writer) runtime.Encoder {
return json.NewEncoder(w)
}
func (j *JSONPb) ContentTypeFromMessage(v interface{}) string {
if httpBody, ok := v.(*response.HttpResponse); ok {
return httpBody.GetContentType()
}
return j.ContentType(v)
}