-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageJSON.swift
More file actions
66 lines (54 loc) · 1.8 KB
/
StorageJSON.swift
File metadata and controls
66 lines (54 loc) · 1.8 KB
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
57
58
59
60
61
62
63
64
65
66
//
// StorageJSON.swift
// Funbox
//
// Created by Антон Кочетков on 29.11.2021.
//
import Foundation
class StorageJSON {
func convertFromJSON() -> [Device]{
let dataString = """
[{ "name": "apple iphone", "price": 1000.0, "count": 1 },
{ "name": "apple ipod", "price": 900.0, "count": 2 },
{ "name": "apple ipad", "price": 1300.0, "count": 1 }]
"""
let devices = try? JSONDecoder().decode([Device].self, from: Data(dataString.utf8))
return devices ?? []
}
func convertInJSON(_ devices: [Device]){
let encodeDevices = try? JSONEncoder().encode(devices)
let strData = String(decoding: encodeDevices!, as: UTF8.self)
print(strData)
}
}
class StorageJSONAdapter: StorageProtocol {
private var storageJSON: StorageJSON
init(storage: StorageJSON) {
storageJSON = storage
}
func getData() -> [Device] {
return storageJSON.convertFromJSON()
}
func saveData(_ devices: [Device]) {
storageJSON.convertInJSON(devices)
}
}
extension Device: Codable {
enum CodingKeys: String, CodingKey {
case name
case price
case count
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(price, forKey: .price)
try container.encode(count, forKey: .count)
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
price = try container.decode(Double.self, forKey: .price)
count = try container.decode(Int.self, forKey: .count)
}
}