]> git.saurik.com Git - apple/dyld.git/blob - dyld3/JSONReader.mm
dyld-733.8.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 const std::string& parseRequiredString(Diagnostics& diags, const Node& node) {
98 static std::string sentinelString = "";
99
100 if (diags.hasError())
101 return sentinelString;
102
103 if (!node.array.empty()) {
104 diags.error("Cannot get string value from array node\n");
105 return sentinelString;
106 }
107 if (!node.map.empty()) {
108 diags.error("Cannot get string value from value node\n");
109 return sentinelString;
110 }
111 if (node.value.empty()) {
112 diags.error("Cannot get string value from empty node\n");
113 return sentinelString;
114 }
115 return node.value;
116 }
117
118
119 Node parseNode(Diagnostics& diags, id jsonObject) {
120 __block Node node;
121
122 // NSDictionary -> map
123 if ([jsonObject isKindOfClass:[NSDictionary class]]) {
124 NSDictionary* dict = (NSDictionary*)jsonObject;
125
126 [dict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
127 if (![key isKindOfClass:[NSString class]]) {
128 diags.error("JSON map key is not of string type\n");
129 *stop = true;
130 return;
131 }
132 Node childNode = parseNode(diags, value);
133 if (diags.hasError()) {
134 *stop = true;
135 return;
136 }
137 node.map[[key UTF8String]] = childNode;
138 }];
139
140 if (diags.hasError())
141 return Node();
142
143 return node;
144 }
145
146 // NSArray -> array
147 if ([jsonObject isKindOfClass:[NSArray class]]) {
148 NSArray* array = (NSArray*)jsonObject;
149
150 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
151 Node childNode = parseNode(diags, obj);
152 if (diags.hasError()) {
153 *stop = true;
154 return;
155 }
156 node.array.push_back(childNode);
157 }];
158
159 if (diags.hasError())
160 return Node();
161
162 return node;
163 }
164
165 // NSString -> value
166 if ([jsonObject isKindOfClass:[NSString class]]) {
167 node.value = [(NSString*)jsonObject UTF8String];
168 return node;
169 }
170
171 diags.error("Unknown json deserialized type\n");
172 return Node();
173 }
174
175 Node readJSON(Diagnostics& diags, const char* filePath) {
176 NSInputStream* inputStream = [NSInputStream inputStreamWithFileAtPath:[NSString stringWithUTF8String:filePath]];
177 if (!inputStream) {
178 diags.error("Could not option json file: '%s'\n", filePath);
179 return Node();
180 }
181 [inputStream open];
182
183 NSError* error = nil;
184 id jsonObject = [NSJSONSerialization JSONObjectWithStream:inputStream options:NSJSONReadingMutableContainers error:&error];
185 if (!jsonObject) {
186 diags.error("Could not deserializer json file: '%s' because '%s'\n", filePath, [[error localizedFailureReason] UTF8String]);
187 [inputStream close];
188 return Node();
189 }
190
191 Node node = parseNode(diags, jsonObject);
192 [inputStream close];
193 return node;
194 }
195
196 } //namespace json
197 } //namespace dyld3