Skip to content

Commit

Permalink
Add Codable conformance to LowEnergyAdvertisingData
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Mar 15, 2024
1 parent b516369 commit 974ee43
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/Bluetooth/LowEnergyAdvertisingData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,22 @@ extension LowEnergyAdvertisingData: RandomAccessCollection {
return Slice<LowEnergyAdvertisingData>(base: self, bounds: bounds)
}
}

// MARK: - Codable

extension LowEnergyAdvertisingData: Codable {

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let data = try container.decode(Data.self)
guard let value = Self.init(data: data) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Invalid number of bytes (\(data.count)."))
}
self = value
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(data)
}
}

0 comments on commit 974ee43

Please sign in to comment.