|
| 1 | +// |
| 2 | +// Copyright © 2026 Mattt (https://github.com/mattt) |
| 3 | +// Copyright © 2026 reers. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +import yyjson |
| 24 | +import Foundation |
| 25 | + |
| 26 | +/// Options for reading JSON data. |
| 27 | +public struct JSONReadOptions: OptionSet, Sendable { |
| 28 | + public let rawValue: UInt32 |
| 29 | + |
| 30 | + public init(rawValue: UInt32) { |
| 31 | + self.rawValue = rawValue |
| 32 | + } |
| 33 | + |
| 34 | + /// Default option (RFC 8259 compliant). |
| 35 | + public static let `default` = JSONReadOptions([]) |
| 36 | + |
| 37 | + /// Stops when done instead of issuing an error if there's additional content |
| 38 | + /// after a JSON document. |
| 39 | + public static let stopWhenDone = JSONReadOptions(rawValue: YYJSON_READ_STOP_WHEN_DONE) |
| 40 | + |
| 41 | + /// Read all numbers as raw strings. |
| 42 | + public static let numberAsRaw = JSONReadOptions(rawValue: YYJSON_READ_NUMBER_AS_RAW) |
| 43 | + |
| 44 | + /// Allow reading invalid unicode when parsing string values. |
| 45 | + public static let allowInvalidUnicode = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_INVALID_UNICODE) |
| 46 | + |
| 47 | + /// Read big numbers as raw strings. |
| 48 | + public static let bigNumberAsRaw = JSONReadOptions(rawValue: YYJSON_READ_BIGNUM_AS_RAW) |
| 49 | + |
| 50 | + /// Allow single trailing comma at the end of an object or array. |
| 51 | + public static let allowTrailingCommas = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_TRAILING_COMMAS) |
| 52 | + |
| 53 | + /// Allow C-style single-line and multi-line comments. |
| 54 | + public static let allowComments = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_COMMENTS) |
| 55 | + |
| 56 | + /// Allow inf/nan number and literal, case-insensitive. |
| 57 | + public static let allowInfAndNaN = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_INF_AND_NAN) |
| 58 | + |
| 59 | + /// Allow UTF-8 BOM and skip it before parsing. |
| 60 | + public static let allowBOM = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_BOM) |
| 61 | + |
| 62 | + /// Allow extended number formats (hex, leading/trailing decimal point, leading plus). |
| 63 | + public static let allowExtendedNumbers = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_EXT_NUMBER) |
| 64 | + |
| 65 | + /// Allow extended escape sequences in strings. |
| 66 | + public static let allowExtendedEscapes = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_EXT_ESCAPE) |
| 67 | + |
| 68 | + /// Allow extended whitespace characters. |
| 69 | + public static let allowExtendedWhitespace = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_EXT_WHITESPACE) |
| 70 | + |
| 71 | + /// Allow strings enclosed in single quotes. |
| 72 | + public static let allowSingleQuotedStrings = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_SINGLE_QUOTED_STR) |
| 73 | + |
| 74 | + /// Allow object keys without quotes. |
| 75 | + public static let allowUnquotedKeys = JSONReadOptions(rawValue: YYJSON_READ_ALLOW_UNQUOTED_KEY) |
| 76 | + |
| 77 | + /// Allow JSON5 format. |
| 78 | + /// |
| 79 | + /// This includes trailing commas, comments, inf/nan, extended numbers, |
| 80 | + /// extended escapes, extended whitespace, single-quoted strings, and unquoted keys. |
| 81 | + public static let json5 = JSONReadOptions(rawValue: YYJSON_READ_JSON5) |
| 82 | + |
| 83 | + /// Convert to yyjson read flags. |
| 84 | + internal var yyjsonFlags: yyjson_read_flag { |
| 85 | + yyjson_read_flag(rawValue) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Options for writing JSON data. |
| 90 | +public struct JSONWriteOptions: OptionSet, Sendable { |
| 91 | + public let rawValue: UInt32 |
| 92 | + |
| 93 | + public init(rawValue: UInt32) { |
| 94 | + self.rawValue = rawValue |
| 95 | + } |
| 96 | + |
| 97 | + /// Default option (minified output). |
| 98 | + public static let `default` = JSONWriteOptions([]) |
| 99 | + |
| 100 | + /// Write JSON pretty with 4 space indent. |
| 101 | + public static let prettyPrinted = JSONWriteOptions(rawValue: YYJSON_WRITE_PRETTY) |
| 102 | + |
| 103 | + /// Write JSON pretty with 2 space indent (implies `prettyPrinted`). |
| 104 | + public static let indentationTwoSpaces = JSONWriteOptions(rawValue: YYJSON_WRITE_PRETTY_TWO_SPACES) |
| 105 | + |
| 106 | + /// Escape unicode as `\uXXXX`, making the output ASCII only. |
| 107 | + public static let escapeUnicode = JSONWriteOptions(rawValue: YYJSON_WRITE_ESCAPE_UNICODE) |
| 108 | + |
| 109 | + /// Escape '/' as '\/'. |
| 110 | + public static let escapeSlashes = JSONWriteOptions(rawValue: YYJSON_WRITE_ESCAPE_SLASHES) |
| 111 | + |
| 112 | + /// Writes infinity and NaN values as `Infinity` and `NaN` literals. |
| 113 | + /// |
| 114 | + /// If you set `infAndNaNAsNull`, it takes precedence. |
| 115 | + public static let allowInfAndNaN = JSONWriteOptions(rawValue: YYJSON_WRITE_ALLOW_INF_AND_NAN) |
| 116 | + |
| 117 | + /// Writes infinity and NaN values as `null` literals. |
| 118 | + /// |
| 119 | + /// This option takes precedence over `allowInfAndNaN`. |
| 120 | + public static let infAndNaNAsNull = JSONWriteOptions(rawValue: YYJSON_WRITE_INF_AND_NAN_AS_NULL) |
| 121 | + |
| 122 | + /// Allow invalid unicode when encoding string values. |
| 123 | + public static let allowInvalidUnicode = JSONWriteOptions(rawValue: YYJSON_WRITE_ALLOW_INVALID_UNICODE) |
| 124 | + |
| 125 | + /// Add a newline character at the end of the JSON. |
| 126 | + public static let newlineAtEnd = JSONWriteOptions(rawValue: YYJSON_WRITE_NEWLINE_AT_END) |
| 127 | + |
| 128 | + /// Sorts object keys lexicographically. |
| 129 | + public static let sortedKeys = JSONWriteOptions(rawValue: 1 << 16) |
| 130 | + |
| 131 | + // Mask for Swift-only flags (bits 16+) that should not be passed to yyjson C library |
| 132 | + private static let swiftOnlyFlagsMask: UInt32 = 0xFFFF_0000 |
| 133 | + |
| 134 | + /// Convert to yyjson write flags, excluding Swift-only flags. |
| 135 | + internal var yyjsonFlags: yyjson_write_flag { |
| 136 | + // Only pass bits 0-15 to yyjson C library; bits 16+ are Swift-only flags |
| 137 | + yyjson_write_flag(rawValue & ~JSONWriteOptions.swiftOnlyFlagsMask) |
| 138 | + } |
| 139 | +} |
0 commit comments