Skip to content

Is there any way to support polymorphic deserialization automatically? #109

@tamashii

Description

@tamashii

Polymorphic Deserialization also knowns Type-Tagged Deserialization, means to deserialize objects with difference types by type field (such as type). for example:

[[db_connections]]
type = "mysql"
host = "localhost"
port = 3306
user_id = "root"
password = "123"
schema = "db_test"

[[db_connections]]
type = "sqlite"
file_path = "/var/sqlite/test.db"
password = "123"

Now look at both items of db_connections, it's basically different but with a same field type. Because of the type field it can be deserialized to different types like declearations below:

class BaseDbConnectionConfig {
    public string Type { get; set; }
}

class MysqlDbConnectionConfig : BaseDbConnectionConfig {
    public string Host { get; set; }
    public ushort Port { get; set; }
    public string UserId { get; set; }
    public string Password { get; set; }
    public string Schema { get; set; }
}

class SqliteDbConnectionConfig : BaseDbConnectionConfig {
    public string FilePath { get; set; }
    public string Password { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR welcomeUser contribution/PR is welcomeenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions