当前位置: > > > > Go 在转换为 JSON 之前如何处理浮点数无穷大
来源:stackoverflow
2024-04-27 09:24:36
0浏览
收藏
Golang不知道大家是否熟悉?今天我将给大家介绍《Go 在转换为 JSON 之前如何处理浮点数无穷大》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
问题内容
我遇到过一种情况,我有一些可能是无穷大/nan 的 float64 字段,并且尝试封送至 json 会导致有关不支持 +inf 类型的错误。
type something interface { id string `firestore:"id"` numbera float64 `firestore:"numbera"` numberb float64 `firestore:"numberb"` numberc float64 `firestore:"numberc"` }
此结构最初是通过另一个库 (google firestore) 填充的。
实际上,这个结构体要大得多,有更多的浮点数字段。
我想我可以使用像下面这样的循环,使用反射来找到它们,尽管我想知道是否有更干净的方法或更惯用的方法。
v := reflect.ValueOf(structVar) typeOfS := v.Type() for i := 0; i< v.NumField(); i++ { if typeOfS.Field(i).Type.Kind() == reflect.Float64 && math.IsInf(v.Field(i).Interface().(float64), 1) { // ... some logic I'll put here } }
我不明白如何实现自定义编组,所以也许这可以是处理 +inf 的选项?
正确答案
可以通过实现 marshaler
接口的自定义类型来完成值的自定义处理。但是,您的 something
类型格式错误。它被定义为 type something 接口{}
,而实际上应该是 type something struct
:
type something struct { id string `firestore:"id"` numbera jsonfloat `firestore:"numbera"` numberb jsonfloat `firestore:"numberb"` numberc jsonfloat `firestore:"numberc"` } type jsonfloat float64 func (j jsonfloat) marshaljson() ([]byte, error) { v := float64(j) if math.isinf(j, 0) { // handle infinity, assign desired value to v // or say +/- indicates infinity s := "+" if math.isinf(v, -1) { s = "-" } return []byte(s), nil } return json.marshal(v) // marshal result as standard float64 } func (j *jsonfloat) unsmarshaljson(v []byte) error { if s := string(v); s == "+" || s == "-" { // if +/- indiciates infinity if s == "+" { *j = jsonfloat(math.inf(1)) return nil } *j = jsonfloat(math.inf(-1)) return nil } // just a regular float value var fv float64 if err := json.unmarshal(v, &fv); err != nil {\ return err } *j = jsonfloat(fv) return nil }
应该可以了
我创建了 来支持 nan、+inf 和 -inf。
type T struct { N float64 IP float64 IN float64 } func TestMarshalNaNAndInf(t *testing.T) { s := T{ N: math.NaN(), IP: math.Inf(1), IN: math.Inf(-1), } got, err := Marshal(s) if err != nil { t.Errorf("Marshal() error: %v", err) } want := `{"N":NaN,"IP":+Inf,"IN":-Inf}` if string(got) != want { t.Errorf("Marshal() = %s, want %s", got, want) } } func TestUnmarshalNaNAndInf(t *testing.T) { data := []byte(`{"N":NaN,"IP":+Inf,"IN":-Inf}`) var s T err := Unmarshal(data, &s) if err != nil { t.Fatalf("Unmarshal: %v", err) } if !math.IsNaN(s.N) || !math.IsInf(s.IP, 1) || !math.IsInf(s.IN, -1) { t.Fatalf("after Unmarshal, s.N=%f, s.IP=%f, s.IN=%f, want NaN, +Inf, -Inf", s.N, s.IP, s.IN) } }
今天关于《Go 在转换为 JSON 之前如何处理浮点数无穷大》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!