5 // Created by Hori,Masaki on 2017/01/05.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 class JSONNode: NSObject, NSCoding, NSCopying {
12 class func nodeWithJSON(_ json: AnyObject?) -> JSONNode? {
13 if let j = json as? NSArray { return node(withArray: j) }
14 if let j = json as? NSDictionary { return node(withDictionary: j) }
15 return node(withObject: json)
17 private class func node(withObject obj: AnyObject?) -> JSONNode? {
18 guard let obj = obj else { return nil }
19 let node = JSONLeafNode()
22 case let v as String: value = v
23 case let v as Int: value = String(v)
24 case _ as NSNull: value = nil
25 default: print(obj); return nil
30 private class func node(withArray array: NSArray) -> JSONNode {
31 let node = JSONContainerNode()
32 node.children = array.map {
33 if let node = JSONNode.nodeWithJSON($0 as AnyObject?) { return node }
36 // TODO: check enter below
37 print("Enter JSONNode ARRAY optional statment")
38 let node = JSONLeafNode()
41 case let v as String: value = v
42 case let v as Int: value = String(v)
43 case _ as NSNull: value = nil
44 default: print($0); fatalError()
51 private class func node(withDictionary dict: NSDictionary) -> JSONNode {
52 guard let dict = dict as? [String: AnyObject] else { fatalError("JSON is broken.") }
53 let node = JSONContainerNode()
54 node.children = dict.map { (d: (key: String, value: AnyObject)) -> JSONNode in
55 if let node = JSONNode.nodeWithJSON(d.value) {
60 // TODO: check enter below
61 print("Enter JSONNode DICTIONAY optional statment")
62 let node = JSONLeafNode()
66 case let v as String: value = v
67 case let v as Int: value = String(v)
68 case _ as NSNull: value = nil
69 default: print(d.value); fatalError()
80 var children: [JSONNode] = []
81 var isLeaf: Bool { return false }
83 // MARK: - NSCoding, NSCopying
84 required convenience init?(coder aDecoder: NSCoder) {
87 func encode(with aCoder: NSCoder) {
88 fatalError("Subclass MUST implement this method")
90 func copy(with zone: NSZone? = nil) -> Any {
91 fatalError("Subclass MUST implement this method")
95 class JSONContainerNode: JSONNode {
100 override var value: String? {
102 return "\(children.count) items"
107 override func copy(with zone: NSZone?) -> Any {
108 let node = JSONContainerNode()
109 node.children = children
112 required convenience init?(coder aDecoder: NSCoder) {
114 self.children = aDecoder.decodeObject(forKey: "children") as? [JSONNode] ?? []
116 override func encode(with aCoder: NSCoder) {
117 aCoder.encode(children, forKey: "children")
121 class JSONLeafNode: JSONNode {
126 override var isLeaf: Bool { return true }
128 override func copy(with zone: NSZone?) -> Any {
129 let node = JSONLeafNode()
134 required convenience init?(coder aDecoder: NSCoder) {
136 self.value = aDecoder.decodeObject(forKey: "value") as? String
137 self.key = aDecoder.decodeObject(forKey: "key") as? String
139 override func encode(with aCoder: NSCoder) {
140 aCoder.encode(value, forKey: "value")
141 aCoder.encode(key, forKey: "key")