dyld-832.7.1.tar.gz
[apple/dyld.git] / dyld3 / JSONReader.mm
1 /*
2  * Copyright (c) 2017 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23
24 #import <Foundation/Foundation.h>
25
26 #include "JSONReader.h"
27 #include "Diagnostics.h"
28
29 namespace dyld3 {
30 namespace json {
31
32 static Node gSentinelNode;
33
34
35 const Node& getRequiredValue(Diagnostics& diags, const Node& node, const char* key) {
36     if (diags.hasError())
37         return gSentinelNode;
38
39     if (!node.array.empty()) {
40         diags.error("Cannot get key '%s' from array node\n", key);
41         return gSentinelNode;
42     }
43     if (!node.value.empty()) {
44         diags.error("Cannot get key '%s' from value node\n", key);
45         return gSentinelNode;
46     }
47
48     auto it = node.map.find(key);
49     if (it == node.map.end()) {
50         diags.error("Map node doesn't have element for key '%s'\n", key);
51         return gSentinelNode;
52     }
53     return it->second;
54 }
55
56
57 const Node* getOptionalValue(Diagnostics& diags, const Node& node, const char* key) {
58     if (diags.hasError())
59         return nullptr;
60
61     if (!node.array.empty()) {
62         diags.error("Cannot get key '%s' from array node\n", key);
63         return nullptr;
64     }
65     if (!node.value.empty()) {
66         diags.error("Cannot get key '%s' from value node\n", key);
67         return nullptr;
68     }
69
70     auto it = node.map.find(key);
71     if (it == node.map.end()) {
72         return nullptr;
73     }
74     return &it->second;
75 }
76
77 uint64_t parseRequiredInt(Diagnostics& diags, const Node& node) {
78     if (diags.hasError())
79         return 0;
80
81     if (!node.array.empty()) {
82         diags.error("Cannot get integer value from array node\n");
83         return 0;
84     }
85     if (!node.map.empty()) {
86         diags.error("Cannot get integer value from value node\n");
87         return 0;
88     }
89     if (node.value.empty()) {
90         diags.error("Cannot get integer value from empty node\n");
91         return 0;
92     }
93
94     return atoi(node.value.c_str());
95 }
96
97 bool parseRequiredBool(Diagnostics& diags, const Node& node) {
98     if (diags.hasError())
99         return false;
100
101     if (!node.array.empty()) {
102         diags.error("Cannot get integer value from array node\n");
103         return false;
104     }
105     if (!node.map.empty()) {
106         diags.error("Cannot get integer value from value node\n");
107         return false;
108     }
109     if (node.value.empty()) {
110         diags.error("Cannot get integer value from empty node\n");
111         return false;
112     }
113
114     if ( (node.value == "true") || (node.value == "1") )
115         return true;
116
117     if ( (node.value == "false") || (node.value == "0") )
118         return false;
119
120     diags.error("Boolean value should be true/false/0/1\n");
121     return false;
122 }
123
124 const std::string& parseRequiredString(Diagnostics& diags, const Node& node) {
125     static std::string sentinelString = "";
126
127     if (diags.hasError())
128         return sentinelString;
129
130     if (!node.array.empty()) {
131         diags.error("Cannot get string value from array node\n");
132         return sentinelString;
133     }
134     if (!node.map.empty()) {
135         diags.error("Cannot get string value from value node\n");
136         return sentinelString;
137     }
138     if (node.value.empty()) {
139         diags.error("Cannot get string value from empty node\n");
140         return sentinelString;
141     }
142     return node.value;
143 }
144
145
146 Node parseNode(Diagnostics& diags, id jsonObject) {
147     __block Node node;
148
149     // NSDictionary -> map
150     if ([jsonObject isKindOfClass:[NSDictionary class]]) {
151         NSDictionary* dict = (NSDictionary*)jsonObject;
152
153         [dict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
154             if (![key isKindOfClass:[NSString class]]) {
155                 diags.error("JSON map key is not of string type\n");
156                 *stop = true;
157                 return;
158             }
159             Node childNode = parseNode(diags, value);
160             if (diags.hasError()) {
161                 *stop = true;
162                 return;
163             }
164             node.map[[key UTF8String]] = childNode;
165         }];
166
167         if (diags.hasError())
168             return Node();
169
170         return node;
171     }
172
173     // NSArray -> array
174     if ([jsonObject isKindOfClass:[NSArray class]]) {
175         NSArray* array = (NSArray*)jsonObject;
176
177         [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
178             Node childNode = parseNode(diags, obj);
179             if (diags.hasError()) {
180                 *stop = true;
181                 return;
182             }
183             node.array.push_back(childNode);
184         }];
185
186         if (diags.hasError())
187             return Node();
188
189         return node;
190     }
191
192     // NSString -> value
193     if ([jsonObject isKindOfClass:[NSString class]]) {
194         node.value = [(NSString*)jsonObject UTF8String];
195         return node;
196     }
197
198     // NSBoolean -> string
199     if ([jsonObject isKindOfClass:[NSNumber class]]) {
200         node.value = [[(NSNumber*)jsonObject stringValue] UTF8String];
201         return node;
202     }
203
204     diags.error("Unknown json deserialized type\n");
205     return Node();
206 }
207
208 Node readJSON(Diagnostics& diags, const char* filePath) {
209     NSInputStream* inputStream = [NSInputStream inputStreamWithFileAtPath:[NSString stringWithUTF8String:filePath]];
210     if (!inputStream) {
211         diags.error("Could not option json file: '%s'\n", filePath);
212         return Node();
213     }
214     [inputStream open];
215
216     NSError* error = nil;
217     id jsonObject = [NSJSONSerialization JSONObjectWithStream:inputStream options:NSJSONReadingMutableContainers error:&error];
218     if (!jsonObject) {
219         diags.error("Could not deserializer json file: '%s' because '%s'\n", filePath, [[error localizedFailureReason] UTF8String]);
220         [inputStream close];
221         return Node();
222     }
223
224     Node node = parseNode(diags, jsonObject);
225     [inputStream close];
226     return node;
227 }
228
229 Node readJSON(Diagnostics& diags, const void * contents, size_t length) {
230     NSData* data = [NSData dataWithBytes:contents length:length];
231     NSError* error = nil;
232     id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
233     if (!jsonObject) {
234         diags.error("Could not deserialize json because '%s'\n", [[error localizedFailureReason] UTF8String]);
235         return Node();
236     }
237
238     return parseNode(diags, jsonObject);
239 }
240
241 } //namespace json
242 } //namespace dyld3