Yams
Yams copied to clipboard
__NSCFBoolean will could not convert to `true`
I have a json string:
{"bool_value": true}
I get a hash with the JSONSerialization class:
{
"bool_value": 1 // NSNumber, @(true)
}
Now, I will to yaml, I get a error Failed to represent 1:
private func represent(_ value: Any) throws -> Node {
if let representable = value as? NodeRepresentable { // The NSNumber is not NodeRepresentable
return try representable.represented()
}
throw YamlError.representer(problem: "Failed to represent \(value)")
}
I test some code:
(lldb) po value
1
(lldb) po type(of: value)
__NSCFBoolean
(lldb) po value is Bool
true
(lldb) po value is NSNumber
true
(lldb) po value is NodeRepresentable
false
I think the __NSCFBoolean is not Bool, so it does not extension the NodeRepresentable.
What platform & version is this on? What version of Yams is this with? Do you have a complete sample that I could try to run to see this issue?
The code fix my issue:
extension NSNumber: ScalarRepresentable {
public func represented() -> Node.Scalar {
switch CFGetTypeID(self as CFTypeRef) {
case CFBooleanGetTypeID():
return self.boolValue.represented()
case CFNumberGetTypeID():
switch CFNumberGetType(self as CFNumber) {
case .sInt8Type:
return self.int8Value.represented()
case .sInt16Type:
return self.int16Value.represented()
case .sInt32Type:
return self.int32Value.represented()
case .sInt64Type:
return self.int64Value.represented()
case .doubleType:
return self.doubleValue.represented()
default:
return self.intValue.represented()
}
default:
return self.intValue.represented()
}
}
}