forked from parse-community/Parse-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONStorage.swift
56 lines (53 loc) · 1.83 KB
/
JSONStorage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// JSONStorage.swift
// ParseSwift
//
// Created by Daniel Blyth on 21/10/21.
// Copyright © 2021 Parse Community. All rights reserved.
//
import Foundation
struct JSONStorage {
public static var store: [String: [String: AnyObject]] = [:]
static func put<T>(object: T) where T: ParseObject {
do {
let encoded = try ParseCoding.jsonEncoder().encode(object)
if let objectId = object.objectId,
let json = try JSONSerialization.jsonObject(with: encoded, options: []) as? [String: AnyObject] {
store[objectId] = json
}
} catch {
print("Could not store object")
}
}
static func put<T>(objects: [T]) where T: ParseObject {
for parseObject in objects {
put(object: parseObject)
}
}
static func getEncodedKeys(object: ParseType) -> Set<String> {
var keysToSkip: Set<String>!
if !ParseSwift.configuration.allowCustomObjectId {
keysToSkip = ParseEncoder.SkipKeys.object.keys()
} else {
keysToSkip = ParseEncoder.SkipKeys.customObjectId.keys()
}
guard let objectable = object as? Objectable else {
return keysToSkip
}
do {
let encoded = try ParseCoding.parseEncoder().encode(object)
if let id = objectable.objectId,
let previousStore = store[id],
let json = try JSONSerialization.jsonObject(with: encoded, options: []) as? [String: AnyObject] {
for keyName in json.keys where
(json[keyName] as? NSObject) == (previousStore[keyName] as? NSObject)
&& !keysToSkip.contains(keyName) {
keysToSkip.insert(keyName)
}
}
}
} catch {
}
return keysToSkip
}
}