Single framework to parse and dynamically create/modify Json objects.
go get github.com/libujacob/jsone
To create this:
{
"name":"Ricardo Longa",
"idade":28,
"owner":true,
"skills":[
"Golang",
"Android"
]
}
Do this:
import (
j "github.com/libujacob/jsone"
)
json := j.Object().Put("name", "Ricardo Longa").
Put("idade", 28).
Put("owner", true).
Put("skills", j.Array().Put("Golang").
Put("Android"))
log.Println(json.Indent())
log.Println(json.String())
json.Indent()
json.String()
json.Remove("skills")
json.Get("skills") // Return is interface{}.
skill, err := json.GetString("skills") // Return is string, error
count, err := json.GetInt("count") // Return is int, error
bytes, err := json.GetInt64("bytes") // Return is int64, error
average, err := json.GetFloat64("average") // Return is float64, error
isDownSupport, err := json.GetBoolean("isDownloadSupported") // Return is boolean, error
if json.Has("operations") { // Return is boolean
//do something
}
results := Array().Put("Golang").Put("Android").Put("Java")
for i, result := range results.Array() {
}
array := j.Array().Put("Android").
Put("Golang").
Put("Java")
array.Size() // Result is 3.
Json can be directly of Object or Array type. Both can be parsed using two different APIs which are mentioned below. After parsing you can use all the above said operations on the return value.
import (
j "github.com/libujacob/jsone"
)
parsedObject := j.ParseJsonObject([]byte(`{"type": "oper", "nameList":["John", "Dan"], "id":205896}`))
/*{
"type": "oper",
"nameList": [
"John",
"Dan"
],
"id": 205896
}*/
parsedObject.Put("dept", "Operations")
/*{
"type": "oper",
"nameList": [
"John",
"Dan"
],
"id": 205896,
"dept": "Operations"
}*/
import (
j "github.com/libujacob/jsone"
)
parsedArray := j.ParseJsonArray([]byte(`[{"name": "John", "id": 567314}, {"name": "Dan", "id": 589725}]`))
/*[
{
"name": "John",
"id": 567314
},
{
"name": "Dan",
"id": 589725
}
]*/
parsedArray.Put(j.Object().Put("name", "Tom").Put("id", 589289)
/*[
{
"name": "John",
"id": 567314
},
{
"name": "Dan",
"id": 589725
},
{
"name": "Tom",
"id": 589289
}
]*/
Original work Copyright (c) 2015 Ricardo Longa.
Modified work Copyright (c) 2020 Libu Jacob Varghese.
Using bramp.net/antlr4/json and antlr for json parsing.
JsonE is licensed under the Apache License Version 2.0. See the LICENSE file for more information.