Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Present null for property with nil value #291

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,19 @@ class BasicTypes: HandyJSON {
}
```

## Q: How to present a `null` value of `nil` property when serialize to JSON?

In `mapping(mapper: HelpingMapper)`, use `mapper.nullPresent` to apply null present for property

```swift
class Animal: HandyJSON {
var name: String?
var owner: String?
func mapping(mapper: HelpingMapper) {
mapper.nullPresent(self.owner)
}
}
```

# License

Expand Down
10 changes: 9 additions & 1 deletion Source/ExtendCustomModelType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension _ExtendCustomModelType {
for (key, property) in properties {
var realKey = key
var realValue = property.0

var useNullValueIfNeeded = false
if let info = property.1 {
if info.bridged, let _value = (instance as! NSObject).value(forKey: key) {
realValue = _value
Expand All @@ -245,6 +245,10 @@ extension _ExtendCustomModelType {
continue
}

if mapper.propertyPresentNullValue(key: Int(bitPattern: info.address)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we will use like this ->
useNullValueIfNeeded = mapper.propertyPresentNullValue(key: Int(bitPattern: info.address))

just a proposal

useNullValueIfNeeded = true
}

if let mappingHandler = mapper.getMappingHandler(key: Int(bitPattern: info.address)) {
// if specific key is set, replace the label
if let mappingPaths = mappingHandler.mappingPaths, mappingPaths.count > 0 {
Expand All @@ -266,6 +270,10 @@ extension _ExtendCustomModelType {
dict[realKey] = result
continue
}
else if useNullValueIfNeeded {
dict[realKey] = NSNull()
continue
}
}

InternalLogger.logDebug("The value for key: \(key) is not transformable type")
Expand Down
13 changes: 13 additions & 0 deletions Source/HelpingMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class HelpingMapper {

private var mappingHandlers = [Int: MappingPropertyHandler]()
private var excludeProperties = [Int]()
private var nullPresentProperties = [Int]()

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

internal func propertyPresentNullValue(key: Int) -> Bool {
return self.nullPresentProperties.contains(key)
}

public func specify<T>(property: inout T, name: String) {
self.specify(property: &property, name: name, converter: nil)
}
Expand Down Expand Up @@ -128,6 +133,14 @@ public class HelpingMapper {
self._exclude(property: &property)
}

public func nullPresent<T>(property: inout T) {
let pointer = withUnsafePointer(to: &property, { return $0 })
let iPointer = Int(bitPattern: pointer)
if !self.excludeProperties.contains(iPointer) && !nullPresentProperties.contains(iPointer) {
nullPresentProperties.append(iPointer)
}
}

fileprivate func addCustomMapping(key: Int, mappingInfo: MappingPropertyHandler) {
self.mappingHandlers[key] = mappingInfo
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/HandyJSONTests/OtherFeaturesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,19 @@ class StructObjectTest: XCTestCase {
let a = A.deserialize(from: jsonString)!
XCTAssertEqual(a.upperName, "HANDYJSON")
}

func testNullPresentValue() {
class A: HandyJSON {
var name: String?
required init() {}

func mapping(mapper: HelpingMapper) {
mapper.nullPresent(property: &self.name)
}
}

let a = A()
let jsonString = a.toJSONString()!
XCTAssertEqual(jsonString, "{\"name\":null}")
}
}