06_import_export.md
May 31, 2025 ยท View on GitHub
Import and Export with encoding/json
Prev Page | Contents | Next Page
Import / Export
The initial purpose of designing Import and Export is to convert data between encoding/json and jsonvalue.
However, the development of Import resulted in many additional features as described below:
Function New()
From v1.3.0, a new function called New is provided. This function receives any kind of parameter (any or interface{} in older versions), then parses it to a *jsonvalue.V value. If the input parameter is invalid, the type of the returned value will be NotExist.
Actually, New is a simple wrapper of the function Import; however, it does not return an error type.
Methods Set, Append, Insert, Add
Before version v1.3.0, when you wanted to add a sub-value into a jsonvalue node, you had to specify the parameter type of the input parameter. For example:
v.SetString("Hello, world").At("greeting")
After v1.3.0, methods like Set, Append, Insert, Add will accept any type of parameter, parse it, and then set the corresponding sub-value.
For example, after creating an empty JSON object:
v := jsonvalue.NewObject()
We can add a sub-object into it:
child := map[string]string{
"text": "Hello, jsonvalue!",
}
v.Set(child).At("child")
fmt.Println(v.MustMarshalString())
Outputs: {"child":{"text":"Hello, jsonvalue!"}}
Or we can set a normal JSON value:
v := jsonvalue.NewObject()
v.Set("Hello, JSON!").At("msg")
fmt.Println(v.MustMarshalString())
// Output: {"msg":"Hello, JSON!"}