Skip to content

Commit 0048c4a

Browse files
committed
SR-12696: Add JSONEncoder.OutputFormatting.withoutEscapingSlashes for Linux
- This was missing and is required to match Darwin.
1 parent 0f20a52 commit 0048c4a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Sources/Foundation/JSONEncoder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ open class JSONEncoder {
5757
/// Produce JSON with dictionary keys sorted in lexicographic order.
5858
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
5959
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)
60+
61+
/// By default slashes get escaped ("/" → "\/", "http://apple.com/" → "http:\/\/apple.com\/")
62+
/// for security reasons, allowing outputted JSON to be safely embedded within HTML/XML.
63+
/// In contexts where this escaping is unnecessary, the JSON is known to not be embedded,
64+
/// or is intended only for display, this option avoids this escaping.
65+
public static let withoutEscapingSlashes = OutputFormatting(rawValue: 1 << 3)
6066
}
6167

6268
/// The strategy to use for encoding `Date` values.

Tests/Foundation/Tests/TestJSONEncoder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,12 @@ class TestJSONEncoder : XCTestCase {
853853
XCTAssertEqual(jsonObject, camelCaseDictionary)
854854
}
855855

856+
func test_OutputFormattingValues() {
857+
XCTAssertEqual(JSONEncoder.OutputFormatting.prettyPrinted.rawValue, 1)
858+
XCTAssertEqual(JSONEncoder.OutputFormatting.sortedKeys.rawValue, 2)
859+
XCTAssertEqual(JSONEncoder.OutputFormatting.withoutEscapingSlashes.rawValue, 8)
860+
}
861+
856862
// MARK: - Helper Functions
857863
private var _jsonEmptyDictionary: Data {
858864
return "{}".data(using: .utf8)!
@@ -1454,6 +1460,7 @@ extension TestJSONEncoder {
14541460
("test_snake_case_encoding", test_snake_case_encoding),
14551461
("test_dictionary_snake_case_decoding", test_dictionary_snake_case_decoding),
14561462
("test_dictionary_snake_case_encoding", test_dictionary_snake_case_encoding),
1463+
("test_OutputFormattingValues", test_OutputFormattingValues),
14571464
]
14581465
}
14591466
}

0 commit comments

Comments
 (0)