endocrimes / Jay

Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization.
MIT License
132 stars 19 forks source link

dataFromJson() Int 0,1 exchange Bool #57

Open iq3addLi opened 8 years ago

iq3addLi commented 8 years ago

Use Vapor 1.0.3 contains Jay. Xcode 8.0.

.Package(url: "https://github.com/DanToml/Jay.git", majorVersion: 1)

func dictionaryToJayType(_ maybeDictionary: Any) throws -> JSON? {

    let mirror = Mirror(reflecting: maybeDictionary)
    let childrenValues = mirror.children.map { $0.value }

    var obj = [String: JSON]()
    for i in childrenValues {

        let childMirror = Mirror(reflecting: i)
        let children = childMirror.children
        if childMirror.displayStyle == .tuple && children.count == 2 {
            let it = children.makeIterator()
            let maybeKey = it.next()!.value
            guard let key = maybeKey as? String else {
                throw JayError.keyIsNotString(maybeKey)
            }
            let value = it.next()!.value
            obj[key] = try self.toJayType(value) // Int 1 -> .boolean(true)
        } else {
            throw JayError.unsupportedType(childMirror)
        }
    }
    return .object(obj)
}