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