-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
54 lines (47 loc) · 1.43 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
// SPDX-License-Identifier: 0BSD
package sx
import "encoding/json"
// Tries to convert object to a compact/dense json string
func ToJson(object any) Result[string] {
var bytes, err = json.Marshal(object)
if err != nil {
return NewResultFromError[string](err)
}
return NewResultFrom(string(bytes))
}
// Tries to convert object to a pretty-printed json string
//
// recommended indentationStrings are "\t" or multiple spaces
func ToJsonPretty(object any, indentationString string) Result[string] {
var bytes, err = json.MarshalIndent(object, "", indentationString)
if err != nil {
return NewResultFromError[string](err)
}
return NewResultFrom(string(bytes))
}
// Tries to convert a json string into the specified object
func FromJson[T any](jsonString string) Result[T] {
var object T
var err = json.Unmarshal([]byte(jsonString), &object)
if err != nil {
var e = err.Error()
_ = e
return NewResultFromError[T](err)
}
return NewResultFrom(object)
}
func FromJsonInterface[T any](jsonString string, object *T) Result[T] {
var err = json.Unmarshal([]byte(jsonString), object)
if err != nil {
var e = err.Error()
_ = e
return NewResultFromError[T](err)
}
return NewResultFrom(*object)
}
func (m hashMapImpl[K, V]) MarshalJSON() ([]byte, error) {
return json.Marshal(m.Map)
}
func (m hashMapImpl[K, V]) UnmarshalJSON(bytes []byte) error {
return json.Unmarshal(bytes, &m.Map)
}