@@ -4,20 +4,31 @@ import NIO
4
4
5
5
/// Facilitates the encoding of `Encodable` values into ExtendedJSON.
6
6
public 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 {
10
10
/// Canonical Extended JSON Format: Emphasizes type preservation
11
11
/// at the expense of readability and interoperability.
12
- case canonical
12
+ public static let canonical = Format ( . canonical )
13
13
14
14
/// Relaxed Extended JSON Format: Emphasizes readability and interoperability
15
15
/// 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
+ }
17
28
}
18
29
19
30
/// 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
21
32
22
33
/// Contextual user-provided information for use during encoding.
23
34
public var userInfo : [ CodingUserInfoKey : Any ] = [ : ]
@@ -29,14 +40,14 @@ public class ExtendedJSONEncoder {
29
40
// T --> BSON --> JSONValue --> Data
30
41
// Takes in any encodable type `T`, converts it to an instance of the `BSON` enum via the `BSONDecoder`.
31
42
// 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 `).
33
44
// The `JSON` is then passed through a `JSONEncoder` and outputted as `Data`.
34
45
let encoder = BSONEncoder ( )
35
46
encoder. userInfo = self . userInfo
36
47
let bson : BSON = try encoder. encodeFragment ( value)
37
48
38
49
let json : JSON
39
- switch self . mode {
50
+ switch self . format . _format {
40
51
case . canonical:
41
52
json = bson. bsonValue. toCanonicalExtendedJSON ( )
42
53
case . relaxed:
@@ -49,7 +60,8 @@ public class ExtendedJSONEncoder {
49
60
}
50
61
51
62
/// 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.
53
65
///
54
66
/// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
55
67
///
@@ -62,7 +74,7 @@ public class ExtendedJSONEncoder {
62
74
}
63
75
64
76
/// 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
66
78
/// be used.
67
79
///
68
80
/// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
0 commit comments