Tomlyn icon indicating copy to clipboard operation
Tomlyn copied to clipboard

Converting to a custom type

Open Pale-Gray opened this issue 10 months ago • 1 comments

Hello,

I recently picked this up to try to deserialize TOML, however, I am having trouble with deserializing into custom types. I have this current setup, trying to deserialize an array into a Vector3:

TomlModelOptions op = new TomlModelOptions();
op.ConvertToModel = (o, toType) =>
{
    Console.WriteLine(toType);
    if (toType == typeof(DateTime)) return new DateTime();
    if (o.GetType() == typeof(List<>) && toType == typeof(Vector3)) return new Vector3();
    return null;
};
Toml.ToModel<PrimitiveModelData>(File.ReadAllText(tomlFile), options: op);

however, I end up getting an error when i try to deserialize, stating: Invalid list type OpenTK.Mathematics.Vector3. Getting a StandardObjectDynamicAccessor Type: OpenTK.Mathematics.Vector3 instead.

My toml file is simply

[[cubes]]
start = [0, 0, 0]

Note that it does work for DateTime (Console.WriteLine prints), but doesn't work for the custom type. How could I fix this? Thanks.

Pale-Gray avatar Feb 09 '25 08:02 Pale-Gray

ConvertToModel mostly works for e.g. primitive (string, int) <-> custom type, but doesn't work for e.g. TomlArray/TomlTable <-> custom type

xoofx avatar Mar 11 '25 10:03 xoofx