diff --git a/Cache.xcodeproj/project.pbxproj b/Cache.xcodeproj/project.pbxproj index 97134d0..712424e 100644 --- a/Cache.xcodeproj/project.pbxproj +++ b/Cache.xcodeproj/project.pbxproj @@ -42,9 +42,9 @@ D21B669E1F6A724700125DE1 /* MemoryConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF984E1F694FFA00CE8F68 /* MemoryConfig.swift */; }; D21B66A11F6A747000125DE1 /* ImageWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF98761F69513800CE8F68 /* ImageWrapperTests.swift */; }; D236F31A1F6BEF73004EE01F /* StorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D236F3191F6BEF73004EE01F /* StorageTests.swift */; }; - D285143B1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143A1F6FFB6300C674D1 /* ObjectSerializer.swift */; }; - D285143C1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143A1F6FFB6300C674D1 /* ObjectSerializer.swift */; }; - D285143D1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143A1F6FFB6300C674D1 /* ObjectSerializer.swift */; }; + D28897051F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; }; + D28897061F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; }; + D28897071F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; }; D28A1D221F6FFEF50030DF81 /* ObjectConverterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143E1F6FFE1F00C674D1 /* ObjectConverterTests.swift */; }; D28A1D231F6FFEF50030DF81 /* ObjectConverterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143E1F6FFE1F00C674D1 /* ObjectConverterTests.swift */; }; D28A1D241F6FFEF60030DF81 /* ObjectConverterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D285143E1F6FFE1F00C674D1 /* ObjectConverterTests.swift */; }; @@ -137,8 +137,8 @@ BDEDD3561DBCE5B1007416A6 /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; }; BDEDD3781DBCEB8A007416A6 /* Cache-tvOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Cache-tvOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; D236F3191F6BEF73004EE01F /* StorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageTests.swift; sourceTree = ""; }; - D285143A1F6FFB6300C674D1 /* ObjectSerializer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectSerializer.swift; sourceTree = ""; }; D285143E1F6FFE1F00C674D1 /* ObjectConverterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectConverterTests.swift; sourceTree = ""; }; + D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "JSONDecoder+Extensions.swift"; sourceTree = ""; }; D292DAF01F6A85F30060F614 /* SyncStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncStorage.swift; sourceTree = ""; }; D292DAF41F6A94000060F614 /* AsyncStorageAware.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncStorageAware.swift; sourceTree = ""; }; D292DAF81F6A962D0060F614 /* AsyncStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncStorage.swift; sourceTree = ""; }; @@ -271,6 +271,7 @@ D2CF984F1F694FFA00CE8F68 /* Extensions */ = { isa = PBXGroup; children = ( + D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */, D2CF98501F694FFA00CE8F68 /* Date+Extensions.swift */, ); path = Extensions; @@ -287,7 +288,6 @@ D2CF98591F694FFA00CE8F68 /* StorageError.swift */, D2CF98861F695B8F00CE8F68 /* Types.swift */, D292DAFC1F6A970B0060F614 /* Result.swift */, - D285143A1F6FFB6300C674D1 /* ObjectSerializer.swift */, ); path = Library; sourceTree = ""; @@ -841,7 +841,6 @@ files = ( D21B66861F6A723C00125DE1 /* Entry.swift in Sources */, D21B66971F6A724000125DE1 /* StorageAware.swift in Sources */, - D285143D1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */, D21B669D1F6A724600125DE1 /* DiskConfig.swift in Sources */, D21B66961F6A724000125DE1 /* Storage.swift in Sources */, D21B66871F6A723C00125DE1 /* ExpirationMode.swift in Sources */, @@ -858,6 +857,7 @@ D5A138C21EB29BFA00881A20 /* UIImage+Extensions.swift in Sources */, D21B66851F6A723C00125DE1 /* DataSerializer.swift in Sources */, D21B66941F6A724000125DE1 /* HybridStorage.swift in Sources */, + D28897071F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */, D292DAF71F6A94000060F614 /* AsyncStorageAware.swift in Sources */, D21B66931F6A724000125DE1 /* DiskStorage.swift in Sources */, D21B66981F6A724000125DE1 /* TypeWrapperStorage.swift in Sources */, @@ -906,7 +906,6 @@ files = ( D21B667D1F6A723C00125DE1 /* Entry.swift in Sources */, D21B66911F6A723F00125DE1 /* StorageAware.swift in Sources */, - D285143C1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */, D21B669B1F6A724600125DE1 /* DiskConfig.swift in Sources */, D21B66901F6A723F00125DE1 /* Storage.swift in Sources */, D21B667E1F6A723C00125DE1 /* ExpirationMode.swift in Sources */, @@ -923,6 +922,7 @@ D5A138C41EB29C2100881A20 /* NSImage+Extensions.swift in Sources */, D21B667C1F6A723C00125DE1 /* DataSerializer.swift in Sources */, D21B668E1F6A723F00125DE1 /* HybridStorage.swift in Sources */, + D28897061F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */, D292DAF61F6A94000060F614 /* AsyncStorageAware.swift in Sources */, D21B668D1F6A723F00125DE1 /* DiskStorage.swift in Sources */, D21B66921F6A723F00125DE1 /* TypeWrapperStorage.swift in Sources */, @@ -951,7 +951,6 @@ files = ( D2CF98651F694FFA00CE8F68 /* Entry.swift in Sources */, D2CF98681F694FFA00CE8F68 /* ImageWrapper.swift in Sources */, - D285143B1F6FFB6300C674D1 /* ObjectSerializer.swift in Sources */, D2CF986C1F694FFA00CE8F68 /* HybridStorage.swift in Sources */, D2CF98871F695B8F00CE8F68 /* Types.swift in Sources */, D2CF98621F694FFA00CE8F68 /* Date+Extensions.swift in Sources */, @@ -968,6 +967,7 @@ D2CF986A1F694FFA00CE8F68 /* StorageError.swift in Sources */, D5A138C11EB29BFA00881A20 /* UIImage+Extensions.swift in Sources */, D2CF986E1F694FFA00CE8F68 /* Storage.swift in Sources */, + D28897051F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */, D292DAF51F6A94000060F614 /* AsyncStorageAware.swift in Sources */, D2CF98611F694FFA00CE8F68 /* MemoryConfig.swift in Sources */, D2CF98661F694FFA00CE8F68 /* ExpirationMode.swift in Sources */, diff --git a/README.md b/README.md index d594d18..b8d7808 100644 --- a/README.md +++ b/README.md @@ -279,12 +279,12 @@ Most of the time, our use case is to fetch some json from backend, display it wh While `Storage` can persist `String` or `Data`, we recommend persisting the strong typed objects, since those are the objects that you will use to display in UI. Furthermore, if the json data can't be converted to strongly typed objects, what's the point of saving it ? 😉 -You can use `ObjectConverter` to convert json dictionary, string or data to objects. +You can use `JSONDecoder` to decode json dictionary, string or data to objects. ```swift -let user = ObjectConverter.convert(jsonString, to: User.self) -let cities = Object.Converter.convert(jsonDictionary, to: [City].self) -let dragons = ObjectConverter.convert(jsonData, to: [Dragon].self) +let user = JSONDecoder.decode(jsonString, to: User.self) +let cities = JSONDecoder.decode(jsonDictionary, to: [City].self) +let dragons = JSONDecoder.decode(jsonData, to: [Dragon].self) ``` This is how you perform object converting and saving with `Alamofire` @@ -292,7 +292,7 @@ This is how you perform object converting and saving with `Alamofire` ```swift Alamofire.request("https://gameofthrones.org/mostFavoriteCharacter").responseString { response in do { - let user = try ObjectConverter.convert(response.result.value, to: User.self) + let user = try JSONDecoder.decode(response.result.value, to: User.self) try storage.setObject(user, forKey: "most favorite character") } catch { print(error) diff --git a/Source/Shared/Library/ObjectSerializer.swift b/Source/Shared/Extensions/JSONDecoder+Extensions.swift similarity index 71% rename from Source/Shared/Library/ObjectSerializer.swift rename to Source/Shared/Extensions/JSONDecoder+Extensions.swift index 7493e60..8352440 100644 --- a/Source/Shared/Library/ObjectSerializer.swift +++ b/Source/Shared/Extensions/JSONDecoder+Extensions.swift @@ -1,7 +1,7 @@ import Foundation /// Convert json string, dictionary, data to Codable objects -public class ObjectConverter { +public extension JSONDecoder { /// Convert json string to Codable object /// /// - Parameters: @@ -9,12 +9,12 @@ public class ObjectConverter { /// - type: Type information. /// - Returns: Codable object. /// - Throws: Error if failed. - static func convert(_ string: String, to type: T.Type) throws -> T { + static func decode(_ string: String, to type: T.Type) throws -> T { guard let data = string.data(using: .utf8) else { throw StorageError.decodingFailed } - return try convert(data, to: type.self) + return try decode(data, to: type.self) } /// Convert json dictionary to Codable object @@ -24,9 +24,9 @@ public class ObjectConverter { /// - type: Type information. /// - Returns: Codable object /// - Throws: Error if failed - static func convert(_ json: [String: Any], to type: T.Type) throws -> T { + static func decode(_ json: [String: Any], to type: T.Type) throws -> T { let data = try JSONSerialization.data(withJSONObject: json, options: []) - return try convert(data, to: type) + return try decode(data, to: type) } /// Convert json data to Codable object @@ -36,7 +36,7 @@ public class ObjectConverter { /// - type: Type information. /// - Returns: Codable object /// - Throws: Error if failed - static func convert(_ data: Data, to type: T.Type) throws -> T { + static func decode(_ data: Data, to type: T.Type) throws -> T { return try JSONDecoder().decode(T.self, from: data) } } diff --git a/Tests/iOS/Tests/Library/ObjectConverterTests.swift b/Tests/iOS/Tests/Library/ObjectConverterTests.swift index 9ca132c..f6b605b 100644 --- a/Tests/iOS/Tests/Library/ObjectConverterTests.swift +++ b/Tests/iOS/Tests/Library/ObjectConverterTests.swift @@ -1,7 +1,7 @@ import XCTest @testable import Cache -final class ObjectStorageTests: XCTestCase { +final class JSONDecoderExtensionsTests: XCTestCase { private var storage: HybridStorage! override func setUp() { @@ -23,7 +23,7 @@ final class ObjectStorageTests: XCTestCase { "last_name": "Snow" ] - let user = try ObjectConverter.convert(json, to: User.self) + let user = try JSONDecoder.decode(json, to: User.self) try storage.setObject(user, forKey: "user") let cachedObject = try storage.object(ofType: User.self, forKey: "user") @@ -33,7 +33,7 @@ final class ObjectStorageTests: XCTestCase { func testJsonString() throws { let string: String = "{\"first_name\": \"John\", \"last_name\": \"Snow\"}" - let user = try ObjectConverter.convert(string, to: User.self) + let user = try JSONDecoder.decode(string, to: User.self) try storage.setObject(user, forKey: "user") let cachedObject = try storage.object(ofType: User.self, forKey: "user")