Swiftpack.co -  antofic/GraphCodable as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
antofic/GraphCodable
GraphCodable is an experimental Swift encode/decode package that does not treat reference types as second-class citizens.
.package(url: "https://github.com/antofic/GraphCodable.git", from: "0.2.2")

GraphCodable

Firstly, I apologize for my poor English. I hope the content remains understandable.

GraphCodable is an experimental Swift encode/decode package (similar to Codable at interface level) that does not treat reference types as second-class citizens.

With version 0.2.0 and later versions, the package has been completely revised. It now relies on _mangledTypeName(...) (when available) and NSStringFromClass(...) to generate the "type name" and on _typeByName(...) and NSClassFromString(...) to retrieve the class type from it. At least on Apple systems this procedure should now be stable.

Thanks to this change it is no longer necessary to register the classes (the repository is gone) or even set the main module name.

All the previous features are maintained, except for the reference type replacement system which is no longer available at this point in the redesign phase.

Features

GraphCodable:

  • encodes type information (for reference types);
  • supports reference inheritance;
  • never duplicates the same object (as defined by ObjectIdentifier);
  • in other words, it preserves the structure and the types of your model unaltered during encoding and decoding;
  • is fully type checked at compile time (*);
  • supports keyed and unkeyed coding, also usable simultaneously;
  • supports conditional encoding;
  • implements an userInfo dictionary;
  • implements a reference type version system;
  • implements a reference type substitution system during decode NOT AIVAILABLE NOW - I'm redesigning it;

Check code examples in the User Guide. Check the tests section, too.

(*) Fully type checking at compile time is mutually exclusive with the ability to encode/decode heterogeneous collections (i.e. [Any]) containing 'codable' elements. I chose to support the first feature while giving up the second.

Other information

Most Swift Standard Library and Foundation types are supported now. The list is here.

GraphCodable can encode and decode any 'ARC compatible' Swift object graph, regardless of how complex it is, reconstructing its original structure with its original types without duplicating objects. Only weak variables used in the object graph in order to avoid strong memory cycles in ARC require special treatment during initialization within the init (from: GDecoder) method. One limitation exists: you cannot put a weak variable that must employ the above mechanism inside a value type but only inside a reference type.

This table summarizes the methods to be used depending on the type of variable to be encoded and decoded.

GraphCodable does not use a public format for the data (such as JSON or others) but stores the data in a private binary format defined by the package itself. Having to preserve information about the types, GraphCodable creates larger data in bytes than those generated by Codable encoders. Despite this, the time required for encoding and decoding is generally comparable if not faster.

GraphCodable is written entirely in Swift. The use of 'unsafe' methods is limited to a handful of functions related to reading and writing data in binary format (see BinaryIO). Everything else is 'safe'.

Be aware

For now, the data format may be subject to future changes.

Other documents

Simple interface comparison to Swift Codable

In GraphCodable:

  • GCodable have the same roles as Codable
  • GEncoder, GDecoder have the same roles as Encoder, Decoder
  • GraphEncoder has the same role as JSONEncoder, PropertyListEncoder
  • GraphDecoder has the same role as JSONDecoder, PropertyListDecoder

GraphCodable does not use containers.

Supported technologies

  • Swift 5.3

Changelog

Check it here.

License

GraphCodable is released under the MIT license. See LICENSE for more details.

GitHub

link
Stars: 3
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

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