Fix websocket json; represent NaN as null (#3298)

This commit is contained in:
Michael Geers 2022-05-02 07:54:26 +02:00 • committed by GitHub
parent 63b7ffd7e7
commit 8783defb42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package server
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strings"
"time"
@ -100,7 +101,11 @@ func encode(v interface{}) (string, error) {
// must be before stringer to convert to seconds instead of string
s = fmt.Sprintf("%d", int64(val.Seconds()))
case float64:
s = fmt.Sprintf("%.5g", val)
if math.IsNaN(val) {
s = "null"
} else {
s = fmt.Sprintf("%.5g", val)
}
default:
if b, err := json.Marshal(v); err == nil {
s = string(b)