Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NewJson should fail, but it does not #91

Open
ling-zhou opened this issue Jan 28, 2021 · 1 comment
Open

NewJson should fail, but it does not #91

ling-zhou opened this issue Jan 28, 2021 · 1 comment

Comments

@ling-zhou
Copy link

ling-zhou commented Jan 28, 2021


import (
        "fmt"
        sjson "github.com/bitly/go-simplejson"
)

func main() {
        body := `"{"client_id":"abc123","client_ip":"59.37.125.15","client_version":"1"}"`
        // body := `{"client_id":"abc123","client_ip":"59.37.125.15","client_version":"1"}`

        js, err := sjson.NewJson([]byte(body))
        if err != nil {
                fmt.Printf("failed to decode json(%s): %v", body, err) // should fail, but it does not!
                return
        }

        fmt.Printf("js: (%v)\n", js)
        return
}
@ling-zhou ling-zhou changed the title NewJson should fail, but it dit not NewJson should fail, but it does not Jan 28, 2021
@phanirithvij
Copy link

I think the issue is in the standard library itself

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
)
func main() {
	body := `"{"client_id":"abc123","client_ip":"59.37.125.15","client_version":"1"}"`
	// body := `{"client_id":"abc123","client_ip":"59.37.125.15","client_version":"1"}`

        // what we normally do (works as expected with an error)
	var x interface{}
	err := json.Unmarshal([]byte(body), &x)
	if err != nil {
		fmt.Printf("Err: %v\n", err)
	}
	fmt.Printf("x: (%v)\n", x)

        // the code used by this library to decode (same standard lib but it succeeds)
	var y interface{}
	dec := json.NewDecoder(bytes.NewReader([]byte(body)))
	err = dec.Decode(&y)
	if err != nil {
		fmt.Printf("Err: %v\n", err)
	}
	fmt.Printf("y: (%v)\n", y)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants