Skip to content

Commit

Permalink
Add CocoaPods support
Browse files Browse the repository at this point in the history
  • Loading branch information
SAGESSE-CN committed Mar 2, 2022
1 parent 2ee5a24 commit eecff64
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
27 changes: 27 additions & 0 deletions JSONDecoderNS.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Be sure to run `pod lib lint JSONDecoderEx.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'JSONDecoderNS'
s.version = '1.0.0'
s.summary = 'A enhanced JSON decoder.'
s.homepage = 'https://github.com/SAGESSE-CN/JSONDecoderEx'
s.author = { 'SAGESSE' => 'gdmmyzc@163.com' }
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.source = { :git => 'https://github.com/SAGESSE-CN/JSONDecoderEx.git', :tag => s.version.to_s }
s.source_files = 'Sources/**/*'

s.swift_versions = '5.0'

s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.11'

#s.test_spec 'Tests' do |ts|
# ts.source_files = 'Tests/**/*'
#end
end
31 changes: 29 additions & 2 deletions Tests/JSONDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,34 @@ class JSONDecoderTests: XCTestCase {
XCTAssertEqual(try def.decode(S.self, from: ["r":[:]]).r.i, 0)
}

func testKeyPath() {
struct R: Codable {
let name: String
let name2: String
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: JSONDecoderEx.JSONKey.self)
let p = try container.nestedContainer(keyedBy: JSONDecoderEx.JSONKey.self, forKey: "p")
name = try p.decode(String.self, forKey: "name") // p.name
let pp = try p.nestedContainer(keyedBy: JSONDecoderEx.JSONKey.self, forKey: "pp")
name2 = try pp.decode(String.self, forKey: "name") // p.pp.name
}
}
XCTAssertEqual(try def.decode(R.self, from: "{\"p\":{\"name\":\"swift\"}}".data(using: .utf8)!).name, "swift")
XCTAssertEqual(try def.decode(R.self, from: "{\"p\":{\"name\":\"swift\",\"pp\":{\"name\":5.2}}}".data(using: .utf8)!).name2, "5.2")
XCTAssertEqual(try def.decode(R.self, from: "{}".data(using: .utf8)!).name, "")
XCTAssertEqual(try def.decode(R.self, from: "{\"p\":null}".data(using: .utf8)!).name, "")
XCTAssertThrowsError(try def.decode(R.self, from: "{\"p\":[]}".data(using: .utf8)!))
XCTAssertEqual(try def.decode(R.self, from: "{\"p\":{}}".data(using: .utf8)!).name, "")
XCTAssertThrowsError(try def.decode(R.self, from: "{\"p\":\"123\"}".data(using: .utf8)!))
}

func testDic() {
struct R: Codable {
let name: String
}
XCTAssertEqual(try def.decode(R.self, from: "{\"p\":\"123\"}".data(using: .utf8)!).name, "")
}

func testKeyDecodingStrategy() {
struct R: Decodable {
let k: String
Expand Down Expand Up @@ -391,7 +419,7 @@ class JSONDecoderTests: XCTestCase {
XCTAssertEqual(try def.decode(Date.self, from: 1000), Date(timeIntervalSince1970: 1000))
def.dateDecodingStrategy = .millisecondsSince1970
XCTAssertEqual(try def.decode(Date.self, from: 1000), Date(timeIntervalSince1970: 1))
if #available(iOS 10.0, *) {
if #available(macOS 10.12, iOS 10.0, watchOS 8.0, *) {
def.dateDecodingStrategy = .iso8601
XCTAssertThrowsError(try def.decode(Date.self, from: 1000))
XCTAssertThrowsError(try def.decode(Date.self, from: "1000"))
Expand Down Expand Up @@ -570,6 +598,5 @@ class JSONDecoderTests: XCTestCase {
//print(error)
}
}

}

0 comments on commit eecff64

Please sign in to comment.