Fix StringBool to handle numeric JSON values
Some Kanboard versions return is_active and similar fields as numeric 0/1 instead of string "0"/"1". Update UnmarshalJSON to try string, then number, then bool parsing.
This commit is contained in:
parent
8aa611d3d7
commit
59252a3f63
3 changed files with 55 additions and 9 deletions
25
types.go
25
types.go
|
|
@ -20,17 +20,26 @@ type StringBool bool
|
|||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
func (b *StringBool) UnmarshalJSON(data []byte) error {
|
||||
// Try as string first (most common from Kanboard)
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
// Try as raw bool
|
||||
var boolVal bool
|
||||
if err := json.Unmarshal(data, &boolVal); err != nil {
|
||||
return err
|
||||
}
|
||||
*b = StringBool(boolVal)
|
||||
if err := json.Unmarshal(data, &s); err == nil {
|
||||
*b = s == "1" || s == "true"
|
||||
return nil
|
||||
}
|
||||
*b = s == "1" || s == "true"
|
||||
|
||||
// Try as number (some Kanboard versions return 0/1)
|
||||
var n int
|
||||
if err := json.Unmarshal(data, &n); err == nil {
|
||||
*b = n != 0
|
||||
return nil
|
||||
}
|
||||
|
||||
// Try as raw bool
|
||||
var boolVal bool
|
||||
if err := json.Unmarshal(data, &boolVal); err != nil {
|
||||
return err
|
||||
}
|
||||
*b = StringBool(boolVal)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue