Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/JSONAST/JSON.Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ extension JSON.Node {
@inlinable public func `as`(_: Float.Type) -> Float? {
self.as(JSON.Number.self)?.as(Float.self)
}
/// Attempts to load an instance of ``Float16`` from this variant.
///
/// - Returns:
/// The closest value of ``Float16`` to the payload of this variant if it matches
/// ``number(_:) [case]``, `nil` otherwise.
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
@inlinable public func `as`(_: Float16.Type) -> Float16? {
self.as(JSON.Number.self)?.as(Float16.self)
}
/// Attempts to load an instance of ``Number`` from this variant.
///
/// - Returns:
Expand Down
6 changes: 6 additions & 0 deletions Sources/JSONAST/JSON.Number.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ extension JSON.Number {
public func `as`(_: Float.Type) -> Float {
self.nearest(Float.self)
}
/// Converts this numeric literal to a ``Float16`` value, or its closest
/// floating-point representation.
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
public func `as`(_: Float16.Type) -> Float16 {
self.nearest(Float16.self)
}

/// Converts this numeric literal to a floating-point value, or its closest
/// floating-point representation.
Expand Down
6 changes: 6 additions & 0 deletions Sources/JSONDecoding/Conformances/Float16 (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
extension Float16: JSONDecodable {
@inlinable public init(json: JSON.Node) throws {
self = try json.cast { $0.as(Self.self) }
}
}
23 changes: 23 additions & 0 deletions Sources/JSONDecoding/JSONDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ extension JSONDecodable where Self: UnsignedInteger & FixedWidthInteger {
self = try json.cast { try $0.as(Self.self) }
}
}
#if (os(Linux) || os(macOS)) && arch(x86_64)
extension JSONDecodable where Self == Float80 {
@inlinable public init(json: JSON.Node) throws {
self = try json.cast { $0.as(Self.self) }
}
}
#endif
extension JSONDecodable where Self == Double {
@inlinable public init(json: JSON.Node) throws {
self = try json.cast { $0.as(Self.self) }
}
}
extension JSONDecodable where Self == Float {
@inlinable public init(json: JSON.Node) throws {
self = try json.cast { $0.as(Self.self) }
}
}
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
extension JSONDecodable where Self == Float16 {
@inlinable public init(json: JSON.Node) throws {
self = try json.cast { $0.as(Self.self) }
}
}
extension JSONDecodable where Self: RawRepresentable, RawValue: JSONDecodable & Sendable {
@inlinable public init(json: JSON.Node) throws {
let rawValue: RawValue = try .init(json: json)
Expand Down
2 changes: 2 additions & 0 deletions Sources/JSONEncoding/Conformances/Double (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extension Double: JSONEncodable {
}
2 changes: 2 additions & 0 deletions Sources/JSONEncoding/Conformances/Float (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extension Float: JSONEncodable {
}
3 changes: 3 additions & 0 deletions Sources/JSONEncoding/Conformances/Float16 (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
extension Float16: JSONEncodable {
}
4 changes: 4 additions & 0 deletions Sources/JSONEncoding/Conformances/Float80 (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#if (os(Linux) || os(macOS)) && arch(x86_64)
extension Float80: JSONEncodable {
}
#endif
4 changes: 2 additions & 2 deletions Sources/JSONEncoding/Encoders/JSON.Literal (ext).swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extension JSON.Literal<Bool> {
json.utf8 += (self.value ? "true" : "false").utf8
}
}
extension JSON.Literal where Value: BinaryInteger {
/// Encodes this literal’s integer ``value`` to the provided JSON stream. The value’s
extension JSON.Literal where Value: Numeric & CustomStringConvertible {
/// Encodes this literal’s numeric ``value`` to the provided JSON stream. The value’s
/// ``CustomStringConvertible description`` witness must format the value in base-10.
@inlinable internal static func += (json: inout JSON, self: Self) {
json.utf8 += self.value.description.utf8
Expand Down
2 changes: 1 addition & 1 deletion Sources/JSONEncoding/JSONEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension JSONEncodable where Self: StringProtocol {
json += JSON.Literal<Self>.init(self)
}
}
extension JSONEncodable where Self: BinaryInteger {
extension JSONEncodable where Self: Numeric & CustomStringConvertible {
@inlinable public func encode(to json: inout JSON) {
json += JSON.Literal<Self>.init(self)
}
Expand Down