-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
51 lines (40 loc) · 1.14 KB
/
encoding.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
/*
* Author: fasion
* Created time: 2023-11-03 10:39:49
* Last Modified by: fasion
* Last Modified time: 2023-11-03 11:15:43
*/
package baseutils
import "encoding/json"
type JsonAny = any
type JsonObject = map[string]JsonAny
type JsonMap = JsonObject
type JsonArray = []JsonAny
type JsonRawMessage struct {
json.RawMessage
}
func (message JsonRawMessage) UnmarshalTo(dst interface{}) error {
return json.Unmarshal(message.RawMessage, dst)
}
func (message JsonRawMessage) UnmarshalAsString() (string, error) {
return ParseJsonRawMessage[string](message)
}
func (message JsonRawMessage) UnmarshalAsInt() (int, error) {
return ParseJsonRawMessage[int](message)
}
func ParseJsonRawMessageAsPointer[Ptr *Data, Data any](message JsonRawMessage) (Ptr, error) {
var data Data
if err := message.UnmarshalTo(&data); err != nil {
return nil, err
}
return &data, nil
}
func ParseJsonRawMessage[Data any](message JsonRawMessage) (data Data, err error) {
err = message.UnmarshalTo(&data)
return
}
type JsonRawMessageMap map[string]JsonRawMessage
func (m JsonRawMessageMap) Lookup(key string) (JsonRawMessage, bool) {
v, ok := m[key]
return v, ok
}