diff --git a/README.md b/README.md index b320328..0c172ca 100644 --- a/README.md +++ b/README.md @@ -300,10 +300,10 @@ if let dict = object as? [String: Any] { Configure output formatting with `WritingOptions`: ```swift -// 2-space indentation (useful for Xcode asset catalogs) +// Pretty printing with 2-space indent (useful for Xcode asset catalogs) let data = try YYJSONSerialization.data( withJSONObject: dict, - options: [.prettyPrintedTwoSpaces, .sortedKeys] + options: [.indentationTwoSpaces, .sortedKeys] ) // ASCII-only output with trailing newline @@ -319,7 +319,7 @@ Available writing options: - `.prettyPrinted` — Pretty print with 4-space indent - `.sortedKeys` — Sort dictionary keys lexicographically - `.withoutEscapingSlashes` — Don't escape `/` as `\/` -- `.prettyPrintedTwoSpaces` — Pretty print with 2-space indent (overrides `.prettyPrinted`) +- `.indentationTwoSpaces` — Configure pretty printing to use 2-space indent (implies `.prettyPrinted`) - `.escapeUnicode` — Escape non-ASCII characters as `\uXXXX` - `.newlineAtEnd` — Add trailing newline `\n` @@ -365,7 +365,7 @@ encoder.writeOptions = [.prettyPrinted, .escapeSlashes] Available options: - `.prettyPrinted` — Pretty print with 4-space indent -- `.prettyPrintedTwoSpaces` — Pretty print with 2-space indent +- `.indentationTwoSpaces` — Pretty print with 2-space indent (implies `.prettyPrinted`) - `.escapeUnicode` — Escape non-ASCII as `\uXXXX` - `.escapeSlashes` — Escape `/` as `\/` - `.allowInvalidUnicode` — Allow invalid unicode when encoding diff --git a/Sources/YYJSON/Configuration.swift b/Sources/YYJSON/Configuration.swift index c83dad6..c7ec75b 100644 --- a/Sources/YYJSON/Configuration.swift +++ b/Sources/YYJSON/Configuration.swift @@ -82,8 +82,8 @@ public struct YYJSONWriteOptions: OptionSet, Sendable { /// Write JSON pretty with 4 space indent. public static let prettyPrinted = YYJSONWriteOptions(rawValue: YYJSON_WRITE_PRETTY) - /// Write JSON pretty with 2 space indent. - public static let prettyPrintedTwoSpaces = YYJSONWriteOptions(rawValue: YYJSON_WRITE_PRETTY_TWO_SPACES) + /// Write JSON pretty with 2 space indent (implies `prettyPrinted`). + public static let indentationTwoSpaces = YYJSONWriteOptions(rawValue: YYJSON_WRITE_PRETTY_TWO_SPACES) /// Escape unicode as `\uXXXX`, making the output ASCII only. public static let escapeUnicode = YYJSONWriteOptions(rawValue: YYJSON_WRITE_ESCAPE_UNICODE) diff --git a/Sources/YYJSON/Serialization.swift b/Sources/YYJSON/Serialization.swift index a651c80..a8c17fa 100644 --- a/Sources/YYJSON/Serialization.swift +++ b/Sources/YYJSON/Serialization.swift @@ -55,8 +55,8 @@ public enum YYJSONSerialization { public static let withoutEscapingSlashes = WritingOptions(rawValue: 1 << 3) /// Specifies that the output uses white space and 2-space indentation. - /// This flag overrides `prettyPrinted` if both are set. - public static let prettyPrintedTwoSpaces = WritingOptions(rawValue: 1 << 4) + /// This configures `prettyPrinted` to use 2-space indentation. + public static let indentationTwoSpaces = WritingOptions(rawValue: 1 << 4) /// Escape non-ASCII characters in string values as `\uXXXX`, making the output ASCII only. /// Scalars outside the BMP are emitted as surrogate pairs. @@ -152,7 +152,7 @@ public enum YYJSONSerialization { var flags: yyjson_write_flag = 0 // Pretty printing: 2-space overrides 4-space - if options.contains(.prettyPrintedTwoSpaces) { + if options.contains(.indentationTwoSpaces) { flags |= YYJSON_WRITE_PRETTY_TWO_SPACES } else if options.contains(.prettyPrinted) { flags |= YYJSON_WRITE_PRETTY @@ -261,8 +261,8 @@ public enum YYJSONSerialization { } var writeOptions: YYJSONWriteOptions = [] - if options.contains(.prettyPrintedTwoSpaces) { - writeOptions.insert(.prettyPrintedTwoSpaces) + if options.contains(.indentationTwoSpaces) { + writeOptions.insert(.indentationTwoSpaces) } else if options.contains(.prettyPrinted) { writeOptions.insert(.prettyPrinted) } diff --git a/Tests/YYJSONTests/ConfigurationTests.swift b/Tests/YYJSONTests/ConfigurationTests.swift index 1993316..4c97f42 100644 --- a/Tests/YYJSONTests/ConfigurationTests.swift +++ b/Tests/YYJSONTests/ConfigurationTests.swift @@ -167,9 +167,9 @@ import Testing #expect(json.contains(" ")) } - @Test func prettyPrintedTwoSpacesOption() throws { + @Test func indentationTwoSpacesOption() throws { var encoder = YYJSONEncoder() - encoder.writeOptions = .prettyPrintedTwoSpaces + encoder.writeOptions = .indentationTwoSpaces struct Simple: Codable { let key: String diff --git a/Tests/YYJSONTests/EncoderTests.swift b/Tests/YYJSONTests/EncoderTests.swift index 97bd9a4..feffb3e 100644 --- a/Tests/YYJSONTests/EncoderTests.swift +++ b/Tests/YYJSONTests/EncoderTests.swift @@ -415,9 +415,9 @@ import Testing #expect(result.contains(" ")) } - @Test func encodePrettyPrintedTwoSpaces() throws { + @Test func encodeIndentationTwoSpaces() throws { var encoder = YYJSONEncoder() - encoder.writeOptions = .prettyPrintedTwoSpaces + encoder.writeOptions = .indentationTwoSpaces let value = SimpleStruct(name: "test", value: 42) let data = try encoder.encode(value) let result = String(data: data, encoding: .utf8)! @@ -501,14 +501,14 @@ import Testing #expect(outerA < outerZ) } - @Test func encodeSortedKeysPrettyTwoSpaces() throws { + @Test func encodeSortedKeysIndentationTwoSpaces() throws { struct Item: Encodable { let b: Int let a: Int } var encoder = YYJSONEncoder() - encoder.writeOptions = [.prettyPrintedTwoSpaces, .sortedKeys] + encoder.writeOptions = [.indentationTwoSpaces, .sortedKeys] let data = try encoder.encode(Item(b: 2, a: 1)) let json = String(data: data, encoding: .utf8)! diff --git a/Tests/YYJSONTests/SerializationTests.swift b/Tests/YYJSONTests/SerializationTests.swift index a68fc13..3a2e9e9 100644 --- a/Tests/YYJSONTests/SerializationTests.swift +++ b/Tests/YYJSONTests/SerializationTests.swift @@ -248,17 +248,17 @@ import Testing #expect(json.contains("\n")) } - @Test func writeYYJSONValuePrettyPrintedTwoSpaces() throws { + @Test func writeYYJSONValueIndentationTwoSpaces() throws { let value = try YYJSONValue(string: #"{"key":"value"}"#) let json = try Self.jsonString( for: value, - options: [.prettyPrintedTwoSpaces] + options: [.indentationTwoSpaces] ) #expect(json.contains(" \"key\"")) #expect(!json.contains(" \"key\"")) } - @Test func writeYYJSONObjectPrettyPrintedTwoSpaces() throws { + @Test func writeYYJSONObjectIndentationTwoSpaces() throws { let value = try YYJSONValue(string: #"{"key":"value"}"#) guard let object = value.object else { Issue.record("Expected object") @@ -266,13 +266,13 @@ import Testing } let json = try Self.jsonString( for: object, - options: [.prettyPrintedTwoSpaces] + options: [.indentationTwoSpaces] ) #expect(json.contains(" \"key\"")) #expect(!json.contains(" \"key\"")) } - @Test func writeYYJSONArrayPrettyPrintedTwoSpaces() throws { + @Test func writeYYJSONArrayIndentationTwoSpaces() throws { let value = try YYJSONValue(string: #"[{"key":"value"}]"#) guard let array = value.array else { Issue.record("Expected array") @@ -280,17 +280,17 @@ import Testing } let json = try Self.jsonString( for: array, - options: [.prettyPrintedTwoSpaces] + options: [.indentationTwoSpaces] ) #expect(json.contains("\n {")) #expect(!json.contains("\n {")) } - @Test func writeYYJSONValuePrettyPrintedTwoSpacesOverridesPrettyPrinted() throws { + @Test func writeYYJSONValueIndentationTwoSpacesOverridesPrettyPrinted() throws { let value = try YYJSONValue(string: #"{"key":"value"}"#) let json = try Self.jsonString( for: value, - options: [.prettyPrinted, .prettyPrintedTwoSpaces] + options: [.prettyPrinted, .indentationTwoSpaces] ) #expect(json.contains(" \"key\"")) #expect(!json.contains(" \"key\"")) @@ -369,11 +369,11 @@ import Testing #expect(json.hasSuffix("\n")) } - @Test func writeYYJSONValuePrettyPrintedTwoSpacesSortedKeys() throws { + @Test func writeYYJSONValueIndentationTwoSpacesSortedKeys() throws { let value = try YYJSONValue(string: #"{"b":2,"a":1}"#) let json = try Self.jsonString( for: value, - options: [.prettyPrintedTwoSpaces, .sortedKeys] + options: [.indentationTwoSpaces, .sortedKeys] ) let aIndex = json.range(of: "\"a\"")!.lowerBound let bIndex = json.range(of: "\"b\"")!.lowerBound @@ -381,7 +381,7 @@ import Testing #expect(json.contains(" \"a\"")) } - @Test func writeYYJSONObjectPrettyPrintedTwoSpacesSortedKeys() throws { + @Test func writeYYJSONObjectIndentationTwoSpacesSortedKeys() throws { let value = try YYJSONValue(string: #"{"b":2,"a":1}"#) guard let object = value.object else { Issue.record("Expected object") @@ -389,7 +389,7 @@ import Testing } let json = try Self.jsonString( for: object, - options: [.prettyPrintedTwoSpaces, .sortedKeys] + options: [.indentationTwoSpaces, .sortedKeys] ) let aIndex = json.range(of: "\"a\"")!.lowerBound let bIndex = json.range(of: "\"b\"")!.lowerBound @@ -397,7 +397,7 @@ import Testing #expect(json.contains(" \"a\"")) } - @Test func writeYYJSONArrayPrettyPrintedTwoSpacesSortedKeys() throws { + @Test func writeYYJSONArrayIndentationTwoSpacesSortedKeys() throws { let value = try YYJSONValue(string: #"[{"b":2,"a":1}]"#) guard let array = value.array else { Issue.record("Expected array") @@ -405,7 +405,7 @@ import Testing } let json = try Self.jsonString( for: array, - options: [.prettyPrintedTwoSpaces, .sortedKeys] + options: [.indentationTwoSpaces, .sortedKeys] ) let aIndex = json.range(of: "\"a\"")!.lowerBound let bIndex = json.range(of: "\"b\"")!.lowerBound @@ -522,13 +522,13 @@ import Testing } } - // MARK: - prettyPrintedTwoSpaces + // MARK: - indentationTwoSpaces - @Test func writePrettyPrintedTwoSpaces() throws { + @Test func writeIndentationTwoSpaces() throws { let dict: NSDictionary = ["key": "value"] let data = try YYJSONSerialization.data( withJSONObject: dict, - options: [.prettyPrintedTwoSpaces] + options: [.indentationTwoSpaces] ) let json = String(data: data, encoding: .utf8)! // Should use 2-space indentation (not 4-space) @@ -536,11 +536,11 @@ import Testing #expect(!json.contains(" \"key\"")) } - @Test func writePrettyPrintedTwoSpacesOverridesPrettyPrinted() throws { + @Test func writeIndentationTwoSpacesOverridesPrettyPrinted() throws { let dict: NSDictionary = ["a": 1] let data = try YYJSONSerialization.data( withJSONObject: dict, - options: [.prettyPrinted, .prettyPrintedTwoSpaces] + options: [.prettyPrinted, .indentationTwoSpaces] ) let json = String(data: data, encoding: .utf8)! // 2-space should take priority @@ -548,11 +548,11 @@ import Testing #expect(!json.contains(" \"a\"")) } - @Test func writePrettyPrintedTwoSpacesWithSortedKeys() throws { + @Test func writeIndentationTwoSpacesWithSortedKeys() throws { let dict: NSDictionary = ["b": 2, "a": 1] let data = try YYJSONSerialization.data( withJSONObject: dict, - options: [.prettyPrintedTwoSpaces, .sortedKeys] + options: [.indentationTwoSpaces, .sortedKeys] ) let json = String(data: data, encoding: .utf8)! // Check key order and indentation @@ -562,11 +562,11 @@ import Testing #expect(json.contains(" \"a\"")) // 2-space indent } - @Test func writePrettyPrintedTwoSpacesNestedStructure() throws { + @Test func writeIndentationTwoSpacesNestedStructure() throws { let dict: NSDictionary = ["outer": ["inner": ["deep": 1]]] let data = try YYJSONSerialization.data( withJSONObject: dict, - options: [.prettyPrintedTwoSpaces] + options: [.indentationTwoSpaces] ) let json = String(data: data, encoding: .utf8)! // Verify 2-space indentation at each nesting level