-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
38 lines (32 loc) · 811 Bytes
/
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
package entx
import (
"encoding/json"
"io"
"github.com/99designs/gqlgen/graphql"
)
// MarshalRawMessage provides a graphql.Marshaler for json.RawMessage
func MarshalRawMessage(t json.RawMessage) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
s, _ := t.MarshalJSON()
io.Writer.Write(w, s) // nolint:errcheck
})
}
// UnmarshalRawMessage provides a graphql.Unmarshaler for json.RawMessage
func UnmarshalRawMessage(v interface{}) (json.RawMessage, error) {
switch j := v.(type) {
case []byte:
return json.RawMessage(j), nil
case map[string]interface{}:
js, err := json.Marshal(v)
if err != nil {
return nil, err
}
return json.RawMessage(js), nil
default:
js, err := json.Marshal(v)
if err != nil {
return nil, err
}
return json.RawMessage(js), nil
}
}