mirror of https://github.com/tevino/abool.git
Adjust JSON unmarshalling to inherit from a bool instead of using a blank interface.
This commit is contained in:
parent
9a297a52be
commit
3487d45a26
11
bool.go
11
bool.go
|
@ -4,8 +4,6 @@ package abool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -82,16 +80,11 @@ func (ab *AtomicBool) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
// Unmarshall normal bool's into AtomicBool
|
// Unmarshall normal bool's into AtomicBool
|
||||||
func (ab *AtomicBool) UnmarshalJSON(b []byte) error {
|
func (ab *AtomicBool) UnmarshalJSON(b []byte) error {
|
||||||
var v interface{}
|
var v bool
|
||||||
if err := json.Unmarshal(b, &v); err != nil {
|
if err := json.Unmarshal(b, &v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch value := v.(type) {
|
ab.SetTo(v)
|
||||||
case bool:
|
|
||||||
ab.SetTo(value)
|
|
||||||
return nil
|
return nil
|
||||||
default:
|
|
||||||
return errors.New(fmt.Sprintf("%s is an invalid JSON representation for an AtomicBool\n", b))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue