Skip to content

Commit 2850ef3

Browse files
committed
add test case for nsnull
1 parent 03a52e5 commit 2850ef3

File tree

7 files changed

+73
-16
lines changed

7 files changed

+73
-16
lines changed

KakaJSON/Convert/Convertible.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,14 @@ extension Convertible {
217217
}
218218

219219
mutating func kj_convert(from json: [String: Any]) {
220-
guard let mt = Metadata.type(self) as? ModelType,
221-
let properties = mt.properties else { return }
220+
guard let mt = Metadata.type(self) as? ModelType else {
221+
Logger.warnning("Not a class or struct instance.")
222+
return
223+
}
224+
guard let properties = mt.properties else {
225+
Logger.warnning("Don't have any property.")
226+
return
227+
}
222228

223229
// get data address
224230
let model = _ptr()
@@ -299,9 +305,14 @@ extension Convertible {
299305
// MARK: - Model -> JSON
300306
extension Convertible {
301307
func kj_JSONObject() -> [String: Any]? {
302-
guard let mt = Metadata.type(self) as? ModelType,
303-
let properties = mt.properties
304-
else { return nil }
308+
guard let mt = Metadata.type(self) as? ModelType else {
309+
Logger.warnning("Not a class or struct instance.")
310+
return nil
311+
}
312+
guard let properties = mt.properties else {
313+
Logger.warnning("Don't have any property.")
314+
return nil
315+
}
305316

306317
kj_willConvertToJSON()
307318

KakaJSON/Convert/Values.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import CoreGraphics
1414
public struct Values {
1515
static func value(_ val: Any?, _ type: Any.Type) -> Any? {
1616
guard let v = val.kj_value else { return nil }
17+
if v is NSNull { return v }
18+
1719
if Swift.type(of: v) == type { return v }
1820

1921
switch type {
@@ -35,6 +37,7 @@ public struct Values {
3537

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

3942
switch v {
4043
case let num as NumberValue: return _JSONValue(from: num)

KakaJSON/DevGuidline.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
// Copyright © 2019 MJ Lee. All rights reserved.
77
//
88

9+
/*
10+
Metadata Reference:
11+
0. https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst
12+
1. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataKind.def
13+
2. https://github.com/apple/swift/blob/master/include/swift/ABI/Metadata.h
14+
3. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataValues.h
15+
4. https://github.com/apple/swift/blob/master/utils/dtrace/runtime.d
16+
5. https://github.com/apple/swift/blob/master/include/swift/Reflection/Records.h
17+
*/
18+
919
1. 提交代码之前请务必先保证在真机模拟器上通过所有的测试用例Debug+Release模式
1020

1121
2. 命名规范

KakaJSON/Global/JSON.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
// Copyright © 2019 MJ Lee. All rights reserved.
77
//
88

9-
/*
10-
Reference:
11-
0. https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst
12-
1. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataKind.def
13-
2. https://github.com/apple/swift/blob/master/include/swift/ABI/Metadata.h
14-
3. https://github.com/apple/swift/blob/master/include/swift/ABI/MetadataValues.h
15-
4. https://github.com/apple/swift/blob/master/utils/dtrace/runtime.d
16-
5. https://github.com/apple/swift/blob/master/include/swift/Reflection/Records.h
17-
*/
18-
199
// MARK: Model -> JSON
2010
public func JSONObject<M: Convertible>(from model: M) -> [String: Any]? {
2111
return model.kj_JSONObject()

KakaJSONDemo.xcodeproj/xcshareddata/xcschemes/KakaJSONDemo.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</AdditionalOptions>
5353
</TestAction>
5454
<LaunchAction
55-
buildConfiguration = "Debug"
55+
buildConfiguration = "Release"
5656
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5757
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
5858
launchStyle = "0"

KakaJSONTests/JSON_To_Model/JTM_01_Basic.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ class JTM_01_Basic: XCTestCase {
8080
XCTAssert(cat?.weight == weight)
8181
}
8282

83+
// MARK: - NSNull
84+
func testNSNull() {
85+
struct Cat: Convertible {
86+
var weight: Double = 0.0
87+
var name: String = "xx"
88+
var data: NSNull?
89+
}
90+
91+
let json: [String: Any] = [
92+
"name": NSNull(),
93+
"weight": 6.6,
94+
"data": NSNull()
95+
]
96+
97+
let cat = json.kj.model(Cat.self)
98+
// convert failed, keep default value
99+
XCTAssert(cat?.name == "xx")
100+
XCTAssert(cat?.weight == 6.6)
101+
XCTAssert(cat?.data == NSNull())
102+
}
103+
83104
// MARK: - let
84105
func testLet() {
85106
struct Cat: Convertible {

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Or you can login Xcode with your GitHub account. just search **KakaJSON**.
4141
- [Class Type](#class-type)
4242
- [Inheritance](#inheritance)
4343
- [let](#let)
44+
- [NSNull](#nsnull)
4445
- [JSONString](#jsonstring)
4546
- [JSONData](#jsondata)
4647
- [Nested Model 1](#nested-model-1)
@@ -352,6 +353,27 @@ let json = ...
352353
let cat = json.kj.model(Cat.self)
353354
```
354355

356+
### NSNull
357+
```swift
358+
struct Cat: Convertible {
359+
var weight: Double = 0.0
360+
var name: String = "xx"
361+
var data: NSNull?
362+
}
363+
364+
let json: [String: Any] = [
365+
"name": NSNull(),
366+
"weight": 6.6,
367+
"data": NSNull()
368+
]
369+
370+
let cat = json.kj.model(Cat.self)
371+
// convert failed, keep default value
372+
XCTAssert(cat?.name == "xx")
373+
XCTAssert(cat?.weight == 6.6)
374+
XCTAssert(cat?.data == NSNull())
375+
```
376+
355377
### JSONString
356378
```swift
357379
// jsonString can alse be NSString, NSMutableString

0 commit comments

Comments
 (0)