OSDN Git Service

Damageに問題があるときのログを詳細にした
[kcd/KCD.git] / KCD / JSONNode.swift
1 //
2 //  JSONNode.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/05.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
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)
16     }
17     private class func node(withObject obj: AnyObject?) -> JSONNode? {
18         guard let obj = obj else { return nil }
19         let node = JSONLeafNode()
20         let value: String?
21         switch obj {
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
26         }
27         node.value = value
28         return node
29     }
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 }
34             
35             
36             // TODO: check enter below
37             print("Enter JSONNode ARRAY optional statment")
38             let node = JSONLeafNode()
39             let value: String?
40             switch $0 {
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()
45             }
46             node.value = value
47             return node
48         }
49         return node
50     }
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) {
56                 node.key = d.key
57                 return node
58             }
59             
60             // TODO: check enter below
61             print("Enter JSONNode DICTIONAY optional statment")
62             let node = JSONLeafNode()
63             node.key = d.key
64             let value: String?
65             switch d.value {
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()
70             }
71             node.value = value
72             return node
73         }
74         return node
75     }
76     
77     var key: String?
78     var value: String?
79     
80     var children: [JSONNode] = []
81     var isLeaf: Bool { return false }
82     
83     // MARK: - NSCoding, NSCopying
84     required convenience init?(coder aDecoder: NSCoder) {
85         self.init()
86     }
87     func encode(with aCoder: NSCoder) {
88         fatalError("Subclass MUST implement this method")
89     }
90     func copy(with zone: NSZone? = nil) -> Any {
91         fatalError("Subclass MUST implement this method")
92     }
93 }
94
95 class JSONContainerNode: JSONNode {
96     override init() {
97         super.init()
98     }
99     
100     override var value: String? {
101         get {
102             return "\(children.count) items"
103         }
104         set {}
105     }
106     
107     override func copy(with zone: NSZone?) -> Any {
108         let node = JSONContainerNode()
109         node.children = children
110         return node
111     }
112     required convenience init?(coder aDecoder: NSCoder) {
113         self.init()
114         self.children = aDecoder.decodeObject(forKey: "children") as? [JSONNode] ?? []
115     }
116     override func encode(with aCoder: NSCoder) {
117         aCoder.encode(children, forKey: "children")
118     }
119 }
120
121 class JSONLeafNode: JSONNode {
122     override init() {
123         super.init()
124     }
125     
126     override var isLeaf: Bool { return true }
127     
128     override func copy(with zone: NSZone?) -> Any {
129         let node = JSONLeafNode()
130         node.key = key
131         node.value = value
132         return node
133     }
134     required convenience init?(coder aDecoder: NSCoder) {
135         self.init()
136         self.value = aDecoder.decodeObject(forKey: "value") as? String
137         self.key = aDecoder.decodeObject(forKey: "key") as? String
138     }
139     override func encode(with aCoder: NSCoder) {
140         aCoder.encode(value, forKey: "value")
141         aCoder.encode(key, forKey: "key")
142     }
143 }