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 const std::string& parseRequiredString(Diagnostics& diags, const Node& node) {
98 static std::string sentinelString = "";
100 if (diags.hasError())
101 return sentinelString;
103 if (!node.array.empty()) {
104 diags.error("Cannot get string value from array node\n");
105 return sentinelString;
107 if (!node.map.empty()) {
108 diags.error("Cannot get string value from value node\n");
109 return sentinelString;
111 if (node.value.empty()) {
112 diags.error("Cannot get string value from empty node\n");
113 return sentinelString;
119 Node parseNode(Diagnostics& diags, id jsonObject) {
122 // NSDictionary -> map
123 if ([jsonObject isKindOfClass:[NSDictionary class]]) {
124 NSDictionary* dict = (NSDictionary*)jsonObject;
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");
132 Node childNode = parseNode(diags, value);
133 if (diags.hasError()) {
137 node.map[[key UTF8String]] = childNode;
140 if (diags.hasError())
147 if ([jsonObject isKindOfClass:[NSArray class]]) {
148 NSArray* array = (NSArray*)jsonObject;
150 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
151 Node childNode = parseNode(diags, obj);
152 if (diags.hasError()) {
156 node.array.push_back(childNode);
159 if (diags.hasError())
166 if ([jsonObject isKindOfClass:[NSString class]]) {
167 node.value = [(NSString*)jsonObject UTF8String];
171 diags.error("Unknown json deserialized type\n");
175 Node readJSON(Diagnostics& diags, const char* filePath) {
176 NSInputStream* inputStream = [NSInputStream inputStreamWithFileAtPath:[NSString stringWithUTF8String:filePath]];
178 diags.error("Could not option json file: '%s'\n", filePath);
183 NSError* error = nil;
184 id jsonObject = [NSJSONSerialization JSONObjectWithStream:inputStream options:NSJSONReadingMutableContainers error:&error];
186 diags.error("Could not deserializer json file: '%s' because '%s'\n", filePath, [[error localizedFailureReason] UTF8String]);
191 Node node = parseNode(diags, jsonObject);