mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-08 05:47:07 +09:00
24 lines
446 B
Go
24 lines
446 B
Go
package jsonutil
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"reflect"
|
|
)
|
|
|
|
func MarshalSafely(v any) ([]byte, error) {
|
|
clearStruct(v)
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func clearStruct(res interface{}) {
|
|
elem := reflect.ValueOf(res).Elem()
|
|
for i := 0; i < elem.NumField(); i++ {
|
|
field := elem.Field(i)
|
|
if kind := field.Kind(); kind == reflect.Float64 {
|
|
if math.IsNaN(field.Float()) || math.IsInf(field.Float(), 0) {
|
|
field.SetFloat(0)
|
|
}
|
|
}
|
|
}
|
|
}
|