Skip to content

SR-12696: Add JSONEncoder.OutputFormatting.withoutEscapingSlashes for Linux #2781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2020
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
6 changes: 6 additions & 0 deletions Sources/Foundation/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ open class JSONEncoder {
/// Produce JSON with dictionary keys sorted in lexicographic order.
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)

/// By default slashes get escaped ("/" → "\/", "http://apple.com/" → "http:\/\/apple.com\/")
/// for security reasons, allowing outputted JSON to be safely embedded within HTML/XML.
/// In contexts where this escaping is unnecessary, the JSON is known to not be embedded,
/// or is intended only for display, this option avoids this escaping.
public static let withoutEscapingSlashes = OutputFormatting(rawValue: 1 << 3)
}

/// The strategy to use for encoding `Date` values.
Expand Down
7 changes: 7 additions & 0 deletions Tests/Foundation/Tests/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,12 @@ class TestJSONEncoder : XCTestCase {
XCTAssertEqual(jsonObject, camelCaseDictionary)
}

func test_OutputFormattingValues() {
XCTAssertEqual(JSONEncoder.OutputFormatting.prettyPrinted.rawValue, 1)
XCTAssertEqual(JSONEncoder.OutputFormatting.sortedKeys.rawValue, 2)
XCTAssertEqual(JSONEncoder.OutputFormatting.withoutEscapingSlashes.rawValue, 8)
}

// MARK: - Helper Functions
private var _jsonEmptyDictionary: Data {
return "{}".data(using: .utf8)!
Expand Down Expand Up @@ -1454,6 +1460,7 @@ extension TestJSONEncoder {
("test_snake_case_encoding", test_snake_case_encoding),
("test_dictionary_snake_case_decoding", test_dictionary_snake_case_decoding),
("test_dictionary_snake_case_encoding", test_dictionary_snake_case_encoding),
("test_OutputFormattingValues", test_OutputFormattingValues),
]
}
}