Skip to content

Commit 487c03f

Browse files
authored
SWIFT-922 Sketch of public API for ExtendedJSONEncoder (#29)
1 parent f936b06 commit 487c03f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Foundation
2+
3+
/// Facilitates the encoding of `Encodable` values into ExtendedJSON.
4+
public class ExtendedJSONEncoder {
5+
/// An enum representing one of the two supported string formats based on the JSON standard
6+
/// that describe how to represent BSON documents in JSON using standard JSON types and/or type wrapper objects.
7+
public enum Mode {
8+
/// Canonical Extended JSON Format: Emphasizes type preservation
9+
/// at the expense of readability and interoperability.
10+
case canonical
11+
12+
/// Relaxed Extended JSON Format: Emphasizes readability and interoperability
13+
/// at the expense of type preservation.
14+
case relaxed
15+
}
16+
17+
/// Determines whether to encode to canonical or relaxed extended JSON. Default is relaxed.
18+
public var mode: Mode = .relaxed
19+
20+
/// Contextual user-provided information for use during encoding.
21+
public var userInfo: [CodingUserInfoKey: Any] = [:]
22+
23+
/// Initialize an `ExtendedJSONEncoder`.
24+
public init() {
25+
fatalError("unimplemented")
26+
}
27+
28+
/// Encodes an instance of the Encodable Type `T` into Data representing canonical or relaxed extended JSON.
29+
/// The value of `self.mode` will determine which format is used. If it is not set explicitly, relaxed will be used.
30+
///
31+
/// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
32+
///
33+
/// - Parameters:
34+
/// - value: instance of Encodable type `T` which will be encoded.
35+
/// - Returns: Encoded representation of the `T` input as an instance of `Data` representing ExtendedJSON.
36+
/// - Throws: `EncodingError` if the value is corrupt or cannot be converted to valid ExtendedJSON.
37+
public func encode<T: Encodable>(_ value: T) throws -> Data {
38+
// T --> BSON --> JSON --> Data
39+
// Takes in any encodable type `T`, converts it to an instance of the `BSON` enum via the `BSONDecoder`.
40+
// The `BSON` is converted to an instance of the `JSON` enum via the `toRelaxedExtendedJSON`
41+
// or `toCanonicalExtendedJSON` methods on `BSONValue`s (depending on the `mode`).
42+
// The `JSON` is then passed through a `JSONEncoder` and outputted as `Data`.
43+
fatalError("unimplemented")
44+
}
45+
}

0 commit comments

Comments
 (0)