OSDN Git Service

SwiftyJSONを導入
[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     class func nodeWithJSON(_ json: JSON) -> JSONNode? {
14         if json.type == .array { return node(withArray: json) }
15         if json.type == .dictionary { return node(withDictionary: json) }
16         return node(withObject: json)
17     }
18     private class func node(withObject obj: JSON) -> JSONNode? {
19         let node = JSONLeafNode()
20         let value: String?
21         switch obj.type {
22         case .string: value = obj.stringValue
23         case .number: value = String(obj.intValue)
24         case .null: value = nil
25         default: print(obj); return nil
26         }
27         node.value = value
28         return node
29     }
30     private class func node(withArray array: JSON) -> JSONNode {
31         let node = JSONContainerNode()
32         node.children = array.flatMap { (_, json) in JSONNode.nodeWithJSON(json) }
33         return node
34     }
35     private class func node(withDictionary dict: JSON) -> JSONNode {
36         let node = JSONContainerNode()
37         node.children = dict.flatMap { (key: String, json: JSON) -> JSONNode? in
38             if let node = JSONNode.nodeWithJSON(json) {
39                 node.key = key
40                 return node
41             }
42             return nil
43         }
44         return node
45     }
46     
47     var key: String?
48     var value: String?
49     
50     var children: [JSONNode] = []
51     var isLeaf: Bool { return false }
52     
53     // MARK: - NSCoding, NSCopying
54     required convenience init?(coder aDecoder: NSCoder) {
55         self.init()
56     }
57     func encode(with aCoder: NSCoder) {
58         fatalError("Subclass MUST implement this method")
59     }
60     func copy(with zone: NSZone? = nil) -> Any {
61         fatalError("Subclass MUST implement this method")
62     }
63 }
64
65 class JSONContainerNode: JSONNode {
66     override init() {
67         super.init()
68     }
69     
70     override var value: String? {
71         get {
72             return "\(children.count) items"
73         }
74         set {}
75     }
76     
77     override func copy(with zone: NSZone?) -> Any {
78         let node = JSONContainerNode()
79         node.children = children
80         return node
81     }
82     required convenience init?(coder aDecoder: NSCoder) {
83         self.init()
84         self.children = aDecoder.decodeObject(forKey: "children") as? [JSONNode] ?? []
85     }
86     override func encode(with aCoder: NSCoder) {
87         aCoder.encode(children, forKey: "children")
88     }
89 }
90
91 class JSONLeafNode: JSONNode {
92     override init() {
93         super.init()
94     }
95     
96     override var isLeaf: Bool { return true }
97     
98     override func copy(with zone: NSZone?) -> Any {
99         let node = JSONLeafNode()
100         node.key = key
101         node.value = value
102         return node
103     }
104     required convenience init?(coder aDecoder: NSCoder) {
105         self.init()
106         self.value = aDecoder.decodeObject(forKey: "value") as? String
107         self.key = aDecoder.decodeObject(forKey: "key") as? String
108     }
109     override func encode(with aCoder: NSCoder) {
110         aCoder.encode(value, forKey: "value")
111         aCoder.encode(key, forKey: "key")
112     }
113 }