I have an interesting problem where I need to store an Any.Type as a string in an encoded JSON file.
Later, I need to be able to decode this back into the original type, of course storing is as Any.Type, but then I can later cast this to the appropriate type.
I had all but given up on a solution, until I noticed this is very similarly implemented somehow in the new SwiftData framework.
Attribute
for example has a property on it called valueTypeName
which stores the mangled type name when encoded to JSON. (e.g. Swift.String becomes "SS")
If I then decode the JSON back into Attribute
I see the original type can now be type-cast correctly.
I'd love any insights into how this could be achieved? Any support/ideas on this appreciated.
FYI, tried a pure custom type with no conformances and this worked also, so its not limited to SwiftData attribute types AFAICT.