@altcraft/react-native-sdk 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +119 -27
- package/Sdk.podspec +2 -2
- package/android/build.gradle +3 -4
- package/android/src/main/java/com/altcraft/sdk/SdkEventEmitter.kt +50 -0
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +663 -480
- package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +16 -8
- package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
- package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
- package/ios/Converter.swift +485 -48
- package/ios/Sdk.mm +195 -270
- package/ios/SdkModule.swift +456 -507
- package/lib/module/NativeSdk.js +35 -0
- package/lib/module/NativeSdk.js.map +1 -1
- package/lib/module/Utilities.js +178 -0
- package/lib/module/Utilities.js.map +1 -0
- package/lib/module/index.js +335 -245
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeSdk.d.ts +125 -33
- package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
- package/lib/typescript/src/Utilities.d.ts +47 -0
- package/lib/typescript/src/Utilities.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +218 -94
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeSdk.ts +169 -46
- package/src/Utilities.ts +180 -0
- package/src/index.tsx +410 -312
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
- package/ios/protocols/RNAPNSProvider.swift +0 -15
- package/ios/protocols/RNFCMProvider.swift +0 -24
- package/ios/protocols/RNHMSProvider.swift +0 -24
- package/ios/protocols/RNJWTProvider.swift +0 -15
package/ios/Converter.swift
CHANGED
|
@@ -1,27 +1,52 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//
|
|
2
|
+
// Converter.swift
|
|
3
|
+
// AltcraftRnBridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Andrey Pogodin.
|
|
6
|
+
//
|
|
7
|
+
// Copyright © 2026 Altcraft. All rights reserved.
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
import Foundation
|
|
10
|
+
import Altcraft
|
|
11
|
+
|
|
12
|
+
/// Utility namespace for bridging and normalizing values between React Native (Obj-C) and Swift.
|
|
13
|
+
///
|
|
14
|
+
/// `Converter` provides:
|
|
15
|
+
/// - Recursive normalization of Obj-C containers (`NSDictionary`/`NSArray`) into Swift-friendly
|
|
16
|
+
/// structures (`[String: Any?]`, `[Any]`).
|
|
17
|
+
/// - Safe conversion of RN values (`NSNull`, `NSNumber`, `NSString`) into predictable Swift types.
|
|
18
|
+
/// - Optional JSON parsing for strings that look like JSON objects/arrays.
|
|
19
|
+
/// - Helpers for building Altcraft SDK models (`AppInfo`, `UTM`, `Subscription`) from RN payloads.
|
|
20
|
+
/// - Conversion of arbitrary values into `UserDefaults`-compatible property list values.
|
|
21
|
+
///
|
|
22
|
+
/// All functions are stateless and intended to be used from the React Native bridge layer.
|
|
23
|
+
enum Converter: @unchecked Sendable {
|
|
24
|
+
|
|
25
|
+
nonisolated(unsafe) static let iso8601: ISO8601DateFormatter = {
|
|
26
|
+
let f = ISO8601DateFormatter()
|
|
27
|
+
return f
|
|
28
|
+
}()
|
|
29
|
+
|
|
30
|
+
// MARK: - Public (Generic normalization)
|
|
31
|
+
|
|
32
|
+
/// Converts arbitrary values into RN-safe Swift types.
|
|
33
|
+
///
|
|
34
|
+
/// Rules:
|
|
35
|
+
/// - `NSNull` -> `nil`
|
|
36
|
+
/// - `NSNumber` -> `Bool` (if CFBoolean) or normalized numeric (`Int`/`Int64`/`Double`)
|
|
37
|
+
/// - `NSString` -> `String` (and if it looks like JSON `{...}` or `[...]` it will be parsed)
|
|
38
|
+
/// - `NSDictionary`/`NSArray` -> recursively converted
|
|
39
|
+
/// - `Date` -> ISO8601 string
|
|
40
|
+
/// - `Data` -> base64 string
|
|
41
|
+
///
|
|
42
|
+
/// - Parameter value: Input value.
|
|
43
|
+
/// - Returns: Normalized value.
|
|
15
44
|
static func toAny(_ value: Any?) -> Any? {
|
|
16
45
|
guard let value else { return nil }
|
|
17
46
|
if value is NSNull { return nil }
|
|
18
47
|
|
|
19
|
-
// Bool may come as NSNumber, detect it explicitly.
|
|
20
|
-
if let b = value as? NSNumber, isCFBoolean(b) {
|
|
21
|
-
return b.boolValue
|
|
22
|
-
}
|
|
23
|
-
|
|
24
48
|
if let n = value as? NSNumber {
|
|
49
|
+
if isCFBoolean(n) { return n.boolValue }
|
|
25
50
|
return normalizeNumber(n)
|
|
26
51
|
}
|
|
27
52
|
|
|
@@ -30,7 +55,7 @@ enum Converter {
|
|
|
30
55
|
}
|
|
31
56
|
|
|
32
57
|
if let d = value as? Date {
|
|
33
|
-
return
|
|
58
|
+
return iso8601.string(from: d)
|
|
34
59
|
}
|
|
35
60
|
|
|
36
61
|
if let data = value as? Data {
|
|
@@ -45,7 +70,6 @@ enum Converter {
|
|
|
45
70
|
return toAnyArray(arr)
|
|
46
71
|
}
|
|
47
72
|
|
|
48
|
-
// Swift-native containers (defensive).
|
|
49
73
|
if let dict = value as? [String: Any?] {
|
|
50
74
|
var out: [String: Any?] = [:]
|
|
51
75
|
out.reserveCapacity(dict.count)
|
|
@@ -65,14 +89,21 @@ enum Converter {
|
|
|
65
89
|
}
|
|
66
90
|
|
|
67
91
|
if let arr = value as? [Any] {
|
|
68
|
-
|
|
92
|
+
var out: [Any] = []
|
|
93
|
+
out.reserveCapacity(arr.count)
|
|
94
|
+
for v in arr {
|
|
95
|
+
out.append(toAny(v) ?? NSNull())
|
|
96
|
+
}
|
|
97
|
+
return out
|
|
69
98
|
}
|
|
70
99
|
|
|
71
|
-
// Fallback: keep it safe for logging/bridge, but avoid relying on this.
|
|
72
100
|
return String(describing: value)
|
|
73
101
|
}
|
|
74
102
|
|
|
75
|
-
/// NSDictionary
|
|
103
|
+
/// Converts `NSDictionary` into `[String: Any?]` recursively.
|
|
104
|
+
///
|
|
105
|
+
/// - Parameter dict: Source dictionary.
|
|
106
|
+
/// - Returns: Converted dictionary or `nil`.
|
|
76
107
|
static func toAnyDict(_ dict: NSDictionary?) -> [String: Any?]? {
|
|
77
108
|
guard let dict, dict.count > 0 else { return nil }
|
|
78
109
|
var out: [String: Any?] = [:]
|
|
@@ -86,7 +117,10 @@ enum Converter {
|
|
|
86
117
|
return out.isEmpty ? nil : out
|
|
87
118
|
}
|
|
88
119
|
|
|
89
|
-
/// NSArray
|
|
120
|
+
/// Converts `NSArray` into `[Any]` recursively.
|
|
121
|
+
///
|
|
122
|
+
/// - Parameter arr: Source array.
|
|
123
|
+
/// - Returns: Converted array or `nil`.
|
|
90
124
|
static func toAnyArray(_ arr: NSArray?) -> [Any]? {
|
|
91
125
|
guard let arr, arr.count > 0 else { return nil }
|
|
92
126
|
var out: [Any] = []
|
|
@@ -103,13 +137,22 @@ enum Converter {
|
|
|
103
137
|
|
|
104
138
|
/// Converts value into a type supported by UserDefaults:
|
|
105
139
|
/// String/Bool/Int/Double/Data/Date/Array/Dictionary (property list).
|
|
140
|
+
///
|
|
106
141
|
/// If it cannot be represented as a property list, it falls back to JSON string when possible.
|
|
142
|
+
///
|
|
143
|
+
/// - Parameter value: Input value.
|
|
144
|
+
/// - Returns: Property-list compatible value or `nil`.
|
|
107
145
|
static func toPropertyListValue(_ value: Any?) -> Any? {
|
|
108
146
|
guard let value = toAny(value) else { return nil }
|
|
109
147
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
148
|
+
if value is String
|
|
149
|
+
|| value is Bool
|
|
150
|
+
|| value is Int
|
|
151
|
+
|| value is Int64
|
|
152
|
+
|| value is Double
|
|
153
|
+
|| value is Float
|
|
154
|
+
|| value is Data
|
|
155
|
+
|| value is Date {
|
|
113
156
|
if let i64 = value as? Int64 { return NSNumber(value: i64) }
|
|
114
157
|
if let f = value as? Float { return NSNumber(value: f) }
|
|
115
158
|
return value
|
|
@@ -121,9 +164,11 @@ enum Converter {
|
|
|
121
164
|
}
|
|
122
165
|
|
|
123
166
|
if let arr = value as? [Any] {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
167
|
+
var mapped: [Any] = []
|
|
168
|
+
mapped.reserveCapacity(arr.count)
|
|
169
|
+
for element in arr {
|
|
170
|
+
if element is NSNull { continue }
|
|
171
|
+
if let pv = toPropertyListValue(element) { mapped.append(pv) }
|
|
127
172
|
}
|
|
128
173
|
return mapped
|
|
129
174
|
}
|
|
@@ -132,9 +177,7 @@ enum Converter {
|
|
|
132
177
|
var mapped: [String: Any] = [:]
|
|
133
178
|
mapped.reserveCapacity(dict.count)
|
|
134
179
|
for (k, v) in dict {
|
|
135
|
-
if let pv = toPropertyListValue(v) {
|
|
136
|
-
mapped[k] = pv
|
|
137
|
-
}
|
|
180
|
+
if let pv = toPropertyListValue(v) { mapped[k] = pv }
|
|
138
181
|
}
|
|
139
182
|
return mapped
|
|
140
183
|
}
|
|
@@ -143,14 +186,11 @@ enum Converter {
|
|
|
143
186
|
var mapped: [String: Any] = [:]
|
|
144
187
|
mapped.reserveCapacity(dict.count)
|
|
145
188
|
for (k, v) in dict {
|
|
146
|
-
if let pv = toPropertyListValue(v) {
|
|
147
|
-
mapped[k] = pv
|
|
148
|
-
}
|
|
189
|
+
if let pv = toPropertyListValue(v) { mapped[k] = pv }
|
|
149
190
|
}
|
|
150
191
|
return mapped
|
|
151
192
|
}
|
|
152
193
|
|
|
153
|
-
// Fallback: JSON string.
|
|
154
194
|
if JSONSerialization.isValidJSONObject(value) {
|
|
155
195
|
if let data = try? JSONSerialization.data(withJSONObject: value, options: []),
|
|
156
196
|
let json = String(data: data, encoding: .utf8) {
|
|
@@ -161,22 +201,416 @@ enum Converter {
|
|
|
161
201
|
return String(describing: value)
|
|
162
202
|
}
|
|
163
203
|
|
|
164
|
-
|
|
204
|
+
/// Converts `[String: Any?]` into `[String: String]` by stringifying values.
|
|
205
|
+
///
|
|
206
|
+
/// - Parameter dict: Input dictionary.
|
|
207
|
+
/// - Returns: A string map or `nil`.
|
|
208
|
+
static func toStringMap(_ dict: [String: Any?]?) -> [String: String]? {
|
|
209
|
+
guard let dict, !dict.isEmpty else { return nil }
|
|
210
|
+
var out: [String: String] = [:]
|
|
211
|
+
out.reserveCapacity(dict.count)
|
|
212
|
+
for (k, v) in dict {
|
|
213
|
+
guard let v else { continue }
|
|
214
|
+
out[k] = String(describing: v)
|
|
215
|
+
}
|
|
216
|
+
return out.isEmpty ? nil : out
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
/// Builds `AppInfo` from a config value.
|
|
221
|
+
///
|
|
222
|
+
/// - Parameter any: `NSDictionary` / `[String: Any]` / `[String: Any?]` or `nil`.
|
|
223
|
+
/// - Returns: `AppInfo` or `nil` if all fields are blank.
|
|
224
|
+
static func appInfo(from any: Any?) -> AppInfo? {
|
|
225
|
+
guard let map = normalizeStringAnyMap(any) else { return nil }
|
|
226
|
+
|
|
227
|
+
let appID = (map["appID"] as? String) ?? ""
|
|
228
|
+
let appIID = (map["appIID"] as? String) ?? ""
|
|
229
|
+
let appVer = (map["appVer"] as? String) ?? ""
|
|
230
|
+
|
|
231
|
+
if appID.isEmpty && appIID.isEmpty && appVer.isEmpty { return nil }
|
|
232
|
+
return AppInfo(appID: appID, appIID: appIID, appVer: appVer)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/// Converts an Objective-C array of category items into `[CategoryData]`.
|
|
236
|
+
///
|
|
237
|
+
/// Supported item formats:
|
|
238
|
+
/// - String: treated as category `name` with `active = true`
|
|
239
|
+
/// - Map: supports `name`, `title`, `steady`, `active`
|
|
240
|
+
///
|
|
241
|
+
/// - Parameter cats: Source array.
|
|
242
|
+
/// - Returns: Category list or `nil`.
|
|
243
|
+
static func toCats(_ cats: NSArray?) -> [CategoryData]? {
|
|
244
|
+
guard let arr = cats, arr.count > 0 else { return nil }
|
|
245
|
+
|
|
246
|
+
var out: [CategoryData] = []
|
|
247
|
+
out.reserveCapacity(arr.count)
|
|
248
|
+
|
|
249
|
+
for item in arr {
|
|
250
|
+
if let s = item as? String, !s.isEmpty {
|
|
251
|
+
out.append(CategoryData(name: s, title: nil, steady: nil, active: true))
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if let m = item as? NSDictionary {
|
|
256
|
+
let mm = toAnyDict(m) ?? [:]
|
|
257
|
+
let name = mm["name"] as? String
|
|
258
|
+
if name == nil { continue }
|
|
259
|
+
|
|
260
|
+
let title = mm["title"] as? String
|
|
261
|
+
let steady = mm["steady"] as? Bool
|
|
262
|
+
let active = mm["active"] as? Bool
|
|
263
|
+
|
|
264
|
+
out.append(CategoryData(name: name, title: title, steady: steady, active: active))
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return out.isEmpty ? nil : out
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/// Converts `utm` dictionary into `UTM`.
|
|
272
|
+
///
|
|
273
|
+
/// - Parameter utm: Source map (`NSDictionary`) from RN.
|
|
274
|
+
/// - Returns: `UTM` or `nil`.
|
|
275
|
+
static func toUTM(_ utm: NSDictionary?) -> UTM? {
|
|
276
|
+
guard let map = toAnyDict(utm) else { return nil }
|
|
277
|
+
return UTM(
|
|
278
|
+
campaign: map["campaign"] as? String,
|
|
279
|
+
content: map["content"] as? String,
|
|
280
|
+
keyword: map["keyword"] as? String,
|
|
281
|
+
medium: map["medium"] as? String,
|
|
282
|
+
source: map["source"] as? String,
|
|
283
|
+
temp: map["temp"] as? String
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/// Converts a React Native subscription map into a native `Subscription` instance.
|
|
288
|
+
///
|
|
289
|
+
/// Supported `type` values:
|
|
290
|
+
/// - `email`, `sms`, `push`, `cc_data`
|
|
291
|
+
///
|
|
292
|
+
/// - Parameter subscriptionDict: Subscription dictionary from JS.
|
|
293
|
+
/// - Returns: A native subscription object or `nil` if the input is invalid.
|
|
294
|
+
static func toSubscription(_ subscriptionDict: NSDictionary?) -> Subscription? {
|
|
295
|
+
guard let dict = subscriptionDict else { return nil }
|
|
296
|
+
guard let type = stringFromAny(dict["type"]) else { return nil }
|
|
297
|
+
|
|
298
|
+
let resourceId =
|
|
299
|
+
intFromAny(dict["resource_id"]) ??
|
|
300
|
+
intFromAny(dict["resourceId"])
|
|
301
|
+
guard let resourceId else { return nil }
|
|
302
|
+
|
|
303
|
+
let status = stringFromAny(dict["status"])
|
|
304
|
+
let priority = intFromAny(dict["priority"])
|
|
305
|
+
|
|
306
|
+
let customFields: [String: JSONValue]? =
|
|
307
|
+
jsonValueDictFromAny(dict["custom_fields"]) ??
|
|
308
|
+
jsonValueDictFromAny(dict["customFields"])
|
|
309
|
+
|
|
310
|
+
let cats: [String]? = stringArrayFromAny(dict["cats"])
|
|
311
|
+
|
|
312
|
+
switch type {
|
|
313
|
+
case "email":
|
|
314
|
+
guard let email = stringFromAny(
|
|
315
|
+
dict["email"]
|
|
316
|
+
) else {
|
|
317
|
+
return nil
|
|
318
|
+
}
|
|
319
|
+
return EmailSubscription(
|
|
320
|
+
resourceId: resourceId,
|
|
321
|
+
email: email, status: status,
|
|
322
|
+
priority: priority,
|
|
323
|
+
customFields: customFields,
|
|
324
|
+
cats: cats
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
case "sms":
|
|
328
|
+
guard let phone = stringFromAny(
|
|
329
|
+
dict["phone"]
|
|
330
|
+
) else {
|
|
331
|
+
return nil
|
|
332
|
+
}
|
|
333
|
+
return SmsSubscription(
|
|
334
|
+
resourceId: resourceId,
|
|
335
|
+
phone: phone,
|
|
336
|
+
status: status,
|
|
337
|
+
priority: priority,
|
|
338
|
+
customFields: customFields,
|
|
339
|
+
cats: cats
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
case "push":
|
|
343
|
+
let provider = stringFromAny(dict["provider"])
|
|
344
|
+
let subscriptionId =
|
|
345
|
+
stringFromAny(dict["subscription_id"]) ??
|
|
346
|
+
stringFromAny(dict["subscriptionId"])
|
|
347
|
+
guard let provider, let subscriptionId else {
|
|
348
|
+
return nil
|
|
349
|
+
}
|
|
350
|
+
return PushSubscription(
|
|
351
|
+
resourceId: resourceId,
|
|
352
|
+
provider: provider,
|
|
353
|
+
subscriptionId: subscriptionId,
|
|
354
|
+
status: status,
|
|
355
|
+
priority: priority,
|
|
356
|
+
customFields: customFields,
|
|
357
|
+
cats: cats
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
case "cc_data":
|
|
361
|
+
guard let channel = stringFromAny(
|
|
362
|
+
dict["channel"]
|
|
363
|
+
), !channel.isEmpty else {
|
|
364
|
+
return nil
|
|
365
|
+
}
|
|
366
|
+
guard let ccData = jsonValueDictFromAny(
|
|
367
|
+
dict["cc_data"]
|
|
368
|
+
) ?? jsonValueDictFromAny(
|
|
369
|
+
dict["ccData"]
|
|
370
|
+
) else {
|
|
371
|
+
return nil
|
|
372
|
+
}
|
|
373
|
+
return CcDataSubscription(
|
|
374
|
+
resourceId: resourceId,
|
|
375
|
+
channel: channel,
|
|
376
|
+
ccData: ccData,
|
|
377
|
+
status: status,
|
|
378
|
+
priority: priority,
|
|
379
|
+
customFields: customFields,
|
|
380
|
+
cats: cats
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
default:
|
|
384
|
+
return nil
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/// Parses a JSON string into a `[String: JSONValue]` dictionary.
|
|
389
|
+
///
|
|
390
|
+
/// - Parameter jsonString: JSON string representing an object.
|
|
391
|
+
/// - Returns: A JSONValue dictionary or `nil` if parsing fails or the root is not an object.
|
|
392
|
+
static func jsonStringToJSONValueDict(_ jsonString: String) -> [String: JSONValue]? {
|
|
393
|
+
guard let data = jsonString.data(using: .utf8) else { return nil }
|
|
394
|
+
|
|
395
|
+
do {
|
|
396
|
+
let obj = try JSONSerialization.jsonObject(with: data, options: [])
|
|
397
|
+
guard let dict = obj as? [String: Any] else { return nil }
|
|
398
|
+
|
|
399
|
+
var out: [String: JSONValue] = [:]
|
|
400
|
+
out.reserveCapacity(dict.count)
|
|
401
|
+
|
|
402
|
+
for (k, v) in dict {
|
|
403
|
+
out[k] = anyToJSONValuePublic(v)
|
|
404
|
+
}
|
|
405
|
+
return out
|
|
406
|
+
} catch {
|
|
407
|
+
return nil
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/// Parses a JSON string into a `[String]` array.
|
|
412
|
+
///
|
|
413
|
+
/// - Parameter jsonString: JSON string representing an array.
|
|
414
|
+
/// - Returns: A string array or `nil` if parsing fails or elements are not strings.
|
|
415
|
+
static func jsonStringToStringArray(_ jsonString: String) -> [String]? {
|
|
416
|
+
guard let data = jsonString.data(using: .utf8) else { return nil }
|
|
417
|
+
|
|
418
|
+
do {
|
|
419
|
+
let obj = try JSONSerialization.jsonObject(with: data, options: [])
|
|
420
|
+
guard let array = obj as? [Any] else { return nil }
|
|
421
|
+
if array.isEmpty { return [] }
|
|
422
|
+
return array.compactMap { $0 as? String }
|
|
423
|
+
} catch {
|
|
424
|
+
return nil
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// Converts an arbitrary Swift value into a `JSONValue` suitable for SDK APIs.
|
|
429
|
+
///
|
|
430
|
+
/// - Parameter value: Input value.
|
|
431
|
+
/// - Returns: Converted `JSONValue`.
|
|
432
|
+
static func anyToJSONValuePublic(_ value: Any) -> JSONValue {
|
|
433
|
+
if value is NSNull { return .null }
|
|
434
|
+
if let s = value as? String { return .string(s) }
|
|
435
|
+
if let b = value as? Bool { return .bool(b) }
|
|
436
|
+
|
|
437
|
+
if let n = value as? NSNumber {
|
|
438
|
+
if isCFBoolean(n) { return .bool(n.boolValue) }
|
|
439
|
+
return .number(n.doubleValue)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if let dict = value as? [String: Any] {
|
|
443
|
+
var out: [String: JSONValue] = [:]
|
|
444
|
+
out.reserveCapacity(dict.count)
|
|
445
|
+
for (k, v) in dict { out[k] = anyToJSONValuePublic(v) }
|
|
446
|
+
return .object(out)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if let dict = value as? [String: Any?] {
|
|
450
|
+
var out: [String: JSONValue] = [:]
|
|
451
|
+
out.reserveCapacity(dict.count)
|
|
452
|
+
for (k, v) in dict {
|
|
453
|
+
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
454
|
+
else { out[k] = .null }
|
|
455
|
+
}
|
|
456
|
+
return .object(out)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if let arr = value as? [Any] {
|
|
460
|
+
var out: [JSONValue] = []
|
|
461
|
+
out.reserveCapacity(arr.count)
|
|
462
|
+
for v in arr { out.append(anyToJSONValuePublic(v)) }
|
|
463
|
+
return .array(out)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if let arr = value as? NSArray {
|
|
467
|
+
var out: [JSONValue] = []
|
|
468
|
+
out.reserveCapacity(arr.count)
|
|
469
|
+
for v in arr { out.append(anyToJSONValuePublic(v)) }
|
|
470
|
+
return .array(out)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return .string(String(describing: value))
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/// Attempts to convert a value into `Int`.
|
|
477
|
+
///
|
|
478
|
+
/// Supports `Int`, `NSNumber`, and numeric `String` values. Returns `nil` for `nil`/`NSNull` or
|
|
479
|
+
/// non-numeric inputs.
|
|
480
|
+
private static func intFromAny(_ any: Any?) -> Int? {
|
|
481
|
+
guard let any else { return nil }
|
|
482
|
+
if any is NSNull { return nil }
|
|
483
|
+
if let i = any as? Int { return i }
|
|
484
|
+
if let n = any as? NSNumber { return n.intValue }
|
|
485
|
+
if let s = any as? String {
|
|
486
|
+
let t = s.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
487
|
+
if t.isEmpty { return nil }
|
|
488
|
+
return Int(t)
|
|
489
|
+
}
|
|
490
|
+
return nil
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/// Converts a value into `String` if possible.
|
|
494
|
+
///
|
|
495
|
+
/// Supports `String`, `NSNumber`, and falls back to `String(describing:)`. Returns `nil` for
|
|
496
|
+
/// `nil`/`NSNull`.
|
|
497
|
+
private static func stringFromAny(_ any: Any?) -> String? {
|
|
498
|
+
guard let any else { return nil }
|
|
499
|
+
if any is NSNull { return nil }
|
|
500
|
+
if let s = any as? String { return s }
|
|
501
|
+
if let n = any as? NSNumber { return n.stringValue }
|
|
502
|
+
return String(describing: any)
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/// Converts a value into a `[String: JSONValue]` dictionary.
|
|
506
|
+
///
|
|
507
|
+
/// Accepts a JSON object string, `NSDictionary`, `[String: Any]`, or `[String: Any?]`. Returns `nil`
|
|
508
|
+
/// for `nil`/`NSNull` or incompatible inputs.
|
|
509
|
+
private static func jsonValueDictFromAny(_ any: Any?) -> [String: JSONValue]? {
|
|
510
|
+
guard let any else { return nil }
|
|
511
|
+
if any is NSNull { return nil }
|
|
512
|
+
|
|
513
|
+
if let s = any as? String {
|
|
514
|
+
return jsonStringToJSONValueDict(s)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if let d = any as? NSDictionary {
|
|
518
|
+
let dict = toAnyDict(d) ?? [:]
|
|
519
|
+
var out: [String: JSONValue] = [:]
|
|
520
|
+
out.reserveCapacity(dict.count)
|
|
521
|
+
for (k, v) in dict {
|
|
522
|
+
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
523
|
+
else { out[k] = .null }
|
|
524
|
+
}
|
|
525
|
+
return out
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if let d = any as? [String: Any] {
|
|
529
|
+
var out: [String: JSONValue] = [:]
|
|
530
|
+
out.reserveCapacity(d.count)
|
|
531
|
+
for (k, v) in d {
|
|
532
|
+
out[k] = anyToJSONValuePublic(v)
|
|
533
|
+
}
|
|
534
|
+
return out
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if let d = any as? [String: Any?] {
|
|
538
|
+
var out: [String: JSONValue] = [:]
|
|
539
|
+
out.reserveCapacity(d.count)
|
|
540
|
+
for (k, v) in d {
|
|
541
|
+
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
542
|
+
else { out[k] = .null }
|
|
543
|
+
}
|
|
544
|
+
return out
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return nil
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/// Converts a value into an array of `String`.
|
|
551
|
+
///
|
|
552
|
+
/// Accepts a JSON array string, `[Any]`, or `NSArray`. Returns `nil` for `nil`/`NSNull` or
|
|
553
|
+
/// incompatible inputs. Non-string elements are ignored.
|
|
554
|
+
private static func stringArrayFromAny(_ any: Any?) -> [String]? {
|
|
555
|
+
guard let any else { return nil }
|
|
556
|
+
if any is NSNull { return nil }
|
|
557
|
+
|
|
558
|
+
if let s = any as? String {
|
|
559
|
+
return jsonStringToStringArray(s)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if let arr = any as? [Any] {
|
|
563
|
+
if arr.isEmpty { return [] }
|
|
564
|
+
return arr.compactMap { $0 as? String }
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if let arr = any as? NSArray {
|
|
568
|
+
if arr.count == 0 { return [] }
|
|
569
|
+
return arr.compactMap { $0 as? String }
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return nil
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/// Normalizes a value into a `[String: Any]` map.
|
|
576
|
+
///
|
|
577
|
+
/// Accepts `[String: Any]`, `[String: Any?]` (dropping `nil` values), or `NSDictionary`. Returns
|
|
578
|
+
/// `nil` for `nil`/`NSNull` or incompatible inputs.
|
|
579
|
+
private static func normalizeStringAnyMap(_ any: Any?) -> [String: Any]? {
|
|
580
|
+
guard let any else { return nil }
|
|
581
|
+
if any is NSNull { return nil }
|
|
582
|
+
|
|
583
|
+
if let m = any as? [String: Any] { return m }
|
|
584
|
+
|
|
585
|
+
if let mOpt = any as? [String: Any?] {
|
|
586
|
+
var out: [String: Any] = [:]
|
|
587
|
+
out.reserveCapacity(mOpt.count)
|
|
588
|
+
for (k, v) in mOpt { if let v { out[k] = v } }
|
|
589
|
+
return out.isEmpty ? nil : out
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if let ns = any as? NSDictionary {
|
|
593
|
+
return ns as? [String: Any]
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
return nil
|
|
597
|
+
}
|
|
165
598
|
|
|
599
|
+
/// Returns `true` if the given `NSNumber` represents a CoreFoundation boolean.
|
|
166
600
|
private static func isCFBoolean(_ n: NSNumber) -> Bool {
|
|
167
601
|
CFGetTypeID(n) == CFBooleanGetTypeID()
|
|
168
602
|
}
|
|
169
603
|
|
|
170
|
-
/// NSNumber
|
|
171
|
-
///
|
|
172
|
-
///
|
|
604
|
+
/// Normalizes `NSNumber` into `Int`, `Int64`, or `Double`.
|
|
605
|
+
///
|
|
606
|
+
/// If the value has a fractional part, returns `Double`. If it is an integer, returns `Int` when it
|
|
607
|
+
/// fits, otherwise `Int64`.
|
|
173
608
|
private static func normalizeNumber(_ n: NSNumber) -> Any {
|
|
174
609
|
let d = n.doubleValue
|
|
175
610
|
if !d.isFinite { return d }
|
|
176
611
|
|
|
177
612
|
let asInt64 = Int64(d)
|
|
178
|
-
|
|
179
|
-
if !isInteger { return d }
|
|
613
|
+
if d != Double(asInt64) { return d }
|
|
180
614
|
|
|
181
615
|
if asInt64 >= Int64(Int.min) && asInt64 <= Int64(Int.max) {
|
|
182
616
|
return Int(asInt64)
|
|
@@ -185,19 +619,22 @@ enum Converter {
|
|
|
185
619
|
}
|
|
186
620
|
}
|
|
187
621
|
|
|
188
|
-
///
|
|
622
|
+
/// Parses a string that looks like JSON (`{...}` or `[...]`) and normalizes the result.
|
|
623
|
+
///
|
|
624
|
+
/// If parsing fails, returns the original string.
|
|
189
625
|
private static func parseJSONIfPossible(_ s: String) -> Any {
|
|
190
626
|
let trimmed = s.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
191
|
-
|
|
627
|
+
if trimmed.isEmpty { return "" }
|
|
192
628
|
|
|
193
629
|
guard let first = trimmed.first, first == "{" || first == "[" else {
|
|
194
630
|
return s
|
|
195
631
|
}
|
|
196
632
|
|
|
197
633
|
guard let data = trimmed.data(using: .utf8) else { return s }
|
|
198
|
-
guard let obj = try? JSONSerialization.jsonObject(
|
|
634
|
+
guard let obj = try? JSONSerialization.jsonObject(
|
|
635
|
+
with: data, options: []
|
|
636
|
+
) else { return s }
|
|
199
637
|
|
|
200
|
-
// obj will be NSArray/NSDictionary -> normalize through the same converter.
|
|
201
638
|
return toAny(obj) ?? s
|
|
202
639
|
}
|
|
203
|
-
}
|
|
640
|
+
}
|