Swiftpack.co - g-mark/NullCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by g-mark.
g-mark/NullCodable 1.1.0
Property wrapper that encodes `nil` optional values as `null` when encoded using `JSONEncoder`
⭐️ 51
🕓 4 weeks ago
.package(url: "https://github.com/g-mark/NullCodable.git", from: "1.1.0")

@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:

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:

  struct Test: Codable {
    @NullCodable var name: String? = nil
  }

will encode as: {\"name\": null}.

GitHub

link
Stars: 51
Last commit: 4 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Version 1.1.0
3 years ago

Adds support for decoding JSON with missing fields.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics