0

Is there any custom encoding to encode Bool as true instead of 0 or 1 ?

URLEncoding has option to change the Bool as literal or numeric. But JSONEncoding.default doesn't have that option.

Is there any one who has created JSONEncoding with respect to this problem ?

Tanvir Nayem
  • 702
  • 10
  • 25
AtherSajjad
  • 109
  • 10

2 Answers2

1

You need to set the boolEncoding: .literal in the URLEncoding initializer.

Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
0

You can directly convert the boolean value using following method, By default return false. It will work for Bool as literal or numeric.

func boolean(_ anything: Any?) -> Bool {
    if let any = anything {
        if let num = any as? NSNumber {
            return num.boolValue
        } else if let str = any as? NSString {
            return str.boolValue
        }
    }
    return false
}
Anup Kanjariya
  • 292
  • 1
  • 2
  • 15