Skip to content

Commit 8a9d60c

Browse files
authored
SWIFT-1064 Rename ExtendedJSONEncoder.mode and make it a struct (#61)
1 parent e2b2dc0 commit 8a9d60c

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ disabled_rules:
88
- inclusive_language # disabled until we complete work for the "remove offensive terminology" project.
99
- cyclomatic_complexity
1010
- opening_brace # conflicts with SwiftFormat wrapMultilineStatementBraces
11+
- nesting
1112

1213
opt_in_rules:
1314
- array_init

Guides/JSON-Interop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The `ExtendedJSONEncoder` produces relaxed Extended JSON by default, but can be
5757
```swift
5858
let bob = Person(name: "Bob", age: 25)
5959
let encoder = ExtendedJSONEncoder()
60-
encoder.mode = .canonical
60+
encoder.format = .canonical
6161
let canonicalEncoded = try encoder.encode(bob) // "{\"name\":\"Bob\",\"age\":{\"$numberInt\":\"25\"}}"
6262
```
6363
The `ExtendedJSONDecoder` accepts either format, or a mix of both:

Sources/SwiftBSON/BSONDocument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public struct BSONDocument {
135135
/// On error, an empty string will be returned.
136136
public func toCanonicalExtendedJSONString() -> String {
137137
let encoder = ExtendedJSONEncoder()
138-
encoder.mode = .canonical
138+
encoder.format = .canonical
139139
guard let encoded = try? encoder.encode(self) else {
140140
return ""
141141
}

Sources/SwiftBSON/ExtendedJSONEncoder.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@ import NIO
44

55
/// Facilitates the encoding of `Encodable` values into ExtendedJSON.
66
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 {
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/

Tests/.swiftlint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ disabled_rules:
22
- force_cast
33
- force_try
44
- force_unwrapping
5-
- nesting
65
- explicit_acl
76
- missing_docs
87
- cyclomatic_complexity

Tests/SwiftBSONTests/BSONCorpusTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ final class BSONCorpusTests: BSONTestCase {
166166

167167
// native_to_canonical_extended_json( bson_to_native(cB) ) = cEJ
168168
let canonicalEncoder = ExtendedJSONEncoder()
169-
canonicalEncoder.mode = .canonical
169+
canonicalEncoder.format = .canonical
170170
expect(try canonicalEncoder.encode(docFromCB))
171171
.to(cleanEqual(test.canonicalExtJSON), description: test.description)
172172

173173
// native_to_relaxed_extended_json( bson_to_native(cB) ) = rEJ (if rEJ exists)
174-
let relaxedEncoder = ExtendedJSONEncoder() // default mode is .relaxed
174+
let relaxedEncoder = ExtendedJSONEncoder() // default format is .relaxed
175175
if let rEJ = test.relaxedExtJSON {
176176
expect(try relaxedEncoder.encode(docFromCB))
177177
.to(cleanEqual(rEJ), description: test.description)

Tests/SwiftBSONTests/ExtendedJSONConversionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ open class ExtendedJSONConversionTestCase: BSONTestCase {
2626

2727
// Test canonical encoder
2828
let encoder = ExtendedJSONEncoder()
29-
encoder.mode = .canonical
29+
encoder.format = .canonical
3030
let encoded: Data = try encoder.encode(test)
3131
expect(encoded).to(cleanEqual(canonicalExtJSON))
3232
let encodedBuffer = try encoder.encodeBuffer(test)
3333
expect(Data(encodedBuffer.readableBytesView)).to(cleanEqual(canonicalExtJSON))
3434

3535
// Test relaxed encoder
36-
encoder.mode = .relaxed
36+
encoder.format = .relaxed
3737
let relaxedEncoded: Data = try encoder.encode(test)
3838
let relaxedExtJSON = "{\"x\":true,\"y\":5,\"z\":\(regexStr)}"
3939
expect(relaxedEncoded).to(cleanEqual(relaxedExtJSON))

0 commit comments

Comments
 (0)