2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Foundation/Foundation.h>
26 #include "JSONReader.h"
27 #include "Diagnostics.h"
32 static Node gSentinelNode;
35 const Node& getRequiredValue(Diagnostics& diags, const Node& node, const char* key) {
39 if (!node.array.empty()) {
40 diags.error("Cannot get key '%s' from array node\n", key);
43 if (!node.value.empty()) {
44 diags.error("Cannot get key '%s' from value node\n", key);
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);
57 const Node* getOptionalValue(Diagnostics& diags, const Node& node, const char* key) {
61 if (!node.array.empty()) {
62 diags.error("Cannot get key '%s' from array node\n", key);
65 if (!node.value.empty()) {
66 diags.error("Cannot get key '%s' from value node\n", key);
70 auto it = node.map.find(key);
71 if (it == node.map.end()) {
77 uint64_t parseRequiredInt(Diagnostics& diags, const Node& node) {
81 if (!node.array.empty()) {
82 diags.error("Cannot get integer value from array node\n");
85 if (!node.map.empty()) {
86 diags.error("Cannot get integer value from value node\n");
89 if (node.value.empty()) {
90 diags.error("Cannot get integer value from empty node\n");
94 return atoi(node.value.c_str());
97 bool parseRequiredBool(Diagnostics& diags, const Node& node) {
101 if (!node.array.empty()) {
102 diags.error("Cannot get integer value from array node\n");
105 if (!node.map.empty()) {
106 diags.error("Cannot get integer value from value node\n");
109 if (node.value.empty()) {
110 diags.error("Cannot get integer value from empty node\n");
114 if ( (node.value == "true") || (node.value == "1") )
117 if ( (node.value == "false") || (node.value == "0") )
120 diags.error("Boolean value should be true/false/0/1\n");
124 const std::string& parseRequiredString(Diagnostics& diags, const Node& node) {
125 static std::string sentinelString = "";
127 if (diags.hasError())
128 return sentinelString;
130 if (!node.array.empty()) {
131 diags.error("Cannot get string value from array node\n");
132 return sentinelString;
134 if (!node.map.empty()) {
135 diags.error("Cannot get string value from value node\n");
136 return sentinelString;
138 if (node.value.empty()) {
139 diags.error("Cannot get string value from empty node\n");
140 return sentinelString;
146 Node parseNode(Diagnostics& diags, id jsonObject) {
149 // NSDictionary -> map
150 if ([jsonObject isKindOfClass:[NSDictionary class]]) {
151 NSDictionary* dict = (NSDictionary*)jsonObject;
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");
159 Node childNode = parseNode(diags, value);
160 if (diags.hasError()) {
164 node.map[[key UTF8String]] = childNode;
167 if (diags.hasError())
174 if ([jsonObject isKindOfClass:[NSArray class]]) {
175 NSArray* array = (NSArray*)jsonObject;
177 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
178 Node childNode = parseNode(diags, obj);
179 if (diags.hasError()) {
183 node.array.push_back(childNode);
186 if (diags.hasError())
193 if ([jsonObject isKindOfClass:[NSString class]]) {
194 node.value = [(NSString*)jsonObject UTF8String];
198 // NSBoolean -> string
199 if ([jsonObject isKindOfClass:[NSNumber class]]) {
200 node.value = [[(NSNumber*)jsonObject stringValue] UTF8String];
204 diags.error("Unknown json deserialized type\n");
208 Node readJSON(Diagnostics& diags, const char* filePath) {
209 NSInputStream* inputStream = [NSInputStream inputStreamWithFileAtPath:[NSString stringWithUTF8String:filePath]];
211 diags.error("Could not option json file: '%s'\n", filePath);
216 NSError* error = nil;
217 id jsonObject = [NSJSONSerialization JSONObjectWithStream:inputStream options:NSJSONReadingMutableContainers error:&error];
219 diags.error("Could not deserializer json file: '%s' because '%s'\n", filePath, [[error localizedFailureReason] UTF8String]);
224 Node node = parseNode(diags, jsonObject);
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];
234 diags.error("Could not deserialize json because '%s'\n", [[error localizedFailureReason] UTF8String]);
238 return parseNode(diags, jsonObject);