OSDN Git Service

不要となっていたプロパティを削除
[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 import SwiftyJSON
11
12 class JSONNode: NSObject, NSCoding, NSCopying {
13     
14     class func nodeWithJSON(_ json: JSON) -> JSONNode? {
15         
16         if json.type == .array {
17             
18             return node(withArray: json)
19         }
20         if json.type == .dictionary {
21             
22             return node(withDictionary: json)
23         }
24         
25         return node(withObject: json)
26     }
27     
28     private class func node(withObject obj: JSON) -> JSONNode? {
29         
30         let node = JSONLeafNode()
31         let value: String?
32         
33         switch obj.type {
34             
35         case .string: value = obj.stringValue
36             
37         case .number: value = String(obj.intValue)
38             
39         case .null: value = nil
40             
41         default:
42             
43             print(obj)
44             
45             return nil
46             
47         }
48         
49         node.value = value
50         
51         return node
52     }
53     
54     private class func node(withArray array: JSON) -> JSONNode {
55         
56         let node = JSONContainerNode()
57         node.children = array.compactMap { _, json in JSONNode.nodeWithJSON(json) }
58         
59         return node
60     }
61     
62     private class func node(withDictionary dict: JSON) -> JSONNode {
63         
64         let node = JSONContainerNode()
65         node.children = dict.compactMap { (key: String, json: JSON) -> JSONNode? in
66             
67             if let node = JSONNode.nodeWithJSON(json) {
68                 
69                 node.key = key
70                 
71                 return node
72             }
73             
74             return nil
75         }
76         
77         return node
78     }
79     
80     @objc var key: String?
81     @objc var value: String?
82     
83     @objc var children: [JSONNode] = []
84     @objc var isLeaf: Bool { return false }
85     
86     // MARK: - NSCoding, NSCopying
87     required convenience init?(coder aDecoder: NSCoder) {
88         
89         self.init()
90     }
91     
92     func encode(with aCoder: NSCoder) {
93         
94         fatalError("Subclass MUST implement this method")
95     }
96     
97     func copy(with zone: NSZone? = nil) -> Any {
98         
99         fatalError("Subclass MUST implement this method")
100     }
101 }
102
103 final class JSONContainerNode: JSONNode {
104     
105     override init() {
106         
107         super.init()
108     }
109     
110     override var value: String? {
111         
112         get { return "\(children.count) items" }
113         set {}
114     }
115     
116     override func copy(with zone: NSZone?) -> Any {
117         
118         let node = JSONContainerNode()
119         node.children = children
120         
121         return node
122     }
123     
124     required convenience init?(coder aDecoder: NSCoder) {
125         
126         self.init()
127         
128         self.children = aDecoder.decodeObject(forKey: "children") as? [JSONNode] ?? []
129     }
130     
131     override func encode(with aCoder: NSCoder) {
132         
133         aCoder.encode(children, forKey: "children")
134     }
135 }
136
137 final class JSONLeafNode: JSONNode {
138     
139     override init() {
140         
141         super.init()
142     }
143     
144     override var isLeaf: Bool { return true }
145     
146     override func copy(with zone: NSZone?) -> Any {
147         
148         let node = JSONLeafNode()
149         node.key = key
150         node.value = value
151         
152         return node
153     }
154     
155     required convenience init?(coder aDecoder: NSCoder) {
156         
157         self.init()
158         
159         self.value = aDecoder.decodeObject(forKey: "value") as? String
160         self.key = aDecoder.decodeObject(forKey: "key") as? String
161     }
162     
163     override func encode(with aCoder: NSCoder) {
164         
165         aCoder.encode(value, forKey: "value")
166         aCoder.encode(key, forKey: "key")
167     }
168 }