Skip to content

Commit b7613b5

Browse files
committed
present null for nil property
1 parent bbd582c commit b7613b5

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,19 @@ class BasicTypes: HandyJSON {
683683
}
684684
```
685685
686+
## Q: How to present a `null` value of `nil` property when serialize to JSON?
686687
688+
In `mapping(mapper: HelpingMapper)`, use `mapper.nullPresent` to apply null present for property
689+
690+
```swift
691+
class Animal: HandyJSON {
692+
var name: String?
693+
var owner: String?
694+
func mapping(mapper: HelpingMapper) {
695+
mapper.nullPresent(self.owner)
696+
}
697+
}
698+
```
687699
688700
# License
689701

Source/ExtendCustomModelType.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ extension _ExtendCustomModelType {
235235
for (key, property) in properties {
236236
var realKey = key
237237
var realValue = property.0
238-
238+
var useNullValueIfNeeded = false
239239
if let info = property.1 {
240240
if info.bridged, let _value = (instance as! NSObject).value(forKey: key) {
241241
realValue = _value
@@ -245,6 +245,10 @@ extension _ExtendCustomModelType {
245245
continue
246246
}
247247

248+
if mapper.propertyPresentNullValue(key: Int(bitPattern: info.address)) {
249+
useNullValueIfNeeded = true
250+
}
251+
248252
if let mappingHandler = mapper.getMappingHandler(key: Int(bitPattern: info.address)) {
249253
// if specific key is set, replace the label
250254
if let mappingPaths = mappingHandler.mappingPaths, mappingPaths.count > 0 {
@@ -266,6 +270,10 @@ extension _ExtendCustomModelType {
266270
dict[realKey] = result
267271
continue
268272
}
273+
else if useNullValueIfNeeded {
274+
dict[realKey] = NSNull()
275+
continue
276+
}
269277
}
270278

271279
InternalLogger.logDebug("The value for key: \(key) is not transformable type")

Source/HelpingMapper.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class HelpingMapper {
8585

8686
private var mappingHandlers = [Int: MappingPropertyHandler]()
8787
private var excludeProperties = [Int]()
88+
private var nullPresentProperties = [Int]()
8889

8990
internal func getMappingHandler(key: Int) -> MappingPropertyHandler? {
9091
return self.mappingHandlers[key]
@@ -94,6 +95,10 @@ public class HelpingMapper {
9495
return self.excludeProperties.contains(key)
9596
}
9697

98+
internal func propertyPresentNullValue(key: Int) -> Bool {
99+
return self.nullPresentProperties.contains(key)
100+
}
101+
97102
public func specify<T>(property: inout T, name: String) {
98103
self.specify(property: &property, name: name, converter: nil)
99104
}
@@ -128,6 +133,14 @@ public class HelpingMapper {
128133
self._exclude(property: &property)
129134
}
130135

136+
public func nullPresent<T>(property: inout T) {
137+
let pointer = withUnsafePointer(to: &property, { return $0 })
138+
let iPointer = Int(bitPattern: pointer)
139+
if !self.excludeProperties.contains(iPointer) && !nullPresentProperties.contains(iPointer) {
140+
nullPresentProperties.append(iPointer)
141+
}
142+
}
143+
131144
fileprivate func addCustomMapping(key: Int, mappingInfo: MappingPropertyHandler) {
132145
self.mappingHandlers[key] = mappingInfo
133146
}

Tests/HandyJSONTests/OtherFeaturesTest.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,19 @@ class StructObjectTest: XCTestCase {
404404
let a = A.deserialize(from: jsonString)!
405405
XCTAssertEqual(a.upperName, "HANDYJSON")
406406
}
407+
408+
func testNullPresentValue() {
409+
class A: HandyJSON {
410+
var name: String?
411+
required init() {}
412+
413+
func mapping(mapper: HelpingMapper) {
414+
mapper.nullPresent(property: &self.name)
415+
}
416+
}
417+
418+
let a = A()
419+
let jsonString = a.toJSONString()!
420+
XCTAssertEqual(jsonString, "{\"name\":null}")
421+
}
407422
}

0 commit comments

Comments
 (0)