From 2bf09f9601dd8ace2ec7cb596aa653f3637c9bb1 Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:02:01 -0400 Subject: [PATCH 1/7] Method to create JSON Array from Data --- Sources/Gloss/ExtensionArray.swift | 29 +++++++++++++++++++++++++++++ Sources/GlossTests/GlossTests.swift | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Sources/Gloss/ExtensionArray.swift b/Sources/Gloss/ExtensionArray.swift index b149068..17b11ea 100644 --- a/Sources/Gloss/ExtensionArray.swift +++ b/Sources/Gloss/ExtensionArray.swift @@ -53,6 +53,35 @@ public extension Array where Element: JSONDecodable & Decodable { return models } + /** + Returns array of new objects created from provided data. + If creation of JSON or any decodings fail, nil is returned. + + - parameter data: Raw JSON data. + - parameter jsonDecoder: A `Swift.JSONDecoder`. + - parameter serializer: Serializer to use when creating JSON from data. + - parameter ooptions: Options for reading the JSON data. + - parameter logger: Logs issues with `Swift.Decodable`. + + - returns: Object or nil. + */ + static func from(decodableData data: Data, jsonDecoder: JSONDecoder = JSONDecoder(), serializer: JSONSerializer = GlossJSONSerializer(), options: JSONSerialization.ReadingOptions = .mutableContainers, logger: GlossLogger = GlossLogger()) -> [Element]? { + do { + let jsonArray = try jsonDecoder.decode([Element].self, from: data) + return jsonArray + } catch { + logger.log(message: "Swift.Decodable error: \(error)") + + guard + let jsonArray = serializer.jsonArray(from: data, options: options), + let models = [Element].from(jsonArray: jsonArray) else { + return nil + } + + return models + } + } + } public extension Array where Element: JSONEncodable & Encodable { diff --git a/Sources/GlossTests/GlossTests.swift b/Sources/GlossTests/GlossTests.swift index 0723c00..bfa7114 100644 --- a/Sources/GlossTests/GlossTests.swift +++ b/Sources/GlossTests/GlossTests.swift @@ -332,7 +332,7 @@ class GlossTests: XCTestCase { func testModelArrayFromJSONArrayRawData() { let data = try! JSONSerialization.data(withJSONObject: testJSONArray!, options: []) - let modelArray = [TestModel].from(data: data) + let modelArray: [TestModel]? = .from(decodableData: data) XCTAssertNotNil(modelArray, "Model array from Data should not be nil.") } From d2c16d88e969be6d77eccf4d02ab55424a48327f Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 08:52:34 -0400 Subject: [PATCH 2/7] Fix not using param --- Sources/Gloss/ExtensionArray.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Gloss/ExtensionArray.swift b/Sources/Gloss/ExtensionArray.swift index 17b11ea..13803e4 100644 --- a/Sources/Gloss/ExtensionArray.swift +++ b/Sources/Gloss/ExtensionArray.swift @@ -167,7 +167,7 @@ public extension Array where Element: JSONDecodable { */ static func from(data: Data, serializer: JSONSerializer = GlossJSONSerializer(), options: JSONSerialization.ReadingOptions = .mutableContainers) -> [Element]? { guard - let jsonArray = (try? JSONSerialization.jsonObject(with: data, options: options)) as? [JSON], + let jsonArray = serializer.jsonArray(from: data, options: options), let models = [Element].from(jsonArray: jsonArray) else { return nil } From ec176609b6c3f72a547cc335c2049fc054f32e41 Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:02:09 -0400 Subject: [PATCH 3/7] Consistent syntax in tests --- Sources/GlossTests/GlossTests.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/GlossTests/GlossTests.swift b/Sources/GlossTests/GlossTests.swift index bfa7114..455a64d 100644 --- a/Sources/GlossTests/GlossTests.swift +++ b/Sources/GlossTests/GlossTests.swift @@ -143,7 +143,7 @@ class GlossTests: XCTestCase { } func testModelsFromJSONArrayProducesValidModels() { - let result = [TestModel].from(decodableJSONArray: testJSONArray!) + let result: [TestModel]? = .from(decodableJSONArray: testJSONArray!) let model1: TestModel = result![0] let model2: TestModel = result![1] @@ -206,7 +206,7 @@ class GlossTests: XCTestCase { func testModelsFromJSONArrayReturnsNilIfDecodingFails() { testJSONArray![0].removeValue(forKey: "bool") - let result = [TestModel].from(decodableJSONArray: testJSONArray!) + let result: [TestModel]? = .from(decodableJSONArray: testJSONArray!) XCTAssertNil(result, "Model array from JSON array should be nil is any decoding fails.") } @@ -303,7 +303,7 @@ class GlossTests: XCTestCase { invalidJSON.removeValue(forKey: "bool") var jsonArray = testJSONArray! jsonArray.append(invalidJSON) - let result = [TestModel].from(decodableJSONArray: jsonArray) + let result: [TestModel]? = .from(decodableJSONArray: jsonArray) XCTAssertNil(result, "JSON array from model array should be nil is any encoding fails.") } @@ -325,7 +325,7 @@ class GlossTests: XCTestCase { func testModelFromJSONRawData() { let data = try! JSONSerialization.data(withJSONObject: testModelsJSON!, options: []) - let model = TestModel(data: data) + let model: TestModel? = .from(decodableData: data) XCTAssertNotNil(model, "Model from Data should not be nil.") } From 709cdf3638f32e885f0add50f8b958fd39a7869d Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:30:30 -0400 Subject: [PATCH 4/7] Remove extra comment --- Sources/Gloss/ExtensionArray.swift | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Sources/Gloss/ExtensionArray.swift b/Sources/Gloss/ExtensionArray.swift index 13803e4..2630dba 100644 --- a/Sources/Gloss/ExtensionArray.swift +++ b/Sources/Gloss/ExtensionArray.swift @@ -137,15 +137,6 @@ public extension Array where Element: JSONDecodable { return models } - - /** - Returns array of new objects created from provided JSON array. - If any decodings fail, nil is returned. - - - parameter jsonArray: Array of JSON representations of objects. - - - returns: Array of objects created from JSON. - */ /** Initializes array of model objects from provided data. From 4027004053b959b1d708f3f797fc924393a7ddb8 Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:30:39 -0400 Subject: [PATCH 5/7] Update PR template --- PULL_REQUEST_TEMPLATE.md | 1 - 1 file changed, 1 deletion(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index b49fecf..19e903e 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -3,7 +3,6 @@ Gloss is deprecated in favor of Swift's `Codable`. Only Issues and Pull Requests ### Acknowledgment of Deprecation * [ ] I acknowledge awareness that Gloss is deprecated in favor of Codable -* [ ] Are you using Gloss 3.2.x or higher? ### Description From b84297b0521da0ccaef9b349c9800125ffdddd61 Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:30:47 -0400 Subject: [PATCH 6/7] Update podspec --- Gloss.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gloss.podspec b/Gloss.podspec index e095e58..5f6588d 100644 --- a/Gloss.podspec +++ b/Gloss.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Gloss" - s.version = "3.2.0" + s.version = "3.2.1" s.summary = "[Deprecated] A shiny JSON parsing library in Swift" s.description = "[Deprecated] A shiny JSON parsing library in Swift." s.homepage = "https://github.com/hkellaway/Gloss" From e963af8a4fadade9ba583f5883954aee3cdff4ab Mon Sep 17 00:00:00 2001 From: Harlan Kellaway Date: Tue, 1 Sep 2020 09:30:55 -0400 Subject: [PATCH 7/7] Update CHANGELOG --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 952812d..d31e1e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. `Gloss` adheres to [Semantic Versioning](http://semver.org/). -- `3.0.X` Releases - [3.0.0](#300) | [3.1.0](#310) | [3.1.1](#311) | [3.2.0](#320) +- `3.0.X` Releases - [3.0.0](#300) | [3.1.0](#310) | [3.1.1](#311) | [3.2.0](#320) | [3.2.1](#321) - `2.1.x` Releases - [2.1.0](#210) | [2.1.1](#211) - `2.0.x` Releases - [2.0.0-beta.1](#200-beta1) | [2.0.0-beta.2](#200-beta2) | [2.0.0](#200) | [2.0.1](#201) - `1.2.x` Releases - [1.2.0](#120) | [1.2.1](#121) | [1.2.2](#122) | [1.2.3](#123) | [1.2.4](#124) @@ -17,6 +17,14 @@ All notable changes to this project will be documented in this file. - `0.2.x` Releases - [0.2.0](#020) - `0.1.x` Releases - [0.1.0](#010) +--- +## [3.2.1](https://github.com/hkellaway/Gloss/releases/tag/3.2.1) +Released on 2020-09-01. + +#### Fixed +- Missing ability to create JSON Arrays from Data [[PR #365](https://github.com/hkellaway/Gloss/pull/365)] +- Not using passed parameter [[PR #361](https://github.com/hkellaway/Gloss/pull/365)] + --- ## [3.2.0](https://github.com/hkellaway/Gloss/releases/tag/3.2.0) Released on 2020-08-30.