-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonx.go
165 lines (157 loc) · 5.39 KB
/
jsonx.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package unify4g
import (
"encoding/json"
)
// MarshalN converts a Go value into its JSON byte representation.
//
// This function marshals the input value `v` using the standard json library.
// The resulting JSON data is returned as a byte slice. If there is an error
// during marshalling, it returns the error.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
//
// Returns:
// - A byte slice containing the JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
//
// jsonData, err := MarshalN(myStruct)
func MarshalN(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// MarshalIndentN converts a Go value to its JSON string representation with indentation.
//
// This function marshals the input value `v` into a formatted JSON string,
// allowing for easy readability by including a specified prefix and indentation.
// It returns the resulting JSON byte slice or an error if marshalling fails.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
// - `prefix`: A string that will be prefixed to each line of the output JSON.
// - `indent`: A string used for indentation (typically a series of spaces or a tab).
//
// Returns:
// - A byte slice containing the formatted JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
//
// jsonIndented, err := MarshalIndentN(myStruct, "", " ")
func MarshalIndentN(v interface{}, prefix, indent string) ([]byte, error) {
return json.MarshalIndent(v, prefix, indent)
}
// MarshalToStringN converts a Go value to its JSON string representation.
//
// This function utilizes the standard json library to marshal the input value `v`
// into a JSON string. If the marshalling is successful, it returns the resulting
// JSON string. If an error occurs during the process, it returns an error.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
//
// Returns:
// - A string containing the JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
//
// jsonString, err := MarshalToStringN(myStruct)
func MarshalToStringN(v interface{}) (string, error) {
data, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(data), nil
}
// UnmarshalN parses JSON-encoded data and stores the result in the value pointed to by `v`.
//
// This function uses the standard json library to unmarshal JSON data
// (given as a byte slice) into the specified Go value `v`. If the unmarshalling
// is successful, it populates the value `v`. If an error occurs, it returns the error.
//
// Parameters:
// - `data`: A byte slice containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
//
// Returns:
// - An error if the unmarshalling fails.
//
// Example:
//
// err := UnmarshalN(jsonData, &myStruct)
func UnmarshalN(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// UnmarshalFromStringN parses JSON-encoded string and stores the result in the value pointed to by `v`.
//
// This function utilizes the standard json library to unmarshal JSON data
// from a string into the specified Go value `v`. If the unmarshalling is
// successful, it populates the value `v`. If an error occurs, it returns the error.
//
// Parameters:
// - `str`: A string containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
//
// Returns:
// - An error if the unmarshalling fails.
//
// Example:
//
// err := UnmarshalFromStringN(jsonString, &myStruct)
func UnmarshalFromStringN(str string, v interface{}) error {
return json.Unmarshal([]byte(str), v)
}
// JsonN converts a Go value to its JSON string representation or returns the value directly if it is already a string.
//
// This function checks if the input data is a string; if so, it returns it directly.
// Otherwise, it marshals the input value `data` into a JSON string using the
// MarshalToString function. If an error occurs during marshalling, it returns an empty string.
//
// Parameters:
// - `data`: The Go value to be converted to JSON, or a string to be returned directly.
//
// Returns:
// - A string containing the JSON representation of the input value, or an empty string if an error occurs.
//
// Example:
//
// jsonStr := JsonN(myStruct)
func JsonN(data interface{}) string {
s, ok := data.(string)
if ok {
return s
}
result, err := MarshalToStringN(data)
if err != nil {
return ""
}
return result
}
// JsonPrettyN converts a Go value to its pretty-printed JSON string representation or returns the value directly if it is already a string.
//
// This function checks if the input data is a string; if so, it returns it directly.
// Otherwise, it marshals the input value `data` into a formatted JSON string using
// the MarshalIndent function. If an error occurs during marshalling, it returns an empty string.
//
// Parameters:
// - `data`: The Go value to be converted to pretty-printed JSON, or a string to be returned directly.
//
// Returns:
// - A string containing the pretty-printed JSON representation of the input value, or an empty string if an error occurs.
//
// Example:
//
// jsonPrettyStr := JsonPrettyN(myStruct)
func JsonPrettyN(data interface{}) string {
s, ok := data.(string)
if ok {
return s
}
result, err := MarshalIndentN(data, "", " ")
if err != nil {
return ""
}
return string(result)
}