Skip to content

Different behaviour from encoding/json  #296

Closed
@harjas27

Description

@harjas27

When processing a batch of comma-separated json, where one/more are incorrect(error expected):

  • encoding/json - gives error for the incorrect ones, processes the rest
  • go-json - goes into an infinite loop as soon as it is unable to decode an input

sample program:

package main

import (
	stdjson "encoding/json"
	"fmt"
	"strings"

	goccy_json "github.com/goccy/go-json"
)

type Test struct {
	Val int `json:"val"`
}

func main() {
	p := Test{}

	decoder := stdjson.NewDecoder(strings.NewReader("[12,{\"val\" : 2}]"))
	decoder.Token()
	for decoder.More() {
		err := decoder.Decode(&p)
		fmt.Println("StdJSON:", err)
		fmt.Println("Value:", p)
	}

	decoder2 := goccy_json.NewDecoder(strings.NewReader("[12,{\"val\" : 2}]"))
	decoder2.Token()
	for decoder2.More() {
		err := decoder2.Decode(&p)
		fmt.Println("Goccy JSON:", err)
		fmt.Println("Value:", p)
	}

}
  • output for encoding/json :
StdJSON: json: cannot unmarshal number into Go value of type main.Test
Value: {0}
StdJSON: <nil>
Value: {2}
  • output for go-json : infinite loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions