Skip to content

Instantly share code, notes, and snippets.

@Elland Elland/json.swift
Last active Jan 26, 2017

Embed
What would you like to do?
public enum JSON {
public typealias D = Dictionary<String, Any>
public typealias A = Array<Dictionary<String, Any>>
case dictionary(D)
case array(A)
public init(_ dictionary: [String: Any]) {
self = .dictionary(dictionary)
}
public init(_ array: [[String: Any]]) {
self = .array(array)
}
public var dictionary: D? {
get {
switch self {
case .dictionary(let d):
return d
default:
return nil
}
}
}
public var array: A? {
get {
switch self {
case .array(let a):
return a
default:
return nil
}
}
}
}
@Elland

This comment has been minimized.

Copy link
Owner Author

commented Jan 26, 2017

Usage is not ideal yet tho. Suggestions?

if let dictionary = json?.dictionary {
} else if let array = json?.array {
}
@Elland

This comment has been minimized.

Copy link
Owner Author

commented Jan 26, 2017

switch json {
case .array(let array):
case .dictionary(let dictionary):
}

Doesn't look better to me either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.