Swiftpack.co - Package - rinold/XTendedCodable


CI Status Swift Swift Package Manager License

An easy way to handle JSON objects x-^ extensions in Codable structures, e.g. the OpenAPI Specification Extensions. All extension fields are aggregated in extensions storage field during decoding and are encoded at the same level as base object field.


To support extensions decoding and encoding for your Codable structs, just conform to XTendedCodable protocol, e.g.:

import XTendedCodable

struct ExampleCodable: XTendedCodable {
    let name: String
    let date: Date

    // Default provided [String: AnyCodable]  storage
    var extensions: XTension?

And that's all! :)

It can now decode the x-^ properties from JSON:

    "name" : "Extended Example Codable",
    "timestamp": "2018-07-12T09:13:42Z",
    "x-bool" : false,
    "x-custom" : {
        "key" : -1,
        "description": "something"
    "x-double" : 3.14,
    "x-string" : "Hello!",
    "x-nil": null,
    "x-int" : 10,
    "x-int-array" : [1, 2, 3],
    "x-string?-array" : ["a", null, "c"]

For example:

// Decode the JSON above
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let exampleCodable = try decoder.decode(ExampleCodable.self, from: jsonData)

if let ext = exampleCodable.extensions {
    let xOptionalStringArray = ext["x-string?-array"]?.to([String?].self)
    for string in xOptionalStringArray.compactMap { $0 } {
      print (string)

// Output:

More examples of Decoding and Encoding could be found in tests sources.


You can add properties to your ExampleCodable later in extensions or just for convenience if you know what you will be working with, e.g.:

extension TestCodable {
    var xProperty: Int? {
        get { return extensions?["x-some-property"]?.int }
        set { extensions?["x-some-property"] = .init(from: newValue) }

Note: non-prefixed with "x-" keys will not be encoded. Possibly, you can use this to hide some properties from decoding/encoding - without modifying CodingKeys, decode and encode methods.


XTendedCodable provides Extendable generic protocol so you can set your own Storage type (with needed conformance limitations) and per-model fields filtering rules.

Handling custom types

Any custom type conforming Codable could be handled by just another AnyCodable bycicle, if no, I possibly messed up somewhere :)



Swift Package Manager

To install it add following package to needed Packages.swift dependencies:

.package(url: "https://github.com/rinold/XTendedCodable.git", from: "0.2.1")


We use SemVer for versioning. For the versions available, see the tags on this repository.


  • rinold - Mikhail Churbanov, mihail.churbanov@gmail.com


ProxyResolver is available under the MIT license. See the LICENSE file for more info.


Stars: 0
Help us keep the lights on


Used By



0.2.1 - Jul 18, 2018

Added support for Array and Optional decoding

0.2.0 - Jul 12, 2018

  • Renamed main protocols to XTendedCodable and XTension (storage)
  • Added encoding pre-filtering (x-^ for XTension)
  • Some bug-fixing

0.1.0 - Jul 12, 2018