LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

iOS + Godot 4.4 cannot deserialize Lists and Dictionaries

Open MartinLyne opened this issue 10 months ago • 0 comments

Please bear with me, I've tried a lot of things.

When I compile my Godot game LiteDb works fine on desktop and Android, but in iOS it fails deserializing on some properties of my objects.

It seems like this is because of AOT stripping out code it doesn't think is used.

To attempt to get around this I've used a custom BSON mapper and I can see it functioning - however after returning the type LiteDB seems to attempt to instantiate the type anyway, causing an error. Lite DB reports it can't instantiate and maybe there is no empty constructor, but from what I understand it shouldn't be doing that anyway if the mapper returns a value?

If anyone has any ideas they'd be welcome.

BsonMapper.Global.RegisterType<Dictionary<string, int>>(
            serialize: (dict) =>
            {
                GD.Print("Serializing Dictionary<string, int>");
                var doc = new BsonDocument();
                foreach (var kvp in dict) {
                    doc[kvp.Key] = kvp.Value;
                }
                return doc;
            },
            deserialize: (bson) =>
            {
                GD.Print("Deserializing Dictionary<string, int>");
                if (bson.IsDocument) {
                    var dict = new Dictionary<string, int>();
                    foreach (var key in bson.AsDocument.Keys) {
                        dict[key] = bson.AsDocument[key].AsInt32;
                    }
                    return dict;
                }
                GD.PrintErr("Deserialization failed: BSON is not a document.");
                return null;
            }
        );

MartinLyne avatar Mar 03 '25 22:03 MartinLyne