[swift5] Refactor encodeToJSON (#10961)

* Add protocol extension method

* execute sample script

* Add Packing.cmake

* Revert "Add Packing.cmake"

This reverts commit cb52547fe3.

* Remove empty lines

* Revert FILES

* Add Packing.cmake
This commit is contained in:
Fumito Nakazawa
2021-11-26 01:55:06 +09:00
committed by GitHub
parent 8e303150ae
commit a620853216
31 changed files with 219 additions and 585 deletions

View File

@@ -9,33 +9,15 @@ import Foundation
import AnyCodable
#endif
extension Bool: JSONEncodable {
func encodeToJSON() -> Any { return self }
}
extension Float: JSONEncodable {
func encodeToJSON() -> Any { return self }
}
extension Int: JSONEncodable {
func encodeToJSON() -> Any { return self }
}
extension Int32: JSONEncodable {
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
}
extension Int64: JSONEncodable {
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
}
extension Double: JSONEncodable {
func encodeToJSON() -> Any { return self }
}
extension String: JSONEncodable {
func encodeToJSON() -> Any { return self }
}
extension Bool: JSONEncodable {}
extension Float: JSONEncodable {}
extension Int: JSONEncodable {}
extension Int32: JSONEncodable {}
extension Int64: JSONEncodable {}
extension Double: JSONEncodable {}
extension String: JSONEncodable {}
extension URL: JSONEncodable {}
extension UUID: JSONEncodable {}
extension RawRepresentable where RawValue: JSONEncodable {
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
}
}
extension URL: JSONEncodable {
func encodeToJSON() -> Any {
return self
}
}
extension UUID: JSONEncodable {
func encodeToJSON() -> Any {
return self.uuidString
}
}
extension String: CodingKey {
public var stringValue: String {

View File

@@ -10,6 +10,10 @@ protocol JSONEncodable {
func encodeToJSON() -> Any
}
extension JSONEncodable {
func encodeToJSON() -> Any { self }
}
public enum ErrorResponse: Error {
case error(Int, Data?, URLResponse?, Error)
}