1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-07 21:37:04 +09:00

GO-1014 Fix NaN and inf when marshalling json

This commit is contained in:
Mikhail Iudin 2023-04-25 18:17:59 +02:00
parent d728c5c3f7
commit 180ade1a25
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
2 changed files with 27 additions and 2 deletions

View file

@ -2,7 +2,6 @@ package mill
import (
"bytes"
"encoding/json"
"image"
"io"
"path/filepath"
@ -10,6 +9,8 @@ import (
"time"
"github.com/rwcarlsen/goexif/exif"
"github.com/anytypeio/go-anytype-middleware/util/jsonutil"
)
type ImageExifSchema struct {
@ -140,7 +141,7 @@ func (m *ImageExif) Mill(r io.ReadSeeker, name string) (*Result, error) {
Description: description,
}
b, err := json.Marshal(res)
b, err := jsonutil.MarshalSafely(res)
if err != nil {
return nil, err
}

24
util/jsonutil/json.go Normal file
View file

@ -0,0 +1,24 @@
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)
}
}
}
}