AnyCodable.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. A type-erased `Codable` value.
  3. The `AnyCodable` type forwards encoding and decoding responsibilities
  4. to an underlying value, hiding its specific underlying type.
  5. You can encode or decode mixed-type values in dictionaries
  6. and other collections that require `Encodable` or `Decodable` conformance
  7. by declaring their contained type to be `AnyCodable`.
  8. - SeeAlso: `AnyEncodable`
  9. - SeeAlso: `AnyDecodable`
  10. */
  11. import Foundation
  12. #if swift(>=5.1)
  13. @frozen public struct AnyCodable: Codable {
  14. public let value: Any
  15. public init<T>(_ value: T?) {
  16. self.value = value ?? ()
  17. }
  18. }
  19. #else
  20. public struct AnyCodable: Codable {
  21. public let value: Any
  22. public init<T>(_ value: T?) {
  23. self.value = value ?? ()
  24. }
  25. }
  26. #endif
  27. extension AnyCodable: _AnyEncodable, _AnyDecodable {}
  28. extension AnyCodable: Equatable {
  29. public static func == (lhs: AnyCodable, rhs: AnyCodable) -> Bool {
  30. switch (lhs.value, rhs.value) {
  31. case is (Void, Void):
  32. return true
  33. case let (lhs as Bool, rhs as Bool):
  34. return lhs == rhs
  35. case let (lhs as Int, rhs as Int):
  36. return lhs == rhs
  37. case let (lhs as Int8, rhs as Int8):
  38. return lhs == rhs
  39. case let (lhs as Int16, rhs as Int16):
  40. return lhs == rhs
  41. case let (lhs as Int32, rhs as Int32):
  42. return lhs == rhs
  43. case let (lhs as Int64, rhs as Int64):
  44. return lhs == rhs
  45. case let (lhs as UInt, rhs as UInt):
  46. return lhs == rhs
  47. case let (lhs as UInt8, rhs as UInt8):
  48. return lhs == rhs
  49. case let (lhs as UInt16, rhs as UInt16):
  50. return lhs == rhs
  51. case let (lhs as UInt32, rhs as UInt32):
  52. return lhs == rhs
  53. case let (lhs as UInt64, rhs as UInt64):
  54. return lhs == rhs
  55. case let (lhs as Decimal, rhs as Decimal):
  56. return lhs == rhs
  57. case let (lhs as Float, rhs as Float):
  58. return lhs == rhs
  59. case let (lhs as Double, rhs as Double):
  60. return lhs == rhs
  61. case let (lhs as String, rhs as String):
  62. return lhs == rhs
  63. case let (lhs as [String: AnyCodable], rhs as [String: AnyCodable]):
  64. return lhs == rhs
  65. case let (lhs as [AnyCodable], rhs as [AnyCodable]):
  66. return lhs == rhs
  67. default:
  68. return false
  69. }
  70. }
  71. }
  72. extension AnyCodable: CustomStringConvertible {
  73. public var description: String {
  74. switch value {
  75. case is Void:
  76. return String(describing: nil as Any?)
  77. case let value as CustomStringConvertible:
  78. return value.description
  79. default:
  80. return String(describing: value)
  81. }
  82. }
  83. }
  84. extension AnyCodable: CustomDebugStringConvertible {
  85. public var debugDescription: String {
  86. switch value {
  87. case let value as CustomDebugStringConvertible:
  88. return "AnyCodable(\(value.debugDescription))"
  89. default:
  90. return "AnyCodable(\(description))"
  91. }
  92. }
  93. }
  94. extension AnyCodable: ExpressibleByNilLiteral {}
  95. extension AnyCodable: ExpressibleByBooleanLiteral {}
  96. extension AnyCodable: ExpressibleByIntegerLiteral {}
  97. extension AnyCodable: ExpressibleByFloatLiteral {}
  98. extension AnyCodable: ExpressibleByStringLiteral {}
  99. extension AnyCodable: ExpressibleByArrayLiteral {}
  100. extension AnyCodable: ExpressibleByDictionaryLiteral {}