### Specify Package Traits for swift-yyjson Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Customize the yyjson library at compile time using package traits. This example shows how to include the 'noWriter' and 'strictStandardJSON' traits when adding the package dependency. ```swift .package( url: "https://github.com/mattt/swift-yyjson.git", from: "0.3.0", traits: ["noWriter", "strictStandardJSON"] // Example traits ) ``` -------------------------------- ### Host System Information for Benchmarks Source: https://github.com/mattt/swift-yyjson/blob/main/README.md This output provides details about the host machine where the benchmarks were run, including the processor type, number of cores, memory, and operating system kernel version. This information is crucial for understanding the context of benchmark results. ```console Host 'MacBook-Pro.local' with 16 'arm64' processors with 48 GB memory, running: Darwin Kernel Version 25.2.0: Tue Nov 18 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6041 ``` -------------------------------- ### Run Swift Package Benchmarks Source: https://github.com/mattt/swift-yyjson/blob/main/README.md This command runs Swift package benchmarks, filtering for parsing operations across different fixtures and outputting results in markdown format. It's useful for comparing the performance of different JSON parsing implementations. ```shell swift package benchmark --format markdown --filter "Fixture/.+/Parse/.+" --time-units microseconds ``` -------------------------------- ### Benchmark Results for canada.json Parse Foundation Source: https://github.com/mattt/swift-yyjson/blob/main/README.md This table displays detailed performance metrics for parsing the 'canada.json' fixture using Foundation. It includes instructions, memory usage, throughput, and time metrics, with various percentile values. ```markdown | Metric | p0 | p25 | p50 | p75 | p90 | p99 | p100 | Samples | | :--------------------------- | ------: | ------: | ------: | ------: | ------: | ------: | ------: | ------: | | Instructions (M) * | 308 | 308 | 308 | 308 | 309 | 312 | 312 | 85 | | Malloc (total) (K) * | 167 | 167 | 167 | 167 | 167 | 167 | 167 | 85 | | Memory (resident peak) (M) | 17 | 148 | 274 | 394 | 478 | 524 | 524 | 85 | | Throughput (# / s) (#) | 88 | 85 | 85 | 84 | 83 | 82 | 82 | 85 | | Time (total CPU) (μs) * | 11425 | 11731 | 11821 | 11969 | 12034 | 12234 | 12234 | 85 | | Time (wall clock) (μs) * | 11419 | 11723 | 11821 | 11969 | 12034 | 12227 | 12227 | 85 | ``` -------------------------------- ### Benchmark Results for citm_catalog.json Parse Foundation Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Performance metrics for parsing 'citm_catalog.json' with Foundation. This data helps in understanding the baseline performance of standard JSON parsing for medium-sized JSON files. ```markdown | Metric | p0 | p25 | p50 | p75 | p90 | p99 | p100 | Samples | | :--------------------------- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ------: | | Instructions (M) * | 73 | 73 | 73 | 73 | 73 | 73 | 74 | 297 | | Malloc (total) (K) * | 14 | 14 | 14 | 14 | 14 | 14 | 14 | 297 | | Memory (resident peak) (M) | 18 | 90 | 161 | 230 | 276 | 301 | 301 | 297 | | Throughput (# / s) (#) | 312 | 304 | 301 | 296 | 284 | 276 | 273 | 297 | | Time (total CPU) (μs) * | 3205 | 3293 | 3330 | 3383 | 3521 | 3633 | 3660 | 297 | | Time (wall clock) (μs) * | 3203 | 3291 | 3328 | 3381 | 3518 | 3629 | 3659 | 297 | ``` -------------------------------- ### Configure JSON Writing Options in Swift Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Configure JSON output formatting using `YYJSONWriteOptions`. Options like `.prettyPrinted` and `.escapeSlashes` control the appearance and character escaping of the generated JSON. ```swift var encoder = YYJSONEncoder() encoder.writeOptions = [.prettyPrinted, .escapeSlashes] ``` -------------------------------- ### Configure YYJSONDecoder Strategies Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Configure YYJSONDecoder with various decoding strategies, similar to the standard JSONDecoder, to handle different JSON formats and data types. ```swift let decoder = YYJSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase decoder.dateDecodingStrategy = .iso8601 decoder.dataDecodingStrategy = .base64 ``` -------------------------------- ### Enable JSON5 Parsing in Swift Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Configure YYJSONDecoder to allow JSON5 features for more flexible input. This can be enabled globally or by specifying individual features like trailing commas, comments, and literals. ```swift let decoder = YYJSONDecoder() decoder.allowsJSON5 = true // Enable all JSON5 features ``` ```swift decoder.allowsJSON5 = .init( trailingCommas: true, // Allow [1, 2, 3,] comments: true, // Allow // and /* */ comments infAndNaN: true, // Allow Infinity and NaN literals singleQuotedStrings: true // Allow 'single quotes' ) ``` -------------------------------- ### Benchmark Results for canada.json Parse YYJSON Source: https://github.com/mattt/swift-yyjson/blob/main/README.md This table shows performance metrics for parsing 'canada.json' using YYJSON. It highlights significantly lower memory allocations and higher throughput compared to Foundation, demonstrating YYJSON's efficiency. ```markdown | Metric | p0 | p25 | p50 | p75 | p90 | p99 | p100 | Samples | | :--------------------------- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ------: | | Instructions (M) * | 35 | 35 | 35 | 35 | 35 | 35 | 35 | 790 | | Malloc (total) * | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 790 | | Memory (resident peak) (M) | 17 | 22 | 22 | 22 | 22 | 22 | 22 | 790 | | Throughput (# / s) (#) | 861 | 810 | 802 | 795 | 787 | 760 | 745 | 790 | | Time (total CPU) (μs) * | 1163 | 1236 | 1249 | 1261 | 1274 | 1318 | 1344 | 790 | | Time (wall clock) (μs) * | 1162 | 1234 | 1247 | 1258 | 1271 | 1316 | 1342 | 790 | ``` -------------------------------- ### Configure JSON Reading Options in Swift Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Use `YYJSONReadOptions` to customize JSON parsing behavior. Options like `.allowComments` and `.allowTrailingCommas` can be combined to handle non-standard JSON formats. ```swift let value = try YYJSONValue(data: data, options: [.allowComments, .allowTrailingCommas]) ``` -------------------------------- ### Use YYJSONSerialization as JSONSerialization Alternative Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Employ YYJSONSerialization with an API identical to Foundation's JSONSerialization for converting between JSON and Swift objects. Supports various writing options for output formatting. ```swift import YYJSON let json = #"{"message": "Hello, World!"}" let data = json.data(using: .utf8)! let object = try YYJSONSerialization.jsonObject(with: data) if let dict = object as? [String: Any] { print(dict["message"] as? String ?? "") // "Hello, World!" } ``` ```swift // Pretty printing with 2-space indent (useful for Xcode asset catalogs) let data = try YYJSONSerialization.data( withJSONObject: dict, options: [.indentationTwoSpaces, .sortedKeys] ) ``` ```swift // ASCII-only output with trailing newline let data = try YYJSONSerialization.data( withJSONObject: dict, options: [.escapeUnicode, .newlineAtEnd] ) ``` -------------------------------- ### Add Swift YYJSON Dependency Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Add the Swift YYJSON library to your project's dependencies using Swift Package Manager. ```swift dependencies: [ .package(url: "https://github.com/mattt/swift-yyjson.git", from: "0.3.0") ] ``` -------------------------------- ### Encode Swift Objects with YYJSONEncoder Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Use YYJSONEncoder for encoding Swift objects conforming to Codable, similar to the standard JSONEncoder. Configure output formatting and date encoding strategies. ```swift import YYJSON let user = User(id: 1, name: "Alice", email: "alice@example.com") let encoder = YYJSONEncoder() let data = try encoder.encode(user) print(String(data: data, encoding: .utf8)!) // {"id":1,"name":"Alice","email":"alice@example.com"} ``` ```swift var encoder = YYJSONEncoder() encoder.writeOptions = [.prettyPrinted, .escapeUnicode] ``` ```swift var encoder = YYJSONEncoder() encoder.dateEncodingStrategy = .iso8601 // Or: .secondsSince1970, .millisecondsSince1970, .formatted(formatter), .custom(closure) ``` -------------------------------- ### Perform In-Place JSON Parsing in Swift Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Utilize YYJSONValue.parseInPlace to parse large JSON data directly within the input buffer, avoiding data copying for maximum performance. Note that the input data is consumed. ```swift var data = try Data(contentsOf: fileURL) let json = try YYJSONValue.parseInPlace(consuming: &data) // `data` is now consumed and should not be used ``` -------------------------------- ### Decode JSON with YYJSONDecoder Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Use YYJSONDecoder to decode JSON data into Swift Codable types. This decoder can be used as a direct replacement for the standard JSONDecoder. ```swift import YYJSON struct User: Codable { let id: Int let name: String let email: String } let json = #"{"id": 1, "name": "Alice", "email": "alice@example.com"}"# let data = json.data(using: .utf8)! let decoder = YYJSONDecoder() let user = try decoder.decode(User.self, from: data) print(user.name) // "Alice" ``` -------------------------------- ### Access JSON with DOM-Style in Swift Source: https://github.com/mattt/swift-yyjson/blob/main/README.md Parse JSON into a YYJSONValue and access nested data using subscripts without defining explicit types. This is useful for dynamic JSON structures. ```swift import YYJSON let json = #"{"users": [{"name": "Alice"}, {"name": "Bob"}]} ``` ```swift let value = try YYJSONValue(string: json) // Access nested values with subscripts if let name = value["users"]?[0]?["name"]?.string { print(name) // "Alice" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.