GraphCodable is an experimental Swift encode/decode package that does not treat reference types as second-class citizens.

Related tags

JSON GraphCodable
Overview

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.

Owner
Antonino Ficarra
Antonino Ficarra
✌ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

JSONHelper Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to boolea

Baris Sencan 792 Jun 3, 2021
The better way to deal with JSON data in Swift.

SwiftyJSON SwiftyJSON makes it easy to deal with JSON data in Swift. Platform Build Status *OS Linux Why is the typical JSON handling in Swift NOT goo

SwiftyJSON 21k Nov 27, 2021
Simple JSON Object mapping written in Swift

ObjectMapper ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from J

Tristan Himmelman 8.9k Nov 23, 2021
[Deprecated] The easy to use Swift JSON decoder

⚠️ DEPRECATED Unbox is deprecated in favor of Swift’s built-in Codable API and the Codextended project. All current users are highly encouraged to mig

John Sundell 2k Nov 11, 2021
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

?? Deprecation Notice ?? Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

Harlan Kellaway 1.6k Nov 8, 2021
A JSON deserialization library for Swift

Mapper Mapper is a simple Swift library to convert JSON to strongly typed objects. One advantage to Mapper over some other libraries is you can have i

Lyft 1.2k Oct 27, 2021
A reusable framework for parsing JSON in Swift.

Why Freddy? Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It ha

Big Nerd Ranch 1.1k Oct 3, 2021
Fast JSON parsing for Swift

JASON is a faster JSON deserializer written in Swift. JASON is the best framework we found to manage JSON at Swapcard. This is by far the fastest and

Damien 1k Oct 3, 2021
macOS app to generate Swift 5 code for models from JSON (with Codeable)

SwiftyJSONAccelerator - MacOS app Codeable Model file Generator For Swift 5 Version v2.2 Generate initializer function for classes Application Downloa

Karthikeya Udupa 906 Nov 25, 2021
[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right

Decodable Simple and strict, yet powerful object mapping made possible by Swift 2's error handling. Greatly inspired by Argo, but without a bizillion

Johannes Lund 1.1k Nov 21, 2021
A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux)

Welcome to Genome 3.0. This library seeks to satisfy the following goals: Data Type Agnostic Failure Driven Nested Mapping Collection Mapping Simple a

logan 779 Nov 8, 2021
A type-safe JSON decoding library purely written in Swift

Himotoki Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift. This library is highly inspired by the popular Swift JSON parsin

IKEDA Sho 793 Nov 20, 2021
The easy to use Swift JSON encoder

Unbox | Wrap Wrap is an easy to use Swift JSON encoder. Don't spend hours writing JSON encoding code - just wrap it instead! Using Wrap is as easy as

John Sundell 731 Nov 9, 2021
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable.

Elevate Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Nike Inc. 618 Nov 6, 2021
Hassle-free JSON encoding and decoding in Swift

#JSONCodable Hassle-free JSON encoding and decoding in Swift Installation Simply add the following to your Cartfile and run carthage update: github "m

Matthew Cheok 606 Nov 10, 2021
Convert JSON to Swift objects.

JSONJoy Convert JSON to Swift objects. The Objective-C counterpart can be found here: JSONJoy. Parsing JSON in Swift has be likened to a trip through

Dalton 347 Jan 16, 2021
Swift object serialization

Cereal is a serialization framework built for Swift. Its intended as a substitution for NSCoding to allow advanced Swift features. With NSCoding, you

Weebly 374 Nov 10, 2021
An Alamofire extension which converts JSON response data into swift objects using EVReflection

AlamofireJsonToObjects ?? This is now a subspec of EVReflection and the code is maintained there. ?? You can install it as a subspec like this: use_fr

Edwin Vermeer 160 Jun 3, 2021
Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization.

Jay Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization. Jay conforms to th

Danielle 133 Aug 11, 2021