Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iamjono/JSONConfig
JSON Configuration reader for server side swift
https://github.com/iamjono/JSONConfig
Last synced: 3 months ago
JSON representation
JSON Configuration reader for server side swift
- Host: GitHub
- URL: https://github.com/iamjono/JSONConfig
- Owner: iamjono
- Created: 2016-09-14T17:26:24.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-26T19:34:33.000Z (over 5 years ago)
- Last Synced: 2024-07-29T23:17:09.270Z (4 months ago)
- Language: Swift
- Homepage:
- Size: 9.77 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- TheList - JSON Config - A Swift 3 JSON Config reader library. Reads JSON files for server side configuration. (Server Side Utilities / Configuration)
README
# 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:
``` swift
.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](https://github.com/iamjono/Swifty-pList/blob/master/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:
``` swift
// 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: