MetaSerialization

2.3.0

Serialize any Codable Swift type with one extra line of code
cherrywoods/swift-meta-serialization

What's New

Version 2.3

2024-08-30T14:51:01Z

New Features

  • Maintain the insertion order in keyed containers using the new OrderedKeyedContainer class. PrimitivesEnumTranslator now has a maintainOrder initializer keyword that allows you to use this new container. Note that you have to will then receive OrderedKeyedContainer instances instead of Dictionaries in your encoding code. The container is not used by default to maintain backwards compatibility.

Improvements

  • Run tests on Linux.
  • Fewer typos in the documentation.
  • Hopefully an overall more straight-forward documentation.

MetaSerialization

Carthage compatible

MetaSerialization is a framework to simplify the creation of new serialisation libraries for the Swift standard library environment (Encodable, Decodable, Codable, etc.). Do you already have code to convert a (nested) Dictionary or Array of basic types (String, Int, etc) to your end format (for example, JSON, YAML, XML)? Then add support for arbitrary Codable Swift types using just one line:

let serialization = SimpleSerialization<YOUR FORMAT>(encodeFromMeta: YOUR ENCODING FUNCTION, decodeToMeta: YOUR DECONDING FUNCTION)
serialization.encode(anythingCodable)  // done :)

A more complete example is contained in the BasicUsage playground. At it's core, MetaSerialization provides a Encoder and Decoder implementation with several supporting objects moving around it. The central idea is that these implementations convert Swift objects into a meta format (intermediate format). In the example above, this is the nested Dictionary/Array. Every part of the process is implemented to be highly customizable, so that MetaSerialization can also support more advanced use cases (for example, different meta formats, formats with anchors).

You can find more examples in the this examples folder (used for unit tests). Additionally, https://github.com/cherrywoods/meta-serialization-examples provides two extended examples.

Installation

Install MetaSerialization using one of these dependency managers:

Documentation

Please refer to the docstrings embedded in the source code for API docs. Help for installing MetaSerialization and getting started using the framework is available here: https://github.com/cherrywoods/swift-meta-serialization/tree/master/docs/Guides. These documents will probably leave you with some unanswered questions. Please ask them on the discussions page: https://github.com/cherrywoods/swift-meta-serialization/discussions

Issues and pull requests are also very welcome! Just make sure you stick to the Code of Conduct.

Limitations

MetaSerialization can only do it's work properly, if you do not use the function encode(to: ) of the Encodable protocol directly in your implementation of it. Use the encode methods of the (Un)Keyed/SingleValueEncodingContainers instead.

Maintainance Status

I am maintaining this project as a hobby aside my full-time job. I will answer to questions/issues/pull requests within one or two days, but resolving issues may take more time.

Versions

Swift version

Swift Version MetaSerialization Version
5.2 2.3
5.1 2.2
4.2 2.1.0
4.1.2 2.0.1
4.1 2.0
4.0 1.0

2.3

Version 2.3 adds an optional feature to maintain the insertion order in keyed containers, fixes the tests, and updates the documentation.

2.0

Version 2 added a bunch of features, separated encoding and decoding where necessary and provided better overriding options for Meta(De|En)coder. However, this resulted in a more closed environment, where not everything is overridable, as it as in version 1.

1.0

Version 1 was very similar to Foundations JSONEncoder implementation. Almost everything way declared open.

Licensing

This framework is licensed at the Apache Version 2.0 License, (nearly) the same license swift is licensed at.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Mon May 12 2025 12:39:28 GMT-0900 (Hawaii-Aleutian Daylight Time)