@@ -15,64 +15,8 @@ import class Foundation.DateFormatter
15
15
import class Foundation. JSONDecoder
16
16
import class Foundation. JSONEncoder
17
17
18
- #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
19
- extension DateFormatter {
20
- public static let iso8601 : DateFormatter = {
21
- let dateFormatter = DateFormatter ( )
22
- dateFormatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ssZ "
23
- return dateFormatter
24
- } ( )
25
- }
26
-
27
- extension JSONEncoder . DateEncodingStrategy {
28
- public static let customISO8601 = custom {
29
- var container = $1. singleValueContainer ( )
30
- try container. encode ( DateFormatter . iso8601. string ( from: $0) )
31
- }
32
- }
33
-
34
- extension JSONDecoder . DateDecodingStrategy {
35
- public static let customISO8601 = custom {
36
- let container = try $0. singleValueContainer ( )
37
- let string = try container. decode ( String . self)
38
- if let date = DateFormatter . iso8601. date ( from: string) {
39
- return date
40
- }
41
- throw DecodingError . dataCorruptedError ( in: container, debugDescription: " Invalid date: \( string) " )
42
- }
43
- }
44
- #endif
45
-
46
- extension JSONEncoder . DateEncodingStrategy {
47
- public static var safeISO8601 : JSONEncoder . DateEncodingStrategy {
48
- #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
49
- if #available( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * ) {
50
- return . iso8601
51
- } else {
52
- return . customISO8601
53
- }
54
- #else
55
- return . iso8601
56
- #endif
57
- }
58
- }
59
-
60
- extension JSONDecoder . DateDecodingStrategy {
61
- public static var safeISO8601 : JSONDecoder . DateDecodingStrategy {
62
- #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
63
- if #available( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * ) {
64
- return . iso8601
65
- } else {
66
- return . customISO8601
67
- }
68
- #else
69
- return . iso8601
70
- #endif
71
- }
72
- }
73
-
74
18
extension JSONDecoder {
75
- public static func makeWithDefaults( dateDecodingStrategy: DateDecodingStrategy = . safeISO8601 ) -> JSONDecoder {
19
+ public static func makeWithDefaults( dateDecodingStrategy: DateDecodingStrategy = . iso8601 ) -> JSONDecoder {
76
20
let decoder = JSONDecoder ( )
77
21
decoder. dateDecodingStrategy = dateDecodingStrategy
78
22
return decoder
@@ -82,7 +26,7 @@ extension JSONDecoder {
82
26
extension JSONEncoder {
83
27
public static func makeWithDefaults(
84
28
prettified: Bool = true ,
85
- dateEncodingStrategy: DateEncodingStrategy = . safeISO8601
29
+ dateEncodingStrategy: DateEncodingStrategy = . iso8601
86
30
) -> JSONEncoder {
87
31
Self . makeWithDefaults (
88
32
sortKeys: prettified,
@@ -96,19 +40,13 @@ extension JSONEncoder {
96
40
sortKeys: Bool ,
97
41
prettyPrint: Bool ,
98
42
escapeSlashes: Bool ,
99
- dateEncodingStrategy: DateEncodingStrategy = . safeISO8601
43
+ dateEncodingStrategy: DateEncodingStrategy = . iso8601
100
44
) -> JSONEncoder {
101
45
let encoder = JSONEncoder ( )
102
46
var outputFormatting : JSONEncoder . OutputFormatting = [ ]
103
47
104
48
if sortKeys {
105
- #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
106
- if #available( macOS 10 . 15 , iOS 11 . 0 , watchOS 4 . 0 , tvOS 11 . 0 , * ) {
107
- outputFormatting. insert ( . sortedKeys)
108
- }
109
- #else
110
49
outputFormatting. insert ( . sortedKeys)
111
- #endif
112
50
}
113
51
if prettyPrint {
114
52
outputFormatting. insert ( . prettyPrinted)
0 commit comments