Fix empty string int marshalling in go 1.14

This commit is contained in:
Paul Tyng
2020-03-26 16:13:19 -04:00
parent d076e78005
commit f74d29bd54
5 changed files with 102 additions and 38 deletions

33
unifi/json.go Normal file
View File

@@ -0,0 +1,33 @@
package unifi
import (
"strconv"
"strings"
)
// emptyStringInt was created due to the behavior change in
// Go 1.14 with json.Number's handling of empty string.
type emptyStringInt int
func (e *emptyStringInt) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
if string(b) == `""` {
return nil
}
var err error
s := string(b)
if strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`) {
s, err = strconv.Unquote(s)
if err != nil {
return err
}
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*e = emptyStringInt(i)
return nil
}