0001 // 0002 // JsonPropertyLoader.swift 0003 // Swinject 0004 // 0005 // Created by mike.owens on 12/6/15. 0006 // Copyright © 2015 Swinject Contributors. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 0012 /// The JsonPropertyLoader will load properties from JSON resources 0013 final public class JsonPropertyLoader{ 0014 0015 /// the bundle where the resource exists (defualts to mainBundle) 0016 private let bundle
JsonPropertyLoader.swift:67 extension JsonPropertyLoader: PropertyLoaderType {: NSBundle 0017 0018 /// the name of the JSON resource. For example, if your resource is "properties.json" then this value will be set to "properties" 0019 private let name
JsonPropertyLoader.swift:29 self.bundle = bundle!JsonPropertyLoader.swift:69 let contents = try loadStringFromBundle(bundle, withName: name, ofType: "json")JsonPropertyLoader.swift:75 throw PropertyLoaderError.InvalidJSONFormat(bundle: bundle, name: name): String 0020 0021 /// 0022 /// Will create a JSON property loader 0023 /// 0024 /// - parameter bundle: the bundle where the resource exists (defaults to mainBundle) 0025 /// - parameter name: the name of the JSON resource. For example, if your resource is "properties.json" 0026 /// then this value will be set to "properties" 0027 /// 0028 public init(bundle: NSBundle? = .mainBundle(), name: String) { 0029 self.bundle = bundle! 0030 self.name = name 0031 } 0032 0033 /// Will strip the provide string of comments. This allows JSON property files to contain comments as it 0034 /// is valuable to provide more context to a property then just its key-value and comments are not valid JSON 0035 /// so this will process the JSON string before we attempt to parse the JSON into objects 0036 /// 0037 /// Implementation influence by Typhoon: 0038 /// https://github.com/appsquickly/Typhoon/blob/master/Source/Configuration/ConfigPostProcessor/TyphoonConfiguration/TyphoonJsonStyleConfiguration.m#L30 0039 /// 0040 /// - Parameter str: the string to strip of comments 0041 /// 0042 /// - Returns: the json string stripper of comments 0043 private func stringWithoutComments
JsonPropertyLoader.swift:30 self.name = nameJsonPropertyLoader.swift:69 let contents = try loadStringFromBundle(bundle, withName: name, ofType: "json")JsonPropertyLoader.swift:75 throw PropertyLoaderError.InvalidJSONFormat(bundle: bundle, name: name)(str: String) -> String { 0044 let pattern = "(([\"'])(?:\\\\\\2|.)*?\\2)|(\\/\\/[^\\n\\r]*(?:[\\n\\r]+|$)|(\\/\\*(?:(?!\\*\\/).|[\\n\\r])*\\*\\/))" 0045 let expression = try? NSRegularExpression(pattern: pattern, options: .AnchorsMatchLines) 0046 0047 let matches = expression!.matchesInString(str, options: NSMatchingOptions(rawValue: 0), 0048 range: NSRange(location: 0, length: str.characters.count)) 0049 0050 guard !matches.isEmpty else { 0051 return str 0052 } 0053 0054 let ret = NSMutableString(string: str) 0055 0056 for match in matches.reverse() { 0057 let character = String(str[str.startIndex.advancedBy(match.range.location)]) 0058 if character != "\'" && character != "\"" { 0059 ret.replaceCharactersInRange(match.range, withString: "") 0060 } 0061 } 0062 return ret as String 0063 } 0064 } 0065 0066 // MARK: - PropertyLoadable 0067 extension JsonPropertyLoader: PropertyLoaderType { 0068 public func load() throws -> [String : AnyObject] { 0069 let contents = try loadStringFromBundle(bundle, withName: name, ofType: "json") 0070 let jsonWithoutComments = stringWithoutComments(contents) 0071 let data = jsonWithoutComments.dataUsingEncoding(NSUTF8StringEncoding) 0072 0073 let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) 0074 guard let props = json as? [String:AnyObject] else { 0075 throw PropertyLoaderError.InvalidJSONFormat(bundle: bundle, name: name) 0076 } 0077 return props 0078 0079 } 0080 } 0081
JsonPropertyLoader.swift:70 let jsonWithoutComments = stringWithoutComments(contents)