Skip to content

Commit

Permalink
add test case for nsnull
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMJLee committed Aug 23, 2019
1 parent 03a52e5 commit 2850ef3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 16 deletions.
21 changes: 16 additions & 5 deletions KakaJSON/Convert/Convertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,14 @@ extension Convertible {
}

mutating func kj_convert(from json: [String: Any]) {
guard let mt = Metadata.type(self) as? ModelType,
let properties = mt.properties else { return }
guard let mt = Metadata.type(self) as? ModelType else {
Logger.warnning("Not a class or struct instance.")
return
}
guard let properties = mt.properties else {
Logger.warnning("Don't have any property.")
return
}

// get data address
let model = _ptr()
Expand Down Expand Up @@ -299,9 +305,14 @@ extension Convertible {
// MARK: - Model -> JSON
extension Convertible {
func kj_JSONObject() -> [String: Any]? {
guard let mt = Metadata.type(self) as? ModelType,
let properties = mt.properties
else { return nil }
guard let mt = Metadata.type(self) as? ModelType else {
Logger.warnning("Not a class or struct instance.")
return nil
}
guard let properties = mt.properties else {
Logger.warnning("Don't have any property.")
return nil
}

kj_willConvertToJSON()

Expand Down
3 changes: 3 additions & 0 deletions KakaJSON/Convert/Values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import CoreGraphics
public struct Values {
static func value(_ val: Any?, _ type: Any.Type) -> Any? {
guard let v = val.kj_value else { return nil }
if v is NSNull { return v }

if Swift.type(of: v) == type { return v }

switch type {
Expand All @@ -35,6 +37,7 @@ public struct Values {

static func JSONValue(_ value: Any?) -> Any? {
guard let v = value.kj_value else { return nil }
if v is NSNull { return nil }

switch v {
case let num as NumberValue: return _JSONValue(from: num)
Expand Down
10 changes: 10 additions & 0 deletions KakaJSON/DevGuidline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
// Copyright © 2019 MJ Lee. All rights reserved.
//

/*
Metadata Reference:
0. https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst
1. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataKind.def
2. https://github.com/apple/swift/blob/master/include/swift/ABI/Metadata.h
3. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataValues.h
4. https://github.com/apple/swift/blob/master/utils/dtrace/runtime.d
5. https://github.com/apple/swift/blob/master/include/swift/Reflection/Records.h
*/

1. 提交代码之前请务必先保证在真机模拟器上通过所有的测试用例Debug+Release模式

2. 命名规范
Expand Down
10 changes: 0 additions & 10 deletions KakaJSON/Global/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
// Copyright © 2019 MJ Lee. All rights reserved.
//

/*
Reference:
0. https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst
1. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataKind.def
2. https://github.com/apple/swift/blob/master/include/swift/ABI/Metadata.h
3. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataValues.h
4. https://github.com/apple/swift/blob/master/utils/dtrace/runtime.d
5. https://github.com/apple/swift/blob/master/include/swift/Reflection/Records.h
*/

// MARK: Model -> JSON
public func JSONObject<M: Convertible>(from model: M) -> [String: Any]? {
return model.kj_JSONObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
21 changes: 21 additions & 0 deletions KakaJSONTests/JSON_To_Model/JTM_01_Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ class JTM_01_Basic: XCTestCase {
XCTAssert(cat?.weight == weight)
}

// MARK: - NSNull
func testNSNull() {
struct Cat: Convertible {
var weight: Double = 0.0
var name: String = "xx"
var data: NSNull?
}

let json: [String: Any] = [
"name": NSNull(),
"weight": 6.6,
"data": NSNull()
]

let cat = json.kj.model(Cat.self)
// convert failed, keep default value
XCTAssert(cat?.name == "xx")
XCTAssert(cat?.weight == 6.6)
XCTAssert(cat?.data == NSNull())
}

// MARK: - let
func testLet() {
struct Cat: Convertible {
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Or you can login Xcode with your GitHub account. just search **KakaJSON**.
- [Class Type](#class-type)
- [Inheritance](#inheritance)
- [let](#let)
- [NSNull](#nsnull)
- [JSONString](#jsonstring)
- [JSONData](#jsondata)
- [Nested Model 1](#nested-model-1)
Expand Down Expand Up @@ -352,6 +353,27 @@ let json = ...
let cat = json.kj.model(Cat.self)
```

### NSNull
```swift
struct Cat: Convertible {
var weight: Double = 0.0
var name: String = "xx"
var data: NSNull?
}

let json: [String: Any] = [
"name": NSNull(),
"weight": 6.6,
"data": NSNull()
]

let cat = json.kj.model(Cat.self)
// convert failed, keep default value
XCTAssert(cat?.name == "xx")
XCTAssert(cat?.weight == 6.6)
XCTAssert(cat?.data == NSNull())
```

### JSONString
```swift
// jsonString can alse be NSString, NSMutableString
Expand Down

0 comments on commit 2850ef3

Please sign in to comment.