Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Shared
7
import SwiftyJSON
8
9
open class BasePayloadJSON {
10
let json: JSON
11
required public init(_ jsonString: String) {
12
self.json = JSON(parseJSON: jsonString)
13
}
14
15
public init(_ json: JSON) {
16
self.json = json
17
}
18
19
// Override me.
20
public func isValid() -> Bool {
21
return self.json.type != .unknown &&
22
self.json.error == nil
23
}
24
25
subscript(key: String) -> JSON {
26
get {
27
return json[key]
28
}
29
}
30
}
31
32
/**
34
* "In addition to these custom collection object structures, the
35
* Encrypted DataObject adds fields like id and deleted."
36
*/
37
open class CleartextPayloadJSON: BasePayloadJSON {
38
// Override me.
39
override open func isValid() -> Bool {
40
return super.isValid() && self["id"].isString()
41
}
42
43
open var id: String {
44
return self["id"].string!
45
}
46
47
open var deleted: Bool {
48
let d = self["deleted"]
49
if let bool = d.bool {
50
return bool
51
} else {
52
return false
53
}
54
}
55
56
// Override me.
57
// Doesn't check id. Should it?
58
open func equalPayloads (_ obj: CleartextPayloadJSON) -> Bool {
59
return self.deleted == obj.deleted
60
}
61
}