Skip to content

Commit 65a3841

Browse files
authored
[OrderedDictionary] Document encoding format (apple#26)
Unlike the regular unordered `Dictionary`, `OrderedDictionary` always encodes/decodes itself as alternating key/value pairs in an unkeyed container. Document this fact (and the reason behind it) in the `Decodable` implementation.
1 parent 3fb3c75 commit 65a3841

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Codable.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ extension OrderedDictionary: Encodable where Key: Encodable, Value: Encodable {
1919
/// encoder's format.
2020
///
2121
/// - Note: Unlike the standard `Dictionary` type, ordered dictionaries
22-
/// always encode in an unkeyed container, because `Codable`'s keyed
23-
/// containers do not guarantee that they preserve the ordering of the
24-
/// items they contain. (And in popular encoding formats, keyed containers
25-
/// tend to map to unordered data structures -- e.g., JSON's "object"
26-
/// construct is explicitly unordered.)
22+
/// always encode themselves into an unkeyed container, because
23+
/// `Codable`'s keyed containers do not guarantee that they preserve the
24+
/// ordering of the items they contain. (And in popular encoding formats,
25+
/// keyed containers tend to map to unordered data structures -- e.g.,
26+
/// JSON's "object" construct is explicitly unordered.)
2727
///
2828
/// - Parameter encoder: The encoder to write data to.
2929
@inlinable
@@ -40,8 +40,18 @@ extension OrderedDictionary: Encodable where Key: Encodable, Value: Encodable {
4040
extension OrderedDictionary: Decodable where Key: Decodable, Value: Decodable {
4141
/// Creates a new dictionary by decoding from the given decoder.
4242
///
43+
/// `OrderedDictionary` expects its contents to be encoded as alternating
44+
/// key-value pairs in an unkeyed container.
45+
///
4346
/// This initializer throws an error if reading from the decoder fails, or
44-
/// if the decoded contents are corrupt (such as they contain duplicate values).
47+
/// if the decoded contents are not in the expected format.
48+
///
49+
/// - Note: Unlike the standard `Dictionary` type, ordered dictionaries
50+
/// always encode themselves into an unkeyed container, because
51+
/// `Codable`'s keyed containers do not guarantee that they preserve the
52+
/// ordering of the items they contain. (And in popular encoding formats,
53+
/// keyed containers tend to map to unordered data structures -- e.g.,
54+
/// JSON's "object" construct is explicitly unordered.)
4555
///
4656
/// - Parameter decoder: The decoder to read data from.
4757
@inlinable

0 commit comments

Comments
 (0)