├── .gitignore ├── LICENSE ├── OptJSON.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── OptJSON ├── Info.plist ├── OptJSON.h └── OptJSON.swift ├── OptJSONTests ├── AltJSONTests.swift ├── Info.plist └── OptJSONTests.swift └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control? 25 | # 26 | # Pods/ 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 odnoletkov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /OptJSON.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 50A5DEFE1967637F0093A1AD /* AltJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A5DEFD1967637F0093A1AD /* AltJSONTests.swift */; }; 11 | 50FCCCB5195C5F2100095152 /* OptJSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 50FCCCB4195C5F2100095152 /* OptJSON.h */; settings = {ATTRIBUTES = (Public, ); }; }; 12 | 50FCCCBB195C5F2100095152 /* OptJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50FCCCAF195C5F2100095152 /* OptJSON.framework */; }; 13 | 50FCCCC2195C5F2100095152 /* OptJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50FCCCC1195C5F2100095152 /* OptJSONTests.swift */; }; 14 | 50FCCCCC195C614500095152 /* OptJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50FCCCCB195C614500095152 /* OptJSON.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXContainerItemProxy section */ 18 | 50FCCCBC195C5F2100095152 /* PBXContainerItemProxy */ = { 19 | isa = PBXContainerItemProxy; 20 | containerPortal = 50FCCCA6195C5F2100095152 /* Project object */; 21 | proxyType = 1; 22 | remoteGlobalIDString = 50FCCCAE195C5F2100095152; 23 | remoteInfo = OptJSON; 24 | }; 25 | /* End PBXContainerItemProxy section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 50A5DEFD1967637F0093A1AD /* AltJSONTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AltJSONTests.swift; sourceTree = ""; }; 29 | 50FCCCAF195C5F2100095152 /* OptJSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OptJSON.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 50FCCCB3195C5F2100095152 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | 50FCCCB4195C5F2100095152 /* OptJSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OptJSON.h; sourceTree = ""; }; 32 | 50FCCCBA195C5F2100095152 /* OptJSONTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OptJSONTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | 50FCCCC0195C5F2100095152 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | 50FCCCC1195C5F2100095152 /* OptJSONTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptJSONTests.swift; sourceTree = ""; }; 35 | 50FCCCCB195C614500095152 /* OptJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptJSON.swift; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | 50FCCCAB195C5F2100095152 /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | 50FCCCB7195C5F2100095152 /* Frameworks */ = { 47 | isa = PBXFrameworksBuildPhase; 48 | buildActionMask = 2147483647; 49 | files = ( 50 | 50FCCCBB195C5F2100095152 /* OptJSON.framework in Frameworks */, 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | /* End PBXFrameworksBuildPhase section */ 55 | 56 | /* Begin PBXGroup section */ 57 | 50FCCCA5195C5F2100095152 = { 58 | isa = PBXGroup; 59 | children = ( 60 | 50FCCCB1195C5F2100095152 /* OptJSON */, 61 | 50FCCCBE195C5F2100095152 /* OptJSONTests */, 62 | 50FCCCB0195C5F2100095152 /* Products */, 63 | ); 64 | sourceTree = ""; 65 | }; 66 | 50FCCCB0195C5F2100095152 /* Products */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 50FCCCAF195C5F2100095152 /* OptJSON.framework */, 70 | 50FCCCBA195C5F2100095152 /* OptJSONTests.xctest */, 71 | ); 72 | name = Products; 73 | sourceTree = ""; 74 | }; 75 | 50FCCCB1195C5F2100095152 /* OptJSON */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 50FCCCB4195C5F2100095152 /* OptJSON.h */, 79 | 50FCCCCB195C614500095152 /* OptJSON.swift */, 80 | 50FCCCB2195C5F2100095152 /* Supporting Files */, 81 | ); 82 | path = OptJSON; 83 | sourceTree = ""; 84 | }; 85 | 50FCCCB2195C5F2100095152 /* Supporting Files */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 50FCCCB3195C5F2100095152 /* Info.plist */, 89 | ); 90 | name = "Supporting Files"; 91 | sourceTree = ""; 92 | }; 93 | 50FCCCBE195C5F2100095152 /* OptJSONTests */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 50FCCCC1195C5F2100095152 /* OptJSONTests.swift */, 97 | 50A5DEFD1967637F0093A1AD /* AltJSONTests.swift */, 98 | 50FCCCBF195C5F2100095152 /* Supporting Files */, 99 | ); 100 | path = OptJSONTests; 101 | sourceTree = ""; 102 | }; 103 | 50FCCCBF195C5F2100095152 /* Supporting Files */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 50FCCCC0195C5F2100095152 /* Info.plist */, 107 | ); 108 | name = "Supporting Files"; 109 | sourceTree = ""; 110 | }; 111 | /* End PBXGroup section */ 112 | 113 | /* Begin PBXHeadersBuildPhase section */ 114 | 50FCCCAC195C5F2100095152 /* Headers */ = { 115 | isa = PBXHeadersBuildPhase; 116 | buildActionMask = 2147483647; 117 | files = ( 118 | 50FCCCB5195C5F2100095152 /* OptJSON.h in Headers */, 119 | ); 120 | runOnlyForDeploymentPostprocessing = 0; 121 | }; 122 | /* End PBXHeadersBuildPhase section */ 123 | 124 | /* Begin PBXNativeTarget section */ 125 | 50FCCCAE195C5F2100095152 /* OptJSON */ = { 126 | isa = PBXNativeTarget; 127 | buildConfigurationList = 50FCCCC5195C5F2100095152 /* Build configuration list for PBXNativeTarget "OptJSON" */; 128 | buildPhases = ( 129 | 50FCCCAA195C5F2100095152 /* Sources */, 130 | 50FCCCAB195C5F2100095152 /* Frameworks */, 131 | 50FCCCAC195C5F2100095152 /* Headers */, 132 | 50FCCCAD195C5F2100095152 /* Resources */, 133 | ); 134 | buildRules = ( 135 | ); 136 | dependencies = ( 137 | ); 138 | name = OptJSON; 139 | productName = OptJSON; 140 | productReference = 50FCCCAF195C5F2100095152 /* OptJSON.framework */; 141 | productType = "com.apple.product-type.framework"; 142 | }; 143 | 50FCCCB9195C5F2100095152 /* OptJSONTests */ = { 144 | isa = PBXNativeTarget; 145 | buildConfigurationList = 50FCCCC8195C5F2100095152 /* Build configuration list for PBXNativeTarget "OptJSONTests" */; 146 | buildPhases = ( 147 | 50FCCCB6195C5F2100095152 /* Sources */, 148 | 50FCCCB7195C5F2100095152 /* Frameworks */, 149 | 50FCCCB8195C5F2100095152 /* Resources */, 150 | ); 151 | buildRules = ( 152 | ); 153 | dependencies = ( 154 | 50FCCCBD195C5F2100095152 /* PBXTargetDependency */, 155 | ); 156 | name = OptJSONTests; 157 | productName = OptJSONTests; 158 | productReference = 50FCCCBA195C5F2100095152 /* OptJSONTests.xctest */; 159 | productType = "com.apple.product-type.bundle.unit-test"; 160 | }; 161 | /* End PBXNativeTarget section */ 162 | 163 | /* Begin PBXProject section */ 164 | 50FCCCA6195C5F2100095152 /* Project object */ = { 165 | isa = PBXProject; 166 | attributes = { 167 | LastUpgradeCheck = 0600; 168 | TargetAttributes = { 169 | 50FCCCAE195C5F2100095152 = { 170 | CreatedOnToolsVersion = 6.0; 171 | }; 172 | 50FCCCB9195C5F2100095152 = { 173 | CreatedOnToolsVersion = 6.0; 174 | TestTargetID = 50FCCCAE195C5F2100095152; 175 | }; 176 | }; 177 | }; 178 | buildConfigurationList = 50FCCCA9195C5F2100095152 /* Build configuration list for PBXProject "OptJSON" */; 179 | compatibilityVersion = "Xcode 3.2"; 180 | developmentRegion = English; 181 | hasScannedForEncodings = 0; 182 | knownRegions = ( 183 | en, 184 | ); 185 | mainGroup = 50FCCCA5195C5F2100095152; 186 | productRefGroup = 50FCCCB0195C5F2100095152 /* Products */; 187 | projectDirPath = ""; 188 | projectRoot = ""; 189 | targets = ( 190 | 50FCCCAE195C5F2100095152 /* OptJSON */, 191 | 50FCCCB9195C5F2100095152 /* OptJSONTests */, 192 | ); 193 | }; 194 | /* End PBXProject section */ 195 | 196 | /* Begin PBXResourcesBuildPhase section */ 197 | 50FCCCAD195C5F2100095152 /* Resources */ = { 198 | isa = PBXResourcesBuildPhase; 199 | buildActionMask = 2147483647; 200 | files = ( 201 | ); 202 | runOnlyForDeploymentPostprocessing = 0; 203 | }; 204 | 50FCCCB8195C5F2100095152 /* Resources */ = { 205 | isa = PBXResourcesBuildPhase; 206 | buildActionMask = 2147483647; 207 | files = ( 208 | ); 209 | runOnlyForDeploymentPostprocessing = 0; 210 | }; 211 | /* End PBXResourcesBuildPhase section */ 212 | 213 | /* Begin PBXSourcesBuildPhase section */ 214 | 50FCCCAA195C5F2100095152 /* Sources */ = { 215 | isa = PBXSourcesBuildPhase; 216 | buildActionMask = 2147483647; 217 | files = ( 218 | 50FCCCCC195C614500095152 /* OptJSON.swift in Sources */, 219 | ); 220 | runOnlyForDeploymentPostprocessing = 0; 221 | }; 222 | 50FCCCB6195C5F2100095152 /* Sources */ = { 223 | isa = PBXSourcesBuildPhase; 224 | buildActionMask = 2147483647; 225 | files = ( 226 | 50FCCCC2195C5F2100095152 /* OptJSONTests.swift in Sources */, 227 | 50A5DEFE1967637F0093A1AD /* AltJSONTests.swift in Sources */, 228 | ); 229 | runOnlyForDeploymentPostprocessing = 0; 230 | }; 231 | /* End PBXSourcesBuildPhase section */ 232 | 233 | /* Begin PBXTargetDependency section */ 234 | 50FCCCBD195C5F2100095152 /* PBXTargetDependency */ = { 235 | isa = PBXTargetDependency; 236 | target = 50FCCCAE195C5F2100095152 /* OptJSON */; 237 | targetProxy = 50FCCCBC195C5F2100095152 /* PBXContainerItemProxy */; 238 | }; 239 | /* End PBXTargetDependency section */ 240 | 241 | /* Begin XCBuildConfiguration section */ 242 | 50FCCCC3195C5F2100095152 /* Debug */ = { 243 | isa = XCBuildConfiguration; 244 | buildSettings = { 245 | ALWAYS_SEARCH_USER_PATHS = NO; 246 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 247 | CLANG_CXX_LIBRARY = "libc++"; 248 | CLANG_ENABLE_MODULES = YES; 249 | CLANG_ENABLE_OBJC_ARC = YES; 250 | CLANG_WARN_BOOL_CONVERSION = YES; 251 | CLANG_WARN_CONSTANT_CONVERSION = YES; 252 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 253 | CLANG_WARN_EMPTY_BODY = YES; 254 | CLANG_WARN_ENUM_CONVERSION = YES; 255 | CLANG_WARN_INT_CONVERSION = YES; 256 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 257 | CLANG_WARN_UNREACHABLE_CODE = YES; 258 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 259 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 260 | COPY_PHASE_STRIP = NO; 261 | CURRENT_PROJECT_VERSION = 1; 262 | ENABLE_STRICT_OBJC_MSGSEND = YES; 263 | GCC_C_LANGUAGE_STANDARD = gnu99; 264 | GCC_DYNAMIC_NO_PIC = NO; 265 | GCC_OPTIMIZATION_LEVEL = 0; 266 | GCC_PREPROCESSOR_DEFINITIONS = ( 267 | "DEBUG=1", 268 | "$(inherited)", 269 | ); 270 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 271 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 272 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 273 | GCC_WARN_UNDECLARED_SELECTOR = YES; 274 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 275 | GCC_WARN_UNUSED_FUNCTION = YES; 276 | GCC_WARN_UNUSED_VARIABLE = YES; 277 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 278 | METAL_ENABLE_DEBUG_INFO = YES; 279 | ONLY_ACTIVE_ARCH = YES; 280 | SDKROOT = iphoneos; 281 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 282 | TARGETED_DEVICE_FAMILY = "1,2"; 283 | VERSIONING_SYSTEM = "apple-generic"; 284 | VERSION_INFO_PREFIX = ""; 285 | }; 286 | name = Debug; 287 | }; 288 | 50FCCCC4195C5F2100095152 /* Release */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ALWAYS_SEARCH_USER_PATHS = NO; 292 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 293 | CLANG_CXX_LIBRARY = "libc++"; 294 | CLANG_ENABLE_MODULES = YES; 295 | CLANG_ENABLE_OBJC_ARC = YES; 296 | CLANG_WARN_BOOL_CONVERSION = YES; 297 | CLANG_WARN_CONSTANT_CONVERSION = YES; 298 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 299 | CLANG_WARN_EMPTY_BODY = YES; 300 | CLANG_WARN_ENUM_CONVERSION = YES; 301 | CLANG_WARN_INT_CONVERSION = YES; 302 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 303 | CLANG_WARN_UNREACHABLE_CODE = YES; 304 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 305 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 306 | COPY_PHASE_STRIP = YES; 307 | CURRENT_PROJECT_VERSION = 1; 308 | ENABLE_NS_ASSERTIONS = NO; 309 | ENABLE_STRICT_OBJC_MSGSEND = YES; 310 | GCC_C_LANGUAGE_STANDARD = gnu99; 311 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 312 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 313 | GCC_WARN_UNDECLARED_SELECTOR = YES; 314 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 315 | GCC_WARN_UNUSED_FUNCTION = YES; 316 | GCC_WARN_UNUSED_VARIABLE = YES; 317 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 318 | METAL_ENABLE_DEBUG_INFO = NO; 319 | SDKROOT = iphoneos; 320 | TARGETED_DEVICE_FAMILY = "1,2"; 321 | VALIDATE_PRODUCT = YES; 322 | VERSIONING_SYSTEM = "apple-generic"; 323 | VERSION_INFO_PREFIX = ""; 324 | }; 325 | name = Release; 326 | }; 327 | 50FCCCC6195C5F2100095152 /* Debug */ = { 328 | isa = XCBuildConfiguration; 329 | buildSettings = { 330 | CLANG_ENABLE_MODULES = YES; 331 | DEFINES_MODULE = YES; 332 | DYLIB_COMPATIBILITY_VERSION = 1; 333 | DYLIB_CURRENT_VERSION = 1; 334 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 335 | INFOPLIST_FILE = OptJSON/Info.plist; 336 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 337 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 338 | PRODUCT_NAME = "$(TARGET_NAME)"; 339 | SKIP_INSTALL = YES; 340 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 341 | }; 342 | name = Debug; 343 | }; 344 | 50FCCCC7195C5F2100095152 /* Release */ = { 345 | isa = XCBuildConfiguration; 346 | buildSettings = { 347 | CLANG_ENABLE_MODULES = YES; 348 | DEFINES_MODULE = YES; 349 | DYLIB_COMPATIBILITY_VERSION = 1; 350 | DYLIB_CURRENT_VERSION = 1; 351 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 352 | INFOPLIST_FILE = OptJSON/Info.plist; 353 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 354 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 355 | PRODUCT_NAME = "$(TARGET_NAME)"; 356 | SKIP_INSTALL = YES; 357 | }; 358 | name = Release; 359 | }; 360 | 50FCCCC9195C5F2100095152 /* Debug */ = { 361 | isa = XCBuildConfiguration; 362 | buildSettings = { 363 | FRAMEWORK_SEARCH_PATHS = ( 364 | "$(SDKROOT)/Developer/Library/Frameworks", 365 | "$(inherited)", 366 | ); 367 | GCC_PREPROCESSOR_DEFINITIONS = ( 368 | "DEBUG=1", 369 | "$(inherited)", 370 | ); 371 | INFOPLIST_FILE = OptJSONTests/Info.plist; 372 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 373 | METAL_ENABLE_DEBUG_INFO = YES; 374 | PRODUCT_NAME = "$(TARGET_NAME)"; 375 | }; 376 | name = Debug; 377 | }; 378 | 50FCCCCA195C5F2100095152 /* Release */ = { 379 | isa = XCBuildConfiguration; 380 | buildSettings = { 381 | FRAMEWORK_SEARCH_PATHS = ( 382 | "$(SDKROOT)/Developer/Library/Frameworks", 383 | "$(inherited)", 384 | ); 385 | INFOPLIST_FILE = OptJSONTests/Info.plist; 386 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 387 | METAL_ENABLE_DEBUG_INFO = NO; 388 | PRODUCT_NAME = "$(TARGET_NAME)"; 389 | }; 390 | name = Release; 391 | }; 392 | /* End XCBuildConfiguration section */ 393 | 394 | /* Begin XCConfigurationList section */ 395 | 50FCCCA9195C5F2100095152 /* Build configuration list for PBXProject "OptJSON" */ = { 396 | isa = XCConfigurationList; 397 | buildConfigurations = ( 398 | 50FCCCC3195C5F2100095152 /* Debug */, 399 | 50FCCCC4195C5F2100095152 /* Release */, 400 | ); 401 | defaultConfigurationIsVisible = 0; 402 | defaultConfigurationName = Release; 403 | }; 404 | 50FCCCC5195C5F2100095152 /* Build configuration list for PBXNativeTarget "OptJSON" */ = { 405 | isa = XCConfigurationList; 406 | buildConfigurations = ( 407 | 50FCCCC6195C5F2100095152 /* Debug */, 408 | 50FCCCC7195C5F2100095152 /* Release */, 409 | ); 410 | defaultConfigurationIsVisible = 0; 411 | defaultConfigurationName = Release; 412 | }; 413 | 50FCCCC8195C5F2100095152 /* Build configuration list for PBXNativeTarget "OptJSONTests" */ = { 414 | isa = XCConfigurationList; 415 | buildConfigurations = ( 416 | 50FCCCC9195C5F2100095152 /* Debug */, 417 | 50FCCCCA195C5F2100095152 /* Release */, 418 | ); 419 | defaultConfigurationIsVisible = 0; 420 | defaultConfigurationName = Release; 421 | }; 422 | /* End XCConfigurationList section */ 423 | }; 424 | rootObject = 50FCCCA6195C5F2100095152 /* Project object */; 425 | } 426 | -------------------------------------------------------------------------------- /OptJSON.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /OptJSON/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.odnoletkov.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /OptJSON/OptJSON.h: -------------------------------------------------------------------------------- 1 | // 2 | // OptJSON.h 3 | // OptJSON 4 | // 5 | // Created by max on 26.06.14. 6 | // 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for OptJSON. 12 | FOUNDATION_EXPORT double OptJSONVersionNumber; 13 | 14 | //! Project version string for OptJSON. 15 | FOUNDATION_EXPORT const unsigned char OptJSONVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /OptJSON/OptJSON.swift: -------------------------------------------------------------------------------- 1 | // 2 | // OptJSON.swift 3 | // OptJSON 4 | // 5 | // Created by max on 26.06.14. 6 | // 7 | // 8 | 9 | import Foundation 10 | 11 | public protocol JSONValue { 12 | subscript(#key: String) -> JSONValue? { get } 13 | subscript(#index: Int) -> JSONValue? { get } 14 | } 15 | 16 | extension NSNull : JSONValue { 17 | public subscript(#key: String) -> JSONValue? { return nil } 18 | public subscript(#index: Int) -> JSONValue? { return nil } 19 | } 20 | 21 | extension NSNumber : JSONValue { 22 | public subscript(#key: String) -> JSONValue? { return nil } 23 | public subscript(#index: Int) -> JSONValue? { return nil } 24 | } 25 | 26 | extension NSString : JSONValue { 27 | public subscript(#key: String) -> JSONValue? { return nil } 28 | public subscript(#index: Int) -> JSONValue? { return nil } 29 | } 30 | 31 | extension NSArray : JSONValue { 32 | public subscript(#key: String) -> JSONValue? { return nil } 33 | public subscript(#index: Int) -> JSONValue? { return index < count && index >= 0 ? JSON(self[index]) : nil } 34 | } 35 | 36 | extension NSDictionary : JSONValue { 37 | public subscript(#key: String) -> JSONValue? { return JSON(self[key]) } 38 | public subscript(#index: Int) -> JSONValue? { return nil } 39 | } 40 | 41 | public func JSON(object: AnyObject?) -> JSONValue? { 42 | if let some: AnyObject = object { 43 | switch some { 44 | case let null as NSNull: return null 45 | case let number as NSNumber: return number 46 | case let string as NSString: return string 47 | case let array as NSArray: return array 48 | case let dict as NSDictionary: return dict 49 | default: return nil 50 | } 51 | } else { 52 | return nil 53 | } 54 | } -------------------------------------------------------------------------------- /OptJSONTests/AltJSONTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AltJSONTests.swift 3 | // OptJSON 4 | // 5 | // Created by Max on 05.07.14. 6 | // 7 | // 8 | 9 | import XCTest 10 | 11 | @objc 12 | protocol AltJSON { 13 | optional func array(index: Int) -> AltJSON? 14 | optional func object(key: String) -> AltJSON? 15 | } 16 | 17 | extension NSArray : AltJSON { 18 | func array(index: Int) -> AltJSON? { return index < count && index >= 0 ? self[index] as? AltJSON : nil } 19 | } 20 | 21 | extension NSDictionary: AltJSON { 22 | func object(key: String) -> AltJSON? { return self[key] as? AltJSON } 23 | } 24 | 25 | extension NSNull : AltJSON {} 26 | 27 | extension NSNumber : AltJSON {} 28 | 29 | extension NSString : AltJSON {} 30 | 31 | let altJSON : AltJSON = [ 32 | "name": "John Smith", 33 | "isAlive": true, 34 | "age": 25, 35 | "height_cm": 167.64, 36 | "pet": NSNull(), 37 | "address": [ 38 | "city": "New York", 39 | ], 40 | "phoneNumbers": [ 41 | [ 42 | "type": "home", 43 | "number": "212 555-1234" 44 | ], 45 | [ 46 | "type": "office", 47 | "number": "646 555-4567", 48 | ] 49 | ], 50 | ] 51 | 52 | class AltJSONTests: XCTestCase { 53 | 54 | let nsJSON : AnyObject! = { 55 | let data = NSJSONSerialization.dataWithJSONObject(altJSON, options: NSJSONWritingOptions(0), error: nil) 56 | return NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(0), error: nil) 57 | }() 58 | 59 | func testBaseTypes() { 60 | XCTAssert(altJSON.object?("pet") as? NSNull == NSNull()) 61 | XCTAssert(altJSON.object?("isAlive") as? NSNumber == true) 62 | XCTAssert(altJSON.object?("age") as? NSNumber == 25) 63 | XCTAssert(altJSON.object?("height_cm") as? NSNumber == 167.64) 64 | XCTAssert(altJSON.object?("name") as? NSString == "John Smith") 65 | XCTAssert(altJSON.object?("phoneNumbers") as? NSArray != nil) 66 | XCTAssert(altJSON.object?("address") as? NSDictionary != nil) 67 | } 68 | 69 | func testOptionalChaining() { 70 | XCTAssert(altJSON.object?("address")?.object?("city") as? NSString == "New York") 71 | XCTAssert(altJSON.object?("phoneNumbers")?.array?(0)?.object?("type") as? NSString == "home") 72 | XCTAssert(altJSON.object?("missing")?.array?(0)?.object?("missing") as? NSNumber == nil) 73 | } 74 | 75 | func testSafeArrayBounds() { 76 | XCTAssert(altJSON.object?("phoneNumbers")?.array?(2) as? NSDictionary == nil) 77 | } 78 | 79 | func testNSJSONSerialization() { 80 | XCTAssert(altJSON.object?("pet") as? NSNull == NSNull()) 81 | XCTAssert(altJSON.object?("isAlive") as? NSNumber == true) 82 | XCTAssert(altJSON.object?("age") as? NSNumber == 25) 83 | XCTAssert(altJSON.object?("height_cm") as? NSNumber == 167.64) 84 | XCTAssert(altJSON.object?("name") as? NSString == "John Smith") 85 | XCTAssert(altJSON.object?("phoneNumbers") as? NSArray != nil) 86 | XCTAssert(altJSON.object?("address") as? NSDictionary != nil) 87 | } 88 | 89 | func testCompareSyntax() { 90 | 91 | if let person = nsJSON as? NSDictionary { 92 | if let phones = person["phoneNumbers"] as? NSArray { 93 | if let phone = phones[1] as? NSDictionary { 94 | if let number = phone["number"] as? String { 95 | XCTAssert(number == "646 555-4567"); 96 | } 97 | } 98 | } 99 | } 100 | 101 | XCTAssert((((nsJSON as? NSDictionary)?.objectForKey("phoneNumbers") as? NSArray)?.objectAtIndex(1) as? NSDictionary)?.objectForKey("number") as? NSString == "646 555-4567"); 102 | 103 | XCTAssert((((nsJSON as? NSDictionary)?["phoneNumbers"] as? NSArray)?[1] as? NSDictionary)?["number"] as? NSString == "646 555-4567"); 104 | 105 | XCTAssert((nsJSON as? AltJSON)?.object?("phoneNumbers")?.array?(1)?.object?("number") as? NSString == "646 555-4567") 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /OptJSONTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.odnoletkov.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /OptJSONTests/OptJSONTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // OptJSONTests.swift 3 | // OptJSONTests 4 | // 5 | // Created by max on 26.06.14. 6 | // 7 | // 8 | 9 | import XCTest 10 | import OptJSON 11 | 12 | let nativeJSON = [ 13 | "name": "John Smith", 14 | "isAlive": true, 15 | "age": 25, 16 | "height_cm": 167.64, 17 | "pet": NSNull(), 18 | "address": [ 19 | "city": "New York", 20 | ], 21 | "phoneNumbers": [ 22 | [ 23 | "type": "home", 24 | "number": "212 555-1234" 25 | ], 26 | [ 27 | "type": "office", 28 | "number": "646 555-4567", 29 | ] 30 | ], 31 | ] 32 | 33 | class OptJSONTests: XCTestCase { 34 | 35 | let nsJSON : AnyObject! = { 36 | let data = NSJSONSerialization.dataWithJSONObject(nativeJSON, options: NSJSONWritingOptions(0), error: nil) 37 | return NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(0), error: nil) 38 | }() 39 | 40 | func testBaseTypes() { 41 | XCTAssert(JSON(nativeJSON)?[key:"pet"] as? NSNull == NSNull()) 42 | XCTAssert(JSON(nativeJSON)?[key:"isAlive"] as? NSNumber == true) 43 | XCTAssert(JSON(nativeJSON)?[key:"age"] as? NSNumber == 25) 44 | XCTAssert(JSON(nativeJSON)?[key:"height_cm"] as? NSNumber == 167.64) 45 | XCTAssert(JSON(nativeJSON)?[key:"name"] as? NSString == "John Smith") 46 | XCTAssert(JSON(nativeJSON)?[key:"phoneNumbers"] as? NSArray != nil) 47 | XCTAssert(JSON(nativeJSON)?[key:"address"] as? NSDictionary != nil) 48 | } 49 | 50 | func testOptionalChaining() { 51 | XCTAssert(JSON(nativeJSON)?[key:"address"]?[key:"city"] as? NSString == "New York") 52 | XCTAssert(JSON(nativeJSON)?[key:"phoneNumbers"]?[index:0]?[key:"type"] as? NSString == "home") 53 | XCTAssert(JSON(nativeJSON)?[key:"missing"]?[index:0]?[key:"missing"] as? NSNumber == nil) 54 | } 55 | 56 | func testSafeArrayBounds() { 57 | XCTAssert(JSON(nativeJSON)?[key:"phoneNumbers"]?[index:2] as? NSDictionary == nil) 58 | } 59 | 60 | func testNSJSONSerialization() { 61 | XCTAssert(JSON(nsJSON)?[key:"pet"] as? NSNull == NSNull()) 62 | XCTAssert(JSON(nsJSON)?[key:"isAlive"] as? NSNumber == true) 63 | XCTAssert(JSON(nsJSON)?[key:"age"] as? NSNumber == 25) 64 | XCTAssert(JSON(nsJSON)?[key:"height_cm"] as? NSNumber == 167.64) 65 | XCTAssert(JSON(nsJSON)?[key:"name"] as? NSString == "John Smith") 66 | XCTAssert(JSON(nsJSON)?[key:"phoneNumbers"] as? NSArray != nil) 67 | XCTAssert(JSON(nsJSON)?[key:"address"] as? NSDictionary != nil) 68 | } 69 | 70 | func testCompareSyntax() { 71 | 72 | if let person = nsJSON as? NSDictionary { 73 | if let phones = person["phoneNumbers"] as? NSArray { 74 | if let phone = phones[1] as? NSDictionary { 75 | if let number = phone["number"] as? String { 76 | XCTAssert(number == "646 555-4567"); 77 | } 78 | } 79 | } 80 | } 81 | 82 | XCTAssert((((nsJSON as? NSDictionary)?.objectForKey("phoneNumbers") as? NSArray)?.objectAtIndex(1) as? NSDictionary)?.objectForKey("number") as? NSString == "646 555-4567"); 83 | 84 | XCTAssert((((nsJSON as? NSDictionary)?["phoneNumbers"] as? NSArray)?[1] as? NSDictionary)?["number"] as? NSString == "646 555-4567"); 85 | 86 | XCTAssert(JSON(nsJSON)?[key:"phoneNumbers"]?[index:1]?[key:"number"] as? NSString == "646 555-4567") 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #OptJSON 2 | 3 | A simple and safe approach to work with JSON objects hierarchies in Swift 4 | 5 | Because of the strict static typing working with dynamic data structures (like JSON) is pain in Swift: 6 | 7 | ```swift 8 | let jsonObject = [ 9 | "name": "John", 10 | "age": 32, 11 | "phoneNumbers" = [ 12 | [ 13 | "type": "home", 14 | "number": "646 555-4567" 15 | ] 16 | ] 17 | ] 18 | 19 | // thanks to runtime bridging we can downcast to Foundation types 20 | if let person = nsJSON as? NSDictionary { 21 | if let phones = person["phoneNumbers"] as? NSArray { 22 | if let phone = phones[0] as? NSDictionary { 23 | if let number = phone["number"] as? String { 24 | assert(number == "646 555-4567") 25 | } 26 | } 27 | } 28 | } 29 | 30 | // alternativley – using optional downcasting and optional chaining 31 | let maybeNumber = (((jsonObject as? NSDictionary)?["phoneNumbers"] as? NSArray)?[0] as? NSDictionary)?["number"] as? NSString 32 | assert(maybeNumber == "646 555-4567") 33 | ``` 34 | 35 | With OptJSON traversing JSON hierarchy is easy: 36 | 37 | ```swift 38 | let maybeNumber = JSON(jsonObject)?[key:"phoneNumbers"]?[index:0]?[key:"number"] as? NSString 39 | XCTAssert(maybeNumber == "646 555-4567") 40 | ``` 41 | 42 | OptJSON works by introducing universal protocol JSONValue, which enables JSON-specific subscripts to access dictionaries and arrays elements. This protocol is then adopted by all JSON-related Foundation types. 43 | 44 | OptJSON enables: 45 | * compact and readable code 46 | * no excessive downcasting 47 | * easy subscripts chaining to go deep in a single expression 48 | * safe runtime – you'll get nil if your chain doesn't match actual JSON hierarchy 49 | * minimal CPU/memory overhead 50 | * work with native JSON object representation - no extra conversion needed 51 | --------------------------------------------------------------------------------