@@ -4,20 +4,31 @@ import NIO
44
55/// Facilitates the encoding of `Encodable` values into ExtendedJSON.
66public class ExtendedJSONEncoder {
7- /// An enum representing one of the two supported string formats based on the JSON standard
8- /// that describe how to represent BSON documents in JSON using standard JSON types and/or type wrapper objects.
9- public enum Mode {
7+ /// A struct representing the supported string formats based on the JSON standard that describe how to represent
8+ /// BSON documents in JSON using standard JSON types and/or type wrapper objects.
9+ public struct Format {
1010 /// Canonical Extended JSON Format: Emphasizes type preservation
1111 /// at the expense of readability and interoperability.
12- case canonical
12+ public static let canonical = Format ( . canonical )
1313
1414 /// Relaxed Extended JSON Format: Emphasizes readability and interoperability
1515 /// at the expense of type preservation.
16- case relaxed
16+ public static let relaxed = Format ( . relaxed)
17+
18+ /// Internal representation of extJSON format.
19+ fileprivate enum _Format {
20+ case canonical, relaxed
21+ }
22+
23+ fileprivate var _format : _Format
24+
25+ private init ( _ _format: _Format ) {
26+ self . _format = _format
27+ }
1728 }
1829
1930 /// Determines whether to encode to canonical or relaxed extended JSON. Default is relaxed.
20- public var mode : Mode = . relaxed
31+ public var format : Format = . relaxed
2132
2233 /// Contextual user-provided information for use during encoding.
2334 public var userInfo : [ CodingUserInfoKey : Any ] = [ : ]
@@ -29,14 +40,14 @@ public class ExtendedJSONEncoder {
2940 // T --> BSON --> JSONValue --> Data
3041 // Takes in any encodable type `T`, converts it to an instance of the `BSON` enum via the `BSONDecoder`.
3142 // The `BSON` is converted to an instance of the `JSON` enum via the `toRelaxedExtendedJSON`
32- // or `toCanonicalExtendedJSON` methods on `BSONValue`s (depending on the `mode `).
43+ // or `toCanonicalExtendedJSON` methods on `BSONValue`s (depending on the `format `).
3344 // The `JSON` is then passed through a `JSONEncoder` and outputted as `Data`.
3445 let encoder = BSONEncoder ( )
3546 encoder. userInfo = self . userInfo
3647 let bson : BSON = try encoder. encodeFragment ( value)
3748
3849 let json : JSON
39- switch self . mode {
50+ switch self . format . _format {
4051 case . canonical:
4152 json = bson. bsonValue. toCanonicalExtendedJSON ( )
4253 case . relaxed:
@@ -49,7 +60,8 @@ public class ExtendedJSONEncoder {
4960 }
5061
5162 /// Encodes an instance of the Encodable Type `T` into Data representing canonical or relaxed extended JSON.
52- /// The value of `self.mode` will determine which format is used. If it is not set explicitly, relaxed will be used.
63+ /// The value of `self.format` will determine which format is used. If it is not set explicitly, relaxed will
64+ /// be used.
5365 ///
5466 /// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
5567 ///
@@ -62,7 +74,7 @@ public class ExtendedJSONEncoder {
6274 }
6375
6476 /// Encodes an instance of the Encodable Type `T` into a `ByteBuffer` representing canonical or relaxed extended
65- /// JSON. The value of `self.mode ` will determine which format is used. If it is not set explicitly, relaxed will
77+ /// JSON. The value of `self.format ` will determine which format is used. If it is not set explicitly, relaxed will
6678 /// be used.
6779 ///
6880 /// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
0 commit comments