# Getting type from mangled name (e.g. "SS" -\> Any.Type)

**URL:** <https://forums.swift.org/t/getting-type-from-mangled-name-e-g-ss-any-type/65660>\
**Category:** Using Swift\
**Tags:** swift, type-casting, swiftdata\
**Created:** [June 19, 2023, 10:14am UTC](https://forums.swift.org/t/getting-type-from-mangled-name-e-g-ss-any-type/65660 "2023-06-19T10:14:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shapsuk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/shapsuk/32/19726_2.png) [@shapsuk](https://forums.swift.org/u/shapsuk)\
**Post date:** [June 19, 2023, 10:14am UTC](https://forums.swift.org/t/getting-type-from-mangled-name-e-g-ss-any-type/65660/1 "2023-06-19T10:14:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shapsuk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/shapsuk/32/19726_2.png) [@shapsuk](https://forums.swift.org/u/shapsuk)\
**Post date:** [June 19, 2023, 10:28am UTC](https://forums.swift.org/t/getting-type-from-mangled-name-e-g-ss-any-type/65660/2 "2023-06-19T10:28:25Z")

</div>

Just as I posted this, I found a lovely bit of API:

`_mangledTypeName`  
`_typeByName`

These resolve the issue entirely, however since they're underscored, are they safe to use and will they pass AppStore review?

For context, I'm back porting libraries in my [SwiftBackports](https://github/shaps80/SwiftBackports) and [SwiftUIBackports](https://github/shaps80/SwiftUIBackports) so users would only be using this in "previous" iOS versions, and so forward compatibility isn't necessary.
