Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/g-mark/nullcodable
Property wrapper that encodes `nil` optional values as `null` when encoded using `JSONEncoder`
https://github.com/g-mark/nullcodable
Last synced: 2 months ago
JSON representation
Property wrapper that encodes `nil` optional values as `null` when encoded using `JSONEncoder`
- Host: GitHub
- URL: https://github.com/g-mark/nullcodable
- Owner: g-mark
- License: apache-2.0
- Created: 2020-06-11T11:42:46.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T12:05:28.000Z (10 months ago)
- Last Synced: 2024-04-26T18:45:52.874Z (9 months ago)
- Language: Swift
- Size: 10.7 KB
- Stars: 51
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @NullCodable
`@NullCodable` is a property wrapper that encodes `nil` optional values as `null` when encoded using `JSONEncoder`.
On its own, `JSONEncoder` will omit optional properties that are `nil` - meaning that this:
```swift
struct Test: Codable {
var name: String? = nil
}
```
will be encoded as: `{}`.If for some reason, you would like optional properties that are nil
to be encoded in JSON as `null`, then marking those properties as `@NullCodable`
will do so.For example, adding `@NullCodable` like this:
```swift
struct Test: Codable {
@NullCodable var name: String? = nil
}
```
will encode as: `{\"name\": null}`.