-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔇 silent changes: add jsonN core functions #5
- Loading branch information
1 parent
35ff85e
commit 5f8830b
Showing
1 changed file
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} |