square / go-jose

An implementation of JOSE standards (JWE, JWS, JWT) in Go
1.98k stars 276 forks source link

Hope to keep the float type when the fractional part ends with '. 0' by using UnmarshalIntOrFloat #370

Closed Kish29 closed 2 years ago

Kish29 commented 2 years ago

when I use UnmarshalIntOrFloat to decode float number especially when the fractional part ends with '. 0', here is the test:

package main

import (
    "fmt"
    "strings"

    "gopkg.in/square/go-jose.v2/json"
)

func main() {
    dataStr := `
    {
        "float_zero": 0.0,
        "float_a": 1.0,
        "float_b": 0.123,
        "float_c": 21.00000,
        "integer_zero": 0
    }
    `
    dec :=json.NewDecoder(strings.NewReader(dataStr))
    dec.SetNumberType(json.UnmarshalIntOrFloat)
    var kv map[string]interface{}
    err := dec.Decode(&kv)
    if err != nil {
        panic(err)
    }
    for k, v := range kv {
        fmt.Printf("k=>%v(%T), v=>%v(%T)\n", k, k, v, v)
    }
}

and output is:

k=>integer_zero(string), v=>0(int64)
k=>float_zero(string), v=>0(int64)
k=>float_a(string), v=>1(int64)
k=>float_b(string), v=>0.123(float64)
k=>float_c(string), v=>21(int64)

So I hope that it could offer an option to decide whether keep these special float number still be float type like float_zero,float_a,float_c, thanks! :)

csstaub commented 2 years ago

This is a question for the Go standard library, the json package in go-jose is merely a copy of encoding/json from Go with updated semantics to make it more suitable for JOSE objects. It's not meant for use outside of this library.