OSDN Git Service

第三部隊の7隻目にアクセスすると落ちる問題を修正
[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 { return node(withArray: json) }
17         if json.type == .dictionary { return node(withDictionary: json) }
18         
19         return node(withObject: json)
20     }
21     
22     private class func node(withObject obj: JSON) -> JSONNode? {
23         
24         let node = JSONLeafNode()
25         let value: String?
26         
27         switch obj.type {
28         case .string: value = obj.stringValue
29         case .number: value = String(obj.intValue)
30         case .null: value = nil
31         default: print(obj); return nil
32         }
33         
34         node.value = value
35         
36         return node
37     }
38     
39     private class func node(withArray array: JSON) -> JSONNode {
40         
41         let node = JSONContainerNode()
42         node.children = array.flatMap { _, json in JSONNode.nodeWithJSON(json) }
43         
44         return node
45     }
46     
47     private class func node(withDictionary dict: JSON) -> JSONNode {
48         
49         let node = JSONContainerNode()
50         node.children = dict.flatMap { (key: String, json: JSON) -> JSONNode? in
51             
52             if let node = JSONNode.nodeWithJSON(json) {
53                 
54                 node.key = key
55                 
56                 return node
57             }
58             
59             return nil
60         }
61         
62         return node
63     }
64     
65     @objc var key: String?
66     @objc var value: String?
67     
68     @objc var children: [JSONNode] = []
69     @objc var isLeaf: Bool { return false }
70     
71     // MARK: - NSCoding, NSCopying
72     required convenience init?(coder aDecoder: NSCoder) {
73         
74         self.init()
75     }
76     
77     func encode(with aCoder: NSCoder) {
78         
79         fatalError("Subclass MUST implement this method")
80     }
81     
82     func copy(with zone: NSZone? = nil) -> Any {
83         
84         fatalError("Subclass MUST implement this method")
85     }
86 }
87
88 final class JSONContainerNode: JSONNode {
89     
90     override init() {
91         
92         super.init()
93     }
94     
95     override var value: String? {
96         
97         get { return "\(children.count) items" }
98         set {}
99     }
100     
101     override func copy(with zone: NSZone?) -> Any {
102         
103         let node = JSONContainerNode()
104         node.children = children
105         
106         return node
107     }
108     
109     required convenience init?(coder aDecoder: NSCoder) {
110         
111         self.init()
112         
113         self.children = aDecoder.decodeObject(forKey: "children") as? [JSONNode] ?? []
114     }
115     
116     override func encode(with aCoder: NSCoder) {
117         
118         aCoder.encode(children, forKey: "children")
119     }
120 }
121
122 final class JSONLeafNode: JSONNode {
123     
124     override init() {
125         
126         super.init()
127     }
128     
129     override var isLeaf: Bool { return true }
130     
131     override func copy(with zone: NSZone?) -> Any {
132         
133         let node = JSONLeafNode()
134         node.key = key
135         node.value = value
136         
137         return node
138     }
139     
140     required convenience init?(coder aDecoder: NSCoder) {
141         
142         self.init()
143         
144         self.value = aDecoder.decodeObject(forKey: "value") as? String
145         self.key = aDecoder.decodeObject(forKey: "key") as? String
146     }
147     
148     override func encode(with aCoder: NSCoder) {
149         
150         aCoder.encode(value, forKey: "value")
151         aCoder.encode(key, forKey: "key")
152     }
153 }