Files
jaeson/README.md
T
2026-09-30 09:47:02 +05:30

180 lines
4.5 KiB
Markdown

# jaeson
Arbitrary JSON parser in Go
Supports following data types:
1. JSONObject (a JSON Object)
2. JSONArray (a JSON Array)
3. string
4. int
5. bool
## Usage
```
package main
import (
"fmt"
"gitea.cozeon.cloud/anand.ekka-ext/jaeson"
)
func main(){
jsondata := `{"twenty-point-two":20.2,"one":"1","two":2,"three":{"ten":"10","eleven":11},"four":["4","5","6"],"boolkey1":true}`
sampleObj, _ := jaeson.UnmarshalJSON(jsondata)
fmt.Println("...sampleObj.KeySetSize(): ", sampleObj.KeySetSize())
fmt.Println("...sampleObj: ", sampleObj)
fmt.Println("...sampleObj.StringJCS(): ", sampleObj.StringJCS())
sVlOne,errOne := sampleObj.GetString("one")
if errOne != nil {
fmt.Println("...errOne:", errOne)
}else{
fmt.Println("...GetString(one): ", sVlOne)
}
iVlTwo,errTwo := sampleObj.GetInt("two")
if errTwo != nil {
fmt.Println("...errTwo:", errTwo)
}else{
fmt.Println("...GetInt(two): ", iVlTwo)
}
nmVlTwo,errnmTwo := sampleObj.GetInt("two")
if errnmTwo != nil {
fmt.Println("...errnmTwo:", errnmTwo)
}else{
fmt.Println("...GetNumber(two): ", nmVlTwo)
}
fltVl,errflt := sampleObj.GetFloat("twenty-point-two")
if errflt != nil {
fmt.Println("...errflt:", errflt)
}else{
fmt.Println("...GetFloat(twenty-point-two): ", fltVl)
}
obThree,errobThree := sampleObj.GetJSONObject("three")
if errobThree != nil {
fmt.Println("...errobThree:", errobThree)
}else{
fmt.Println("...GetJSONObject(three):", obThree)
}
arFour,errarFour := sampleObj.GetJSONArray("four")
if errarFour != nil {
fmt.Println("...GetJSONArray(four):", errarFour)
}else{
fmt.Println("...GetJSONArray(four):", arFour)
}
// retrieve value of a non-existent key
blnKey1,errBool := sampleObj.GetBool("boolkey12")
if errBool != nil {
fmt.Println("...GetBool(boolkey12):", errBool)
}else{
fmt.Println("...GetBool(boolkey12):", blnKey1)
}
arFive,errarFive := sampleObj.GetJSONArray("five")
if errarFive != nil {
fmt.Println("...GetJSONArray(five):", errarFive)
}else{
fmt.Println("...GetJSONArray(five):", arFive)
}
// retrieve value of an existing key, of non-coresponding/non-matching type
// would return an error
strKey2,errstr2 := sampleObj.GetString("boolkey1")
if errstr2 != nil {
fmt.Println("...GetString(boolkey1):", errstr2)
}else{
fmt.Println("...GetString(boolkey1):", strKey2)
}
// would return an error
fltVl2,errflt2 := sampleObj.GetFloat("boolkey1")
if errflt2 != nil {
fmt.Println("...GetFloat(boolkey1):", errflt2)
}else{
fmt.Println("...GetFloat(boolkey1):", fltVl2)
}
// create a json object
obSix := jaeson.NewJSONObject()
obSix.PutString("sixty", "60")
obSix.PutString("seventy", "70")
obSix.PutBool("boolVal", true)
obSix.PutInt("intVal", 61)
obSix.PutInt("intVal02", 650)
obSix.PutFloat("floatVal", 62.123456789)
// add created json object within an existing json object
sampleObj.PutJSONObject("six", obSix)
// create a json array
arEight := jaeson.NewJSONArray()
arEight.PutString("eighty")
arEight.PutString("80")
arEight.PutInt(81)
arEight.PutBool(true)
arEight.PutFloat(72.123456789)
// add created json array within an existing json object
sampleObj.PutJSONArray("eight", arEight)
// remove an existing key,value
errRemi := obSix.RemoveInt("intVal02")
if errRemi != nil {
fmt.Println("...RemoveInt(intVal02):", errRemi)
}
errRemjsobthr := sampleObj.RemoveJSONObject("three")
if errRemjsobthr != nil {
fmt.Println("...RemoveJSONObject(three):", errRemjsobthr)
}
// removing an existing key,value (bool) with "RemoveInt"
// would return an error
errRemi2 := obSix.RemoveInt("boolVal")
if errRemi2 != nil {
fmt.Println("...RemoveInt(boolVal):", errRemi2)
}
// removing an existing key, value (int) with "RemoveBool"
// would return an error
errRembln := obSix.RemoveBool("intVal")
if errRembln != nil {
fmt.Println("...RemoveBool(intVal):", errRembln)
}
// removing an existing key, value (bool) with "RemoveFloat"
// would return an error
errRemflt := obSix.RemoveFloat("boolVal")
if errRemflt != nil {
fmt.Println("...RemoveFloat(boolVal):", errRemflt)
}
// works for removing any existing key, value
errRemany := obSix.RemoveAny("intVal")
if errRemany != nil {
fmt.Println("...RemoveAny(intVal):", errRemany)
}
// would return an error for a non-existent key
errRemany2 := obSix.RemoveAny("intValnonexis")
if errRemany2 != nil {
fmt.Println("...RemoveAny(intValnonexis):", errRemany2)
}
// print
fmt.Println("...sampleObj.KeySetSize(): ", sampleObj.KeySetSize())
fmt.Println("...sampleObj.String(): ", sampleObj)
fmt.Println("...sampleObj.StringJCS: ", sampleObj.StringJCS())
}
```
---