Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Errors in parsing simple JSON #4

Copy link
Copy link

Description

@FZambia
Issue body actions

@buger hello, nice package idea, keep up a good work!

Just tried example from readme and got a couple of errors, here is code to reproduce:

package main

import (
    "fmt"

    "github.com/buger/jsonparser"
)

func main() {

    data := []byte(`{
  "person": {
    "married": true
  },
  "company": {
    "name": "Acme",
    "size": 109
  }
}`)

    // If the key doesn't exist it will throw an error
    var size float64 = 0
    if value, _, err := jsonparser.GetNumber(data, "company", "size"); err == nil {
        size = value
    } else {
        fmt.Println(err.Error())
    }
    fmt.Println(size)

    var married bool
    if value, _, err := jsonparser.GetBoolean(data, "person", "married"); err == nil {
        married = value
    } else {
        fmt.Println(err.Error())
    }
    fmt.Println(married)
}

Output:

strconv.ParseFloat: parsing "109\n  ": invalid syntax
0
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/buger/jsonparser.GetBoolean(0xc820058070, 0x61, 0x70, 0xc82003bf28, 0x2, 0x2, 0x0, 0x26, 0x0, 0x0)
    /private/var/www/different/go/gopath/src/github.com/buger/jsonparser/parser.go:286 +0x28a
main.main()
    /private/var/www/different/go/gopath/src/github.com/centrifugal/parse.go:30 +0x2a9
exit status 2

Seems like

  1. some values need proper trimming
  2. I think there must be v[0] on this line

Also example is broken, instead of

// If the key doesn't exist it will throw an error
size := 0
if value, _, err := jsonparser.GetNumber(data, "company", "size"); err != nil {
  size = value
}

There must be sth like this I suppose:

// If the key doesn't exist it will throw an error
var size float64 = 0
if value, _, err := jsonparser.GetNumber(data, "company", "size"); err == nil {
  size = value
}

Also I found confusing return in tests

Sorry for not sending pr!

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.