Swiftpack.co - iamjono/JSONConfig as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by iamjono.
iamjono/JSONConfig 3.1.0
JSON Configuration reader for server side swift
⭐️ 7
🕓 5 years ago
.package(url: "https://github.com/iamjono/JSONConfig.git", from: "3.1.0")

JSON Config

A Swift 3 JSON Config reader library. Reads JSON files for server side configuration.

JSON files can be used for maintaining system specific settings such as database names, credentials, api keys, access tokens. It makes no sense to include these in compiled binaries as they will vary between development, staging and deployment environments, or between distributions.

Setup

Include the JSON Config dependancy in your project's Package.swift file:

 .Package(url: "https://github.com/iamjono/JSONConfig.git", majorVersion: 3)

After changing your Package.swift file, remember to rebuild your Xcode project:

swift package generate-xcodeproj

Open your project, and add a new JSON file, and add the property key-value pairs in the same way as is seen in the example ApplicationConfiguration.json.

In the "Build Phases" for your target binary, add a "Copy Files" phase if one does not already exist. Add your JSON file to the file list, with a destination of "Resources". When you build your project within Xcode this JSON file will be copied next to your executable binary.

Example usage:

// perhaps in main.swift
#if os(Linux)
	let FileRoot = "/home/ubuntu/settings/"
#else
	let FileRoot = ""
#endif

// wherever you like
func GetAPIKey() -> String {
	if let config = JSONConfig(name: "\(FileRoot)ApplicationConfiguration.json") {
		//2
		let dict = config.getValues()!
		let apiKey = dict["apikey"] as! String
		if apiKey.characters.count > 0 {
			return apiKey
		} else {
			print("No API Key")
		}
	} else {
		print("Unable to get Configuration")
	}
	return ""
}

For Linux usage, use a context switch for location, like:

GitHub

link
Stars: 7
Last commit: 5 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics