https://github.com/nohupped/simplejson
https://github.com/nohupped/simplejson
golang json parser simplejson wrapper-library
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nohupped/simplejson
- Owner: nohupped
- License: mit
- Created: 2020-02-09T09:05:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-02T03:25:07.000Z (about 6 years ago)
- Last Synced: 2024-11-12T21:23:10.442Z (over 1 year ago)
- Topics: golang, json, parser, simplejson, wrapper-library
- Language: Go
- Size: 49.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# { SimpleJSON } [](https://travis-ci.com/nohupped/simplejson) [](https://codecov.io/github/nohupped/simplejson?branch=master) [](https://goreportcard.com/report/github.com/nohupped/simplejson)
A lame attempt to re-create python's `json.loads()` using `Get()` methods to retrive a key by moving the ugly typeassertions to the library.
## Why
This is just a wrapper over go's `encoding/json`. This uses an `interface{}` to unmarshal the json and the exposed interface method `Get()` uses `type assertions` internally to get the value of a key if it is a hashmap or the index if it is an array. Since this moves the ugly typeassertions for nested json parsing inside the library, the code becomes easier to read.
## Example
Consider the json string
```go
sampleJSON := `{
"Actors":
[
{
"name": "Tom Cruise",
"age": 56,
"Born At": "Syracuse, NY",
"Birthdate": "July 3, 1962",
"hasChildren":true,
"hasTwitterAccount":true,
"hasGreyHair":false,
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
},
{
"name": "Robert Downey Jr.",
"age": 53,
"Born At": "New York City, NY",
"Birthdate": "April 4, 1965",
"photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
]
}`
```
This can be parsed and values can be fetched as
```go
d, err := simplejson.Loads(sampleJSON)
if err != nil {
panic(err)
}
data := d.Get("Actors").Get("", 0).Get("name")
fmt.Println(data) // outputs Tom Cruise
```
Consider the json file `samplejson.json` in this repository.
This file can be parsed and the values can be fetched as
```go
fd, err := os.Open("samplejson.json")
if err != nil {
panic(err)
}
d1, err := Load(fd)
if err != nil{
panic(err)
}
t.Logf("%s", d1.Get("", 2).Get("tags").Get("", 3))
fd.Close()
```
It is the responsibility of the caller to close the file descriptor.
Because this uses `interface{}` to unmarshal json, the structure of json is not required to be predefined. Each `Get()` evaluates the type and extracts accordingly.
## `Get()` function ~~panics~~ returns `nil` for the method `Bytes()` if the key is not present
Instead of returning an error, `Get()` function ~~panics~~ returns `nil` for the `Bytes()` method when trying to fetch an invalid key or trying to read from an invalid index. This is done to easily chain the method as follows
```go
Get("foo").Get("bar").Get("", 2)
```
without having to evaluate for errors every time or to have to write a `recover` as below.
```go
defer func() {
if r := recover(); r != nil {...}
```
Evaluate for `nil` value before using it.
Eg:
```go
d, err := simplejson.Loads([]byte(sampleJSON))
if err != nil {
...
}
d1 := d.Get("Actors").Get("", 0).Get("names").Get("foo").Get("Bar")
if d1.Bytes != nil {
...
}
...
```
## `String()` function returns empty string `""` for `nil` values
This is done to avoid the
```bash
panic: runtime error: invalid memory address or nil pointer dereference
```
at runtime. Evaluate for `nil` value before applying `String()` method.