type Foo struct {
Name string
Age int
Address string
}
func main() {
foo := Foo{"someone", 25, "earth"}
tmp := jsonbuilder.From(foo)
tmp.
Begin("family").
/* */ Set("father", "his_father").
/* */ Set("mother", "his_mother").
/* */ Set("brothers", jsonbuilder.Array(
/* */ /* */ "brother_1",
/* */ /* */ "brother_2",
/* */ /* */ "brother_3")).
End()
tmp.Enter("family").Set("sisters", "sister_1", "sister_2", "sister_3")
tmp.Dive("family", "brothers").Set(2, jsonbuilder.Array("cousin_1", "cousin_2"))
tmp.Enter("family").Enter("brothers").Enter(2).Delete(0).Leave().Delete(1)
fmt.Println(tmp.MarshalPretty())
}
will give you:
{
"Address": "earth",
"Age": 25,
"Name": "someone",
"family": {
"brothers": [
"brother_1",
[
"cousin_2"
]
],
"father": "his_father",
"mother": "his_mother",
"sisters": [
"sister_1",
"sister_2",
"sister_3"
]
}
}
The code is pretty self-explanatory but there are 3 things to note:
Enter(n)
andBegin(n)
are the same, so areLeave()
andEnd()
. They are for writing better readable codes. If you tend to enter many levels, useDive(n...)
.Set(k, v...)
accepts multiple arguments. The 1st one is the key or index based on the type ofk
(string
orint
) and the rest are values. if there are multiple values, they will automatically form an array.Delete(n)
deletes a key from an object or an element from an array based on the type ofn
(string
orint
).