Adjust JSON unmarshalling to inherit from a bool instead of using a blank interface.

This commit is contained in:
Lucas Rouckhout 2021-05-04 19:26:17 +02:00
parent 9a297a52be
commit 3487d45a26
1 changed files with 3 additions and 10 deletions

13
bool.go
View File

@ -4,8 +4,6 @@ package abool
import (
"encoding/json"
"errors"
"fmt"
"sync/atomic"
)
@ -82,16 +80,11 @@ func (ab *AtomicBool) MarshalJSON() ([]byte, error) {
// Unmarshall normal bool's into AtomicBool
func (ab *AtomicBool) UnmarshalJSON(b []byte) error {
var v interface{}
var v bool
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case bool:
ab.SetTo(value)
return nil
default:
return errors.New(fmt.Sprintf("%s is an invalid JSON representation for an AtomicBool\n", b))
}
ab.SetTo(v)
return nil
}