goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.13k stars 148 forks source link

Incompatibility with the standard library #467

Open sitnikovv opened 1 year ago

sitnikovv commented 1 year ago

big.Int values are incorrectly encoded:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "math/big"

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

func main() {
    a := big.NewInt(123567890)
    for i := 0; i < 6; i++ {
        a = a.Mul(a, a)
    }
    buf := bytes.Buffer{}
    enc := json.NewEncoder(&buf)
    if err := enc.Encode(a); err != nil {
        fmt.Println("failed to encode standard lib, err:", err)
        return
    }
    fmt.Println("Standard library OK")

    buf = bytes.Buffer{}
    encGo := gojson.NewEncoder(&buf)
    if err := encGo.Encode(a); err != nil {
        fmt.Println("failed to encode gojson, err:", err)
    }
}
Standard library OK
failed to encode gojson, err: json: error calling MarshalJSON for type *big.Int: strconv.ParseFloat: parsing "76200948414809119462531517826850960536131686363643267779709124660654187935282503001491794421771859987339009662441641350849331462938930835750824029814149852045881073085880629334064792752258111170220340054386254402830248902603478625028789666943107329593265071439381436119961510836736786097741991680557441102980977291137567585278607104182762284525606679219639314814207975210733462121321620125633045954506623680366327265258003639207705816681123566475526950410000000000000000000000000000000000000000000000000000000000000000": value out of range