title: Polymorphism and Dynamic Types sidebar_position: 7 id: polymorphism license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Fory Swift supports dynamic serialization for Any, AnyObject, and any Serializer.
let fory = Fory() let dynamic: Any = Int32(7) let data = try fory.serialize(dynamic) let decoded: Any = try fory.deserialize(data)
Equivalent overloads exist for:
AnyObjectany SerializerAnyHashable[Any][String: Any][Int32: Any][AnyHashable: Any]@ForyObject Types@ForyObject struct DynamicHolder { var value: Any = ForyAnyNullValue() var list: [Any] = [] var byName: [String: Any] = [:] var byId: [Int32: Any] = [:] var byDynamicKey: [AnyHashable: Any] = [:] }
If dynamic values contain user-defined runtime types, register those concrete types.
@ForyObject struct Address { var street: String = "" var zip: Int32 = 0 } let fory = Fory() fory.register(Address.self, id: 100)
Any null representation: ForyAnyNullValueAnyObject null representation: NSNullAnyHashable keys must wrap runtime values that are both Hashable and supported by Fory dynamic serialization