A lightweight Swift package that converts JSONC (JSON with Comments) to standard JSON.
- Zero dependencies
- Single-pass, length-preserving conversion
- Lenient — malformed input is passed through for downstream parsers to handle
- Swift 6 strict concurrency ready
- Strip single-line comments (
// ...) - Strip multi-line comments (
/* ... */) - Remove trailing commas in objects and arrays
Add JSONCKit to your Package.swift:
dependencies: [
.package(url: "https://github.com/steelbrain/JSONCKit.git", from: "1.0.0"),
]Then add it as a dependency to your target:
.target(
name: "YourTarget",
dependencies: ["JSONCKit"]
),import JSONCKit
let jsonc = """
{
// Database settings
"host": "localhost",
"port": 5432,
/* Credentials */
"user": "admin",
}
"""
let json = JSONC.convert(jsonc)
// json is now valid JSON with comments and trailing comma removedlet jsoncData: Data = ...
let jsonData = try JSONC.convertToData(jsoncData)struct Config: Decodable {
let host: String
let port: Int
let user: String
}
let config = try JSONC.decode(Config.self, from: jsoncData)| Method | Signature | Description |
|---|---|---|
convert |
(String) -> String |
Convert a JSONC string to JSON |
convertToData |
(Data, encoding:) throws -> Data |
Convert JSONC data to JSON data |
decode |
(T.Type, from:encoding:decoder:) throws -> T |
Decode a Decodable type from JSONC data |
The converter makes a single pass over the input bytes, replacing comments and trailing commas with whitespace. The output is always the exact same byte length as the input, so downstream parsers report correct line and column positions for any errors they find.
See ATTRIBUTIONS.md for implementation and test suite references.
MIT — see LICENSE for details.