Skip to content
This repository was archived by the owner on Nov 18, 2018. It is now read-only.

Commit b96460e

Browse files
committed
fixed binary subtype printing
1 parent 54c0da4 commit b96460e

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

Sources/BinaryJSON/BSON.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public struct BSON {
8282

8383
public struct Binary: Equatable {
8484

85-
public enum Subtype {
86-
87-
case Generic
88-
case Function
89-
case Old
90-
case UUID
91-
case UUIDOld
92-
case MD5
93-
case User
85+
public enum Subtype: Byte {
86+
87+
case Generic = 0x00
88+
case Function = 0x01
89+
case Old = 0x02
90+
case UUIDOld = 0x03
91+
case UUID = 0x04
92+
case MD5 = 0x05
93+
case User = 0x80
9494
}
9595

9696
public var data: Data

Sources/BinaryJSON/JSONEncodable.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,22 @@ extension BSON.Timestamp: JSONEncodable {
125125

126126
extension BSON.Binary: JSONEncodable {
127127

128-
private static var JSONKey: String { return "$binary" }
128+
private enum JSONKey: String {
129+
130+
case binary = "$binary"
131+
case type = "$type"
132+
}
129133

130134
public func toJSON() -> JSON.Value {
131135

132136
let base64EncodedData = Base64.encode(self.data)
133137

134138
guard let base64String = String(UTF8Data: base64EncodedData)
135139
else { fatalError("Could not create string from Base64 data") }
140+
141+
let subtypeHexString = String(format:"%02hhX", subtype.rawValue)
136142

137-
return .Object([BSON.Binary.JSONKey: .String(base64String)])
143+
return .Object([JSONKey.binary.rawValue: .String(base64String), JSONKey.type.rawValue: .String(subtypeHexString)])
138144
}
139145
}
140146

Sources/UnitTests/BSONTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class BSONTests: XCTestCase {
6565

6666
print("JSON: \n\(jsonString)\n")
6767

68-
let convertedJSON = document.toJSON()
69-
70-
print("Converted JSON: \n\(convertedJSON)\n")
71-
7268
guard let parsedJSON = JSON.Value(string: jsonString)
7369
else { XCTFail("Could not parse JSON string"); return }
7470

7571
print("Parsed JSON: \n\(parsedJSON)\n")
7672

73+
let convertedJSON = document.toJSON()
74+
75+
print("Converted JSON: \n\(convertedJSON)\n")
76+
7777
XCTAssert(parsedJSON == convertedJSON, "Converted JSON should equal parsed JSON")
7878
}
7979
}

0 commit comments

Comments
 (0)