Swift Package Index


A JSON configuration utility for Perfect Swift


  • The latest stable release is 1.1.3. Released 2 years ago.
  • The last commit to master was 2 years ago.

Swift Version Compatibility

  • 1.1.3 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 1.1.3 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

JSONConfig 简体中文

This is a simple json based configuration library for Perfect.

Rather than presenting a flat json file for your configuration needs, this library lets you organize it a bit more.

Setup:

Include the JSONConfig dependency in your project's Package.swift file:

.package(url: "https://github.com/AutomatonTec/JSONConfig.git", from: "1.1.3")

// section dependencies
dependencies: ["JSONConfig"]

Rebuild your Xcode project after changing your Package.swift file.

swift package generate-xcodeproj

Example usage:

import JSONConfig
// somewhere, perhaps in main.swift, determine the path to your config file

let configServerDefaults = "./config/server/common.json"
#if os(Linux)
    let configServer = "./config/server/linux.json"
#else
    let configServer = "./config/server/macOS.json"
#endif

let config = JSONConfig(withJsonAt:configServer, defaultsInJsonAt:configServerDefaults)

// somewhere, anywhere
func setupDatabase(withConfig config:JSONConfig) {
    MySQLConnector.host     = config.string(forKeyPath: "database.host", otherwise: "127.0.0.1")
    MySQLConnector.username = config.string(forKeyPath: "database.username", otherwise: "db_user")
    MySQLConnector.password = config.string(forKeyPath: "database.password", otherwise: "best_password")
    MySQLConnector.database = config.string(forKeyPath: "database.database", otherwise: "db_user")
}

func setupServer(withConfig config:JSONConfig, server:HTTPServer) {
    server.serverName = config.string(forKeyPath: "server.name", otherwise: "sub.your-domain.com")
    server.serverPort = UInt16(config.integer(forKeyPath: "server.port", otherwise: 8080))
}

In your common json configuration file, you might have something like:

{
	"server": {
		"name" : "api.your-domain.com",
		"port" : 80
	},
    "database": {
        "host":     "127.0.0.1",
        "username": "db_bob",
        "password": "bob_password",
        "database": "db_bob"
    }
}

And in your macOS json configuration file, you might have something like:

{
	"server": {
		"name" : "localhost",
		"port" : 8181
	}
}