kratos/x/json_bool.go

25 lines
485 B
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"fmt"
)
// ConvertibleBoolean can unmarshal both booleans and strings.
type ConvertibleBoolean bool
func (bit *ConvertibleBoolean) UnmarshalJSON(data []byte) error {
asString := string(data)
switch asString {
case "true", `"true"`:
*bit = true
case "false", `"false"`:
*bit = false
default:
return fmt.Errorf("boolean unmarshal error: invalid input %s", asString)
}
return nil
}