Files
go-unifi/unifi/json.go
James Stephenson 47fa522aba Expose the Account API (RADIUS users)
* Adds Account CRUD API
* Enahnces `emptyStringInt` with a predicate for dynamically setting
  which value should be interpreted as a blank JSON payload
2020-08-27 21:48:13 -04:00

42 lines
740 B
Go

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
}
func (e *emptyStringInt) MarshalJSON() ([]byte, error) {
if e == nil || *e == 0 {
return []byte(`""`), nil
}
return []byte(strconv.Itoa(int(*e))), nil
}