go-simplejson icon indicating copy to clipboard operation
go-simplejson copied to clipboard

NewJson should fail, but it does not

Open ling-zhou opened this issue 3 years ago • 1 comments


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 avatar Jan 28 '21 07:01 ling-zhou

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)
}

phanirithvij avatar Jul 25 '21 11:07 phanirithvij