Serializing generic structs

What is the best approach to archive/serialize generic structs?


I cannot use NSCoding (since structs are not NSObjects).

I can translate my structs to some other representation (such as JSON strings),

but I'm struggling to unarchive/deserialize the structs back from the JSON

if the structs are generic.


More precisely, I'm wrestling with the type system to allow me to express

this in an elegant, maintainable way.

The dict is a great way to clean up a 2 page switch statement.


It's a bit of a double edged sword. It might be harder to debug if two types have a collision. Perhaps put some runtime asserts to ensure there aren't any.


The question, I suppose, becomes: When to register?


You will have to ensure that each class has an instance created before any deserilization happens in order to invoke a dispatch_once in the init() for registration.


Either that or put something in at app startup that knows about all the types and calls the register on each class.


This is where objc `-(void)initialize` equivalent would be nice.


Here's the main problem: How to get the value type out of a SerializableStruct? It doesn't have a value instance, as that's in the generic.

Serializing generic structs
 
 
Q