-
Notifications
You must be signed in to change notification settings - Fork 18
/
json.go
100 lines (88 loc) · 2.86 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package common
import (
"encoding/json"
"fmt"
"strings"
"unicode/utf8"
"github.com/spf13/cobra"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/contracts"
)
// PrettyJSONMarshal returns pretty-printed JSON encoding of v.
func PrettyJSONMarshal(v interface{}) ([]byte, error) {
formatted, err := json.MarshalIndent(v, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal to pretty JSON: %w", err)
}
return formatted, nil
}
// JSONMarshalKey encodes k as UTF-8 string if valid, or Base64 otherwise.
func JSONMarshalKey(k interface{}) (keyJSON []byte, err error) {
keyBytes, ok := k.([]byte)
if ok && utf8.Valid(keyBytes) {
// Marshal valid UTF-8 string.
keyJSON, err = json.Marshal(string(keyBytes))
} else {
// Marshal string or Base64 otherwise.
keyJSON, err = json.Marshal(k)
}
return
}
// JSONPrintKeyValueTuple traverses potentially large number of items and prints JSON representation
// of them.
//
// Marshalling is done externally without holding resulting JSON string in-memory.
// Cbor decoding of each value is tried first. If it fails, the binary content is preserved.
// Universal marshalling of map[interface{}]interface{} types is also supported.
// Each key is encoded as string if it contains valid UTF-8 value. Otherwise, Base64 is used.
func JSONPrintKeyValueTuple(items []contracts.InstanceStorageKeyValue) {
first := true
fmt.Printf("{")
for _, kv := range items {
if !first {
fmt.Printf(",")
}
first = false
var val interface{}
err := cbor.Unmarshal(kv.Value, &val)
if err != nil {
// Value is not CBOR, use raw value instead.
val = kv.Value
}
keyJSON, err := JSONMarshalKey(kv.Key)
cobra.CheckErr(err)
valJSON := JSONMarshalUniversalValue(val)
fmt.Printf("%s:%s", keyJSON, valJSON)
}
fmt.Printf("}\n")
}
// JSONMarshalUniversalValue is a wrapper for the built-in JSON encoder which adds support for
// marshalling map[interface{}]interface{}.
//
// Each key is encoded as string if it contains valid UTF-8 value. Otherwise, Base64 is used.
func JSONMarshalUniversalValue(v interface{}) []byte {
// Try array.
if valTest, ok := v.([]interface{}); ok {
e := make([]string, 0, len(valTest))
for _, v := range valTest {
valJSON := JSONMarshalUniversalValue(v)
e = append(e, string(valJSON))
}
return []byte(fmt.Sprintf("[%s]", strings.Join(e, ",")))
}
// Try universal map.
if valTest, ok := v.(map[interface{}]interface{}); ok {
e := make([]string, 0, len(valTest))
for k, v := range valTest {
keyJSON, err := JSONMarshalKey(k)
cobra.CheckErr(err)
valJSON := JSONMarshalUniversalValue(v)
e = append(e, fmt.Sprintf("%s:%s", keyJSON, valJSON))
}
return []byte(fmt.Sprintf("{%s}", strings.Join(e, ",")))
}
// Primitive type - use built-in JSON encoder.
vJSON, err := json.Marshal(v)
cobra.CheckErr(err)
return vJSON
}