|
| 1 | +package unify4go |
| 2 | + |
| 3 | +import ( |
| 4 | + jsonI "github.com/json-iterator/go" |
| 5 | +) |
| 6 | + |
| 7 | +var _json = jsonI.ConfigCompatibleWithStandardLibrary |
| 8 | + |
| 9 | +// Marshal converts a Go value into its JSON byte representation. |
| 10 | +// |
| 11 | +// This function marshals the input value `v` using the json-iterator library. |
| 12 | +// The resulting JSON data is returned as a byte slice. If there is an error |
| 13 | +// during marshalling, it returns the error. |
| 14 | +// |
| 15 | +// Parameters: |
| 16 | +// - `v`: The Go value to be marshalled into JSON. |
| 17 | +// |
| 18 | +// Returns: |
| 19 | +// - A byte slice containing the JSON representation of the input value. |
| 20 | +// - An error if the marshalling fails. |
| 21 | +// |
| 22 | +// Example: |
| 23 | +// jsonData, err := Marshal(myStruct) |
| 24 | +func Marshal(v interface{}) ([]byte, error) { |
| 25 | + return _json.Marshal(v) |
| 26 | +} |
| 27 | + |
| 28 | +// MarshalIndent converts a Go value to its JSON string representation with indentation. |
| 29 | +// |
| 30 | +// This function marshals the input value `v` into a formatted JSON string, |
| 31 | +// allowing for easy readability by including a specified prefix and indentation. |
| 32 | +// It returns the resulting JSON byte slice or an error if marshalling fails. |
| 33 | +// |
| 34 | +// Parameters: |
| 35 | +// - `v`: The Go value to be marshalled into JSON. |
| 36 | +// - `prefix`: A string that will be prefixed to each line of the output JSON. |
| 37 | +// - `indent`: A string used for indentation (typically a series of spaces or a tab). |
| 38 | +// |
| 39 | +// Returns: |
| 40 | +// - A byte slice containing the formatted JSON representation of the input value. |
| 41 | +// - An error if the marshalling fails. |
| 42 | +// |
| 43 | +// Example: |
| 44 | +// jsonIndented, err := MarshalIndent(myStruct, "", " ") |
| 45 | +func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { |
| 46 | + return _json.MarshalIndent(v, prefix, indent) |
| 47 | +} |
| 48 | + |
| 49 | +// MarshalToString converts a Go value to its JSON string representation. |
| 50 | +// |
| 51 | +// This function utilizes the json-iterator library to marshal the input value `v` |
| 52 | +// into a JSON string. If the marshalling is successful, it returns the resulting |
| 53 | +// JSON string. If an error occurs during the process, it returns an error. |
| 54 | +// |
| 55 | +// Parameters: |
| 56 | +// - `v`: The Go value to be marshalled into JSON. |
| 57 | +// |
| 58 | +// Returns: |
| 59 | +// - A string containing the JSON representation of the input value. |
| 60 | +// - An error if the marshalling fails. |
| 61 | +// |
| 62 | +// Example: |
| 63 | +// jsonString, err := MarshalToString(myStruct) |
| 64 | +func MarshalToString(v interface{}) (string, error) { |
| 65 | + return _json.MarshalToString(v) |
| 66 | +} |
| 67 | + |
| 68 | +// Unmarshal parses JSON-encoded data and stores the result in the value pointed to by `v`. |
| 69 | +// |
| 70 | +// This function uses the json-iterator library to unmarshal JSON data |
| 71 | +// (given as a byte slice) into the specified Go value `v`. If the unmarshalling |
| 72 | +// is successful, it populates the value `v`. If an error occurs, it returns the error. |
| 73 | +// |
| 74 | +// Parameters: |
| 75 | +// - `data`: A byte slice containing JSON data to be unmarshalled. |
| 76 | +// - `v`: A pointer to the Go value where the unmarshalled data will be stored. |
| 77 | +// |
| 78 | +// Returns: |
| 79 | +// - An error if the unmarshalling fails. |
| 80 | +// |
| 81 | +// Example: |
| 82 | +// err := Unmarshal(jsonData, &myStruct) |
| 83 | +func Unmarshal(data []byte, v interface{}) error { |
| 84 | + return _json.Unmarshal(data, v) |
| 85 | +} |
| 86 | + |
| 87 | +// UnmarshalFromString parses JSON-encoded string and stores the result in the value pointed to by `v`. |
| 88 | +// |
| 89 | +// This function utilizes the json-iterator library to unmarshal JSON data |
| 90 | +// from a string into the specified Go value `v`. If the unmarshalling is |
| 91 | +// successful, it populates the value `v`. If an error occurs, it returns the error. |
| 92 | +// |
| 93 | +// Parameters: |
| 94 | +// - `str`: A string containing JSON data to be unmarshalled. |
| 95 | +// - `v`: A pointer to the Go value where the unmarshalled data will be stored. |
| 96 | +// |
| 97 | +// Returns: |
| 98 | +// - An error if the unmarshalling fails. |
| 99 | +// |
| 100 | +// Example: |
| 101 | +// err := UnmarshalFromString(jsonString, &myStruct) |
| 102 | +func UnmarshalFromString(str string, v interface{}) error { |
| 103 | + return _json.UnmarshalFromString(str, v) |
| 104 | +} |
| 105 | + |
| 106 | +// Json converts a Go value to its JSON string representation or returns the value directly if it is already a string. |
| 107 | +// |
| 108 | +// This function checks if the input data is a string; if so, it returns it directly. |
| 109 | +// Otherwise, it marshals the input value `data` into a JSON string using the |
| 110 | +// MarshalToString function. If an error occurs during marshalling, it returns an empty string. |
| 111 | +// |
| 112 | +// Parameters: |
| 113 | +// - `data`: The Go value to be converted to JSON, or a string to be returned directly. |
| 114 | +// |
| 115 | +// Returns: |
| 116 | +// - A string containing the JSON representation of the input value, or an empty string if an error occurs. |
| 117 | +// |
| 118 | +// Example: |
| 119 | +// jsonStr := Json(myStruct) |
| 120 | +func Json(data interface{}) string { |
| 121 | + s, ok := data.(string) |
| 122 | + if ok { |
| 123 | + return s |
| 124 | + } |
| 125 | + result, err := MarshalToString(data) |
| 126 | + if err != nil { |
| 127 | + return "" |
| 128 | + } |
| 129 | + return string(result) |
| 130 | +} |
| 131 | + |
| 132 | +// JsonPretty converts a Go value to its pretty-printed JSON string representation or returns the value directly if it is already a string. |
| 133 | +// |
| 134 | +// This function checks if the input data is a string; if so, it returns it directly. |
| 135 | +// Otherwise, it marshals the input value `data` into a formatted JSON string using |
| 136 | +// the MarshalIndent function. If an error occurs during marshalling, it returns an empty string. |
| 137 | +// |
| 138 | +// Parameters: |
| 139 | +// - `data`: The Go value to be converted to pretty-printed JSON, or a string to be returned directly. |
| 140 | +// |
| 141 | +// Returns: |
| 142 | +// - A string containing the pretty-printed JSON representation of the input value, or an empty string if an error occurs. |
| 143 | +// |
| 144 | +// Example: |
| 145 | +// jsonPrettyStr := JsonPretty(myStruct) |
| 146 | +func JsonPretty(data interface{}) string { |
| 147 | + s, ok := data.(string) |
| 148 | + if ok { |
| 149 | + return s |
| 150 | + } |
| 151 | + result, err := MarshalIndent(data, "", " ") |
| 152 | + if err != nil { |
| 153 | + return "" |
| 154 | + } |
| 155 | + return string(result) |
| 156 | +} |
0 commit comments