Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__NSCFBoolean will could not convert to true #292

Open
Whirlwind opened this issue Dec 8, 2020 · 2 comments
Open

__NSCFBoolean will could not convert to true #292

Whirlwind opened this issue Dec 8, 2020 · 2 comments

Comments

@Whirlwind
Copy link

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.

@jpsim
Copy link
Owner

jpsim commented Dec 8, 2020

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?

@Whirlwind
Copy link
Author

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()
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants