Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/YYJSON/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Comment thread
mattt marked this conversation as resolved.
/// Escape unicode as `\uXXXX`, making the output ASCII only.
public static let escapeUnicode = YYJSONWriteOptions(rawValue: YYJSON_WRITE_ESCAPE_UNICODE)
Expand Down
10 changes: 5 additions & 5 deletions Sources/YYJSON/Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Comment thread
mattt marked this conversation as resolved.
/// Escape non-ASCII characters in string values as `\uXXXX`, making the output ASCII only.
/// Scalars outside the BMP are emitted as surrogate pairs.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/YYJSONTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Tests/YYJSONTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
Expand Down Expand Up @@ -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)!

Expand Down
46 changes: 23 additions & 23 deletions Tests/YYJSONTests/SerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,49 +248,49 @@ 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")
return
}
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")
return
}
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\""))
Expand Down Expand Up @@ -369,43 +369,43 @@ 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
#expect(aIndex < bIndex)
#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")
return
}
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
#expect(aIndex < bIndex)
#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")
return
}
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
Expand Down Expand Up @@ -522,37 +522,37 @@ 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)
#expect(json.contains(" \"key\""))
#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
#expect(json.contains(" \"a\""))
#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
Expand All @@ -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
Expand Down