Connect with us

Code

MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming

Supercharge Swift‘s Codable implementations with macros.

Overview

MetaCodable framework exposes custom macros which can be used to generate dynamic Codable implementations. The core of the framework is Codable() macro which generates the implementation aided by data provided with using other macros.

MetaCodable aims to supercharge your Codable implementations by providing these inbox features:

  • Allows custom CodingKey value declaration per variable, instead of requiring you to write all the CodingKey values with CodedAt(_:) passing single argument.
  • Allows to create flattened model for nested CodingKey values with CodedAt(_:) and CodedIn(_:).
  • Allows to create composition of multiple Codable types with CodedAt(_:) passing no arguments.
  • Allows to provide default value in case of decoding failures with Default(_:).
  • Allows to create custom decoding/encoding strategies with HelperCoder and using them with CodedBy(_:). i.e. LossySequenceCoder etc.
  • Allows to ignore specific properties from decoding/encoding with IgnoreCoding()IgnoreDecoding() and @IgnoreEncoding().
  • Allows to use camel-case names for variables according to Swift API Design Guidelines, while enabling a type to work with different case style keys with CodingKeys(_:).
  • Allows to ignore all initialized properties of a type from decoding/encoding with IgnoreCodingInitialized() unless explicitly asked to decode/encode by attaching any coding attributes, i.e. CodedIn(_:)CodedAt(_:)CodedBy(_:)Default(_:) etc.

Requirements

Platform Minimum Swift Version Installation Status
iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ 5.9 Swift Package Manager Fully Tested
Linux 5.9 Swift Package Manager Fully Tested
Windows 5.9 Swift Package Manager Fully Tested

Usage

MetaCodable allows to get rid of boiler plate that was often needed in some typical Codable implementations with features like:

Custom `CodingKey` value declaration per variable, instead of requiring you to write for all fields.
Create flattened model for nested `CodingKey` values.
Provide default value in case of decoding failures.

See the full documentation for API details and advanced use cases.

MetaCodable on GitHub: https://github.com/SwiftyLab/MetaCodable
Platform: iOS
⭐️: 180
Advertisement

Trending