···61 // I hate this conditional cast, but Swift forces us to do this because Codable can't implement a specialized
62 // function for any type, only the standard library types. This is the same method Foundation uses to detect
63 // special encoding cases. It's still lame.
64-65- if let date = value as? Date {
66 switch options.dateEncodingStrategy {
67 case .string:
68- parent.register(StringDateOptimizer(value: date))
69 case .float:
70- parent.register(EpochFloatDateOptimizer(value: date))
71 case .double:
72- parent.register(EpochDoubleDateOptimizer(value: date))
73 }
74- } else if let uuid = value as? UUID {
75- parent.register(UUIDOptimizer(value: uuid))
76- } else if let data = value as? Data {
77- parent.register(ByteStringOptimizer(value: data))
78- } else {
000079 try value.encode(to: self)
80 }
81 }
···61 // I hate this conditional cast, but Swift forces us to do this because Codable can't implement a specialized
62 // function for any type, only the standard library types. This is the same method Foundation uses to detect
63 // special encoding cases. It's still lame.
64+ switch value {
65+ case let value as Date:
66 switch options.dateEncodingStrategy {
67 case .string:
68+ parent.register(StringDateOptimizer(value: value))
69 case .float:
70+ parent.register(EpochFloatDateOptimizer(value: value))
71 case .double:
72+ parent.register(EpochDoubleDateOptimizer(value: value))
73 }
74+ case let value as UUID:
75+ parent.register(UUIDOptimizer(value: value))
76+ case let value as Data:
77+ parent.register(ByteStringOptimizer(value: value))
78+// #if canImport(Float16)
79+// case let value as Float16:
80+// parent.register(Float16Optimizer(value: value))
81+// #endif
82+ default:
83 try value.encode(to: self)
84 }
85 }
···1+//
2+// FloatOptimizer.swift
3+// SwiftCBOR
4+//
5+// Created by Khan Winter on 9/01/25.
6+//
7+8+// Would love to enable this, also see SingleValueCBOREncodingContainer for the other half of this
9+// but I'm not entirely sure how to make it work in that container and still compile on every machine
10+// and platform this library supports...
11+12+// @available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
13+// struct Float16Optimizer: EncodingOptimizer {
14+// let value: Float16
15+//
16+// var type: MajorType { .simple }
17+// var argument: UInt8 { 25 }
18+// var contentSize: Int { 2 }
19+//
20+// init(value: Float16) {
21+// self.value = value
22+// }
23+//
24+// func writePayload(to data: inout Slice<UnsafeMutableRawBufferPointer>) {
25+// assert(data.count >= 2)
26+// var bytes = value.bitPattern.bigEndian
27+// withUnsafeBytes(of: &bytes) { ptr in
28+// UnsafeMutableRawBufferPointer(rebasing: data).copyBytes(from: ptr)
29+// }
30+// data.removeFirst(2)
31+// }
32+// }
+18
Tests/CBORTests/EncodableTests.swift
···228 let data = try encoder.encode(1999)
229 #expect(data == "1907CF".asHexData())
230 }
000000000000000000231}
···228 let data = try encoder.encode(1999)
229 #expect(data == "1907CF".asHexData())
230 }
231+232+ @Test(
233+ .disabled("Disabled until we can figure out how to make Float16 compile on all platforms."),
234+ arguments: [
235+ ("F90000", 0),
236+ ("F93C00", 1.0),
237+ ("F9BE00", -1.5),
238+ ("F97C00", .infinity),
239+ ("F93E32", 1.548828125),
240+ ("F9F021", -8456)
241+ ]
242+ )
243+ func halfFloat(expected: String, value: Float16) throws {
244+ let expectedData = expected.asHexData()
245+ let encoder = CBOREncoder()
246+ let data = try encoder.encode(value)
247+ #expect(data == expectedData)
248+ }
249}