OSDN Git Service

HMMasterMissionCommandクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / HMJSONNode.m
1 //
2 //  HMJSONNode.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/02/11.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMJSONNode.h"
10
11 #if ENABLE_JSON_LOG
12
13 @interface HMJSONNode ()
14 @property (copy, readwrite) NSString *key;
15 @property (copy, readwrite) NSString *value;
16
17 @end
18
19
20 @interface HMJSONContainerNode : HMJSONNode
21 @property (strong) NSArray *childrenArray;
22 @end
23
24 @interface HMJSONLeafNode : HMJSONNode
25
26 @end
27
28
29 @implementation HMJSONNode
30
31 + (id)nodeWithJSON:(id)json
32 {
33         // NSArray
34         if([json isKindOfClass:[NSArray class]]) {
35                 NSMutableArray *array = [NSMutableArray new];
36                 for(id element in json) {
37                         HMJSONNode *node = [HMJSONNode nodeWithJSON:element];
38                         if(![node isKindOfClass:[HMJSONNode class]]) {
39                                 node = [HMJSONLeafNode new];
40                                 node.value = element;
41                         }
42                         [array addObject:node];
43                 }
44                 HMJSONContainerNode *node = [HMJSONContainerNode new];
45                 node.childrenArray = array;
46                 return node;
47         }
48         
49         // NSDictionary
50         if([json isKindOfClass:[NSDictionary class]]) {
51                 NSMutableArray *array = [NSMutableArray new];
52                 for(id key in json) {
53                         
54                         HMJSONNode *node = [HMJSONNode nodeWithJSON:json[key]];
55                         if([node isKindOfClass:[HMJSONNode class]]) {
56                                 node.key = key;
57                         } else {
58                                 node = [HMJSONLeafNode new];
59                                 node.key = key;
60                                 node.value = json[key];
61                         }
62                         [array addObject:node];
63                 }
64                 HMJSONContainerNode *node = [HMJSONContainerNode new];
65                 node.childrenArray = array;
66                 return node;
67         }
68         
69         return json;
70 }
71
72 - (id)copyWithZone:(NSZone *)zone
73 {
74         @throw [NSException exceptionWithName:@"Subclass MUST implement method."
75                                                                    reason:@"Subclass MUST implement method."
76                                                                  userInfo:nil];
77         return nil;
78 }
79 - (id)initWithCoder:(NSCoder *)aDecoder
80 {
81         @throw [NSException exceptionWithName:@"Subclass MUST implement method."
82                                                                    reason:@"Subclass MUST implement method."
83                                                                  userInfo:nil];
84         return nil;
85 }
86 - (void)encodeWithCoder:(NSCoder *)aCoder
87 {
88         @throw [NSException exceptionWithName:@"Subclass MUST implement method."
89                                                                    reason:@"Subclass MUST implement method."
90                                                                  userInfo:nil];
91 }
92
93 @end
94
95
96 @implementation HMJSONContainerNode
97
98 - (id)copyWithZone:(NSZone *)zone
99 {
100         HMJSONContainerNode *res = [[self class] new];
101         res.value = self.value;
102         res.childrenArray = self.childrenArray;
103         
104         return res;
105 }
106
107 - (id)initWithCoder:(NSCoder *)aDecoder
108 {
109         HMJSONContainerNode *node = [HMJSONContainerNode new];
110         node.childrenArray = [aDecoder decodeObjectForKey:@"children"];
111         node.key = [aDecoder decodeObjectForKey:@"key"];
112         
113         return node;
114 }
115 - (void)encodeWithCoder:(NSCoder *)aCoder
116 {
117         [aCoder encodeObject:self.children forKey:@"children"];
118         [aCoder encodeObject:self.key forKey:@"key"];
119 }
120
121
122 - (NSString *)value
123 {
124         return [NSString stringWithFormat:@"%ld items", [self.children count]];
125 }
126
127
128 - (NSArray *)children
129 {
130         return self.childrenArray;
131 }
132
133 - (NSNumber *)isLeaf
134 {
135         return @NO;
136 }
137
138 @end
139
140
141 @implementation HMJSONLeafNode
142 - (id)copyWithZone:(NSZone *)zone
143 {
144         HMJSONLeafNode *res = [[self class] new];
145         res.key = self.key;
146         res.value = self.value;
147         
148         return res;
149 }
150 - (id)initWithCoder:(NSCoder *)aDecoder
151 {
152         HMJSONLeafNode *node = [HMJSONLeafNode new];
153         node.value = [aDecoder decodeObjectForKey:@"value"];
154         node.key = [aDecoder decodeObjectForKey:@"key"];
155         
156         return node;
157 }
158 - (void)encodeWithCoder:(NSCoder *)aCoder
159 {
160         [aCoder encodeObject:self.value forKey:@"value"];
161         [aCoder encodeObject:self.key forKey:@"key"];
162 }
163
164 - (NSArray *)children
165 {
166         return nil;
167 }
168 - (NSNumber *)isLeaf
169 {
170         return @YES;
171 }
172
173
174 @end
175
176 #endif