@@ -26,14 +26,44 @@ public extension JSONFileModule {
26
26
// Note: Why not use fs.readFile etc? Because we want to serialize/deserialize
27
27
// on the background thread.
28
28
29
+ /**
30
+ * Reads a file as JSON.
31
+ *
32
+ * This uses Foundation `JSONSerialization`, the returned objects are the
33
+ * respective Swift objects wrapped in `Any`.
34
+ *
35
+ * The JSON parsing happens on the I/O thread (`fs.threadPool`).
36
+ *
37
+ * Example:
38
+ *
39
+ * jsonfile.readFile("/tmp/myfile.json) { error, value in
40
+ * if let error = error {
41
+ * console.error("loading failed:", error)
42
+ * }
43
+ * else {
44
+ * print("Loaded JSON:", value)
45
+ * }
46
+ * }
47
+ *
48
+ * - Parameters:
49
+ * - eventLoop: The NIO EventLoop to call the callback on. Defaults to the
50
+ * current EventLoop, if available.
51
+ * - path: The path of the file to read from.
52
+ * - options: The `JSONSerialization.ReadingOptions`, defaults to none
53
+ * - yield: Callback which is called when the reading and decoding
54
+ * succeeded or failed. The first argument is the Error if one
55
+ * occurred, the second argument the JSON objects read (or the
56
+ * error).
57
+ */
29
58
static func readFile( on eventLoop : EventLoop ? = nil ,
30
59
_ path : String ,
31
60
options : JSONSerialization . ReadingOptions = [ ] ,
32
61
yield : @escaping ( Swift . Error ? , Any ) -> Void )
33
62
-> Void
34
63
{
35
64
let module = MacroCore . shared. retain ( )
36
-
65
+ let loop = module. fallbackEventLoop ( eventLoop)
66
+
37
67
FileSystemModule . threadPool. submit { shouldRun in
38
68
let result : Any
39
69
let resultError : Swift . Error ?
@@ -56,12 +86,40 @@ public extension JSONFileModule {
56
86
resultError = ChannelError . ioOnClosedChannel
57
87
}
58
88
59
- module . fallbackEventLoop ( eventLoop ) . execute {
89
+ loop . execute {
60
90
yield( resultError, result)
61
91
module. release ( )
62
92
}
63
93
}
64
94
}
95
+
96
+ /**
97
+ * Writes JSON objects to a file.
98
+ *
99
+ * This uses Foundation `JSONSerialization`, the expected objects are the
100
+ * respective Swift objects wrapped in `Any`.
101
+ * There is also a `Codable` version of `writeFile`.
102
+ *
103
+ * The JSON generation happens on the I/O thread (`fs.threadPool`).
104
+ *
105
+ * Example:
106
+ *
107
+ * try jsonfile.writeFile("/tmp/myfile.json", [ "key": "value" ]) { err in
108
+ * if let err = err {
109
+ * print("Writing failed:", err)
110
+ * }
111
+ * }
112
+ *
113
+ * - Parameters:
114
+ * - eventLoop: The NIO EventLoop to call the callback on. Defaults to the
115
+ * current EventLoop, if available.
116
+ * - path: The path of the file to write to.
117
+ * - json: The JSON objects representing the structure to write.
118
+ * - options: The `JSONSerialization.WritingOptions`, defaults to none
119
+ * - yield: Callback which is called when the encoding and writing
120
+ * succeeded or failed. The first argument carries the Error
121
+ * if one occurred, or nil.
122
+ */
65
123
static func writeFile( on eventLoop : EventLoop ? = nil ,
66
124
_ path : String ,
67
125
_ json : Any ,
@@ -70,6 +128,7 @@ public extension JSONFileModule {
70
128
-> Void
71
129
{
72
130
let module = MacroCore . shared. retain ( )
131
+ let loop = module. fallbackEventLoop ( eventLoop)
73
132
74
133
FileSystemModule . threadPool. submit { shouldRun in
75
134
let resultError : Swift . Error ?
@@ -89,12 +148,39 @@ public extension JSONFileModule {
89
148
resultError = ChannelError . ioOnClosedChannel
90
149
}
91
150
92
- module . fallbackEventLoop ( eventLoop ) . execute {
151
+ loop . execute {
93
152
yield( resultError)
94
153
module. release ( )
95
154
}
96
155
}
97
156
}
157
+
158
+ /**
159
+ * Writes JSON objects to a file.
160
+ *
161
+ * This uses Foundation `JSONEncoder`, the expected objects are Swift values
162
+ * conforming to the Swift `Encodable` protocol.
163
+ *
164
+ * The JSON generation happens on the I/O thread (`fs.threadPool`).
165
+ *
166
+ * Example:
167
+ *
168
+ * try jsonfile.writeFile("/tmp/myfile.json", [ "key": "value" ]) { err in
169
+ * if let err = err {
170
+ * print("Writing failed:", err)
171
+ * }
172
+ * }
173
+ *
174
+ * - Parameters:
175
+ * - eventLoop: The NIO EventLoop to call the callback on. Defaults to the
176
+ * current EventLoop, if available.
177
+ * - path: The path of the file to write to.
178
+ * - json: The `Encodable` JSON objects to write.
179
+ * - options: The `JSONSerialization.WritingOptions`, defaults to none
180
+ * - yield: Callback which is called when the encoding and writing
181
+ * succeeded or failed. The first argument carries the Error
182
+ * if one occurred, or nil.
183
+ */
98
184
static func writeFile< T> ( on eventLoop : EventLoop ? = nil ,
99
185
_ path : String ,
100
186
_ json : T ,
@@ -104,6 +190,7 @@ public extension JSONFileModule {
104
190
where T: Encodable
105
191
{
106
192
let module = MacroCore . shared. retain ( )
193
+ let loop = module. fallbackEventLoop ( eventLoop)
107
194
108
195
FileSystemModule . threadPool. submit { shouldRun in
109
196
let resultError : Swift . Error ?
@@ -122,13 +209,29 @@ public extension JSONFileModule {
122
209
resultError = ChannelError . ioOnClosedChannel
123
210
}
124
211
125
- module . fallbackEventLoop ( eventLoop ) . execute {
212
+ loop . execute {
126
213
yield( resultError)
127
214
module. release ( )
128
215
}
129
216
}
130
217
}
131
218
219
+ /**
220
+ * Reads a file as JSON.
221
+ *
222
+ * This uses Foundation `JSONSerialization`, the returned objects are the
223
+ * respective Swift objects wrapped in `Any`.
224
+ *
225
+ * Example:
226
+ *
227
+ * let json = try jsonfile.readFileSync("/tmp/myfile.json)
228
+ * print("Loaded JSON:", json)
229
+ *
230
+ * - Parameters:
231
+ * - path: The path of the file to read from.
232
+ * - options: The `JSONSerialization.ReadingOptions`, defaults to none
233
+ */
234
+ @inlinable
132
235
static func readFileSync( _ path : String ,
133
236
options : JSONSerialization . ReadingOptions = [ ] )
134
237
throws -> Any
@@ -138,6 +241,23 @@ public extension JSONFileModule {
138
241
return json
139
242
}
140
243
244
+ /**
245
+ * Writes JSON objects to a file.
246
+ *
247
+ * This uses Foundation `JSONSerialization`, the expected objects are the
248
+ * respective Swift objects wrapped in `Any`.
249
+ * There is also a `Codable` version of `writeFile`.
250
+ *
251
+ * Example:
252
+ *
253
+ * try jsonfile.writeFileSync("/tmp/myfile.json", [ "key": "value" ]
254
+ *
255
+ * - Parameters:
256
+ * - path: The path of the file to write to.
257
+ * - json: The JSON objects representing the structure to write.
258
+ * - options: The `JSONSerialization.WritingOptions`, defaults to none
259
+ */
260
+ @inlinable
141
261
static func writeFileSync( _ path : String ,
142
262
_ json : Any ,
143
263
options : JSONSerialization . WritingOptions = [ ] )
@@ -148,6 +268,26 @@ public extension JSONFileModule {
148
268
try data. write ( to: URL ( fileURLWithPath: path) , options: [ . atomic] )
149
269
}
150
270
271
+ /**
272
+ * Writes JSON objects to a file.
273
+ *
274
+ * This uses Foundation `JSONEncoder`, the expected objects are Swift values
275
+ * conforming to the Swift `Encodable` protocol.
276
+ *
277
+ * Example:
278
+ *
279
+ * try jsonfile.writeFile("/tmp/myfile.json", [ "key": "value" ]) { err in
280
+ * if let err = err {
281
+ * print("Writing failed:", err)
282
+ * }
283
+ * }
284
+ *
285
+ * - Parameters:
286
+ * - path: The path of the file to write to.
287
+ * - json: The `Encodable` JSON objects to write.
288
+ * - options: The `JSONSerialization.WritingOptions`, defaults to none
289
+ */
290
+ @inlinable
151
291
static func writeFileSync< T> ( _ path : String ,
152
292
_ json : T ,
153
293
options : JSONSerialization . WritingOptions = [ ] )
0 commit comments