SamboyCoding / Tomlet

Zero-Dependency, model-based TOML De/Serializer for .NET
MIT License
164 stars 29 forks source link

Capture unrecognized fields #46

Open laper32 opened 1 week ago

laper32 commented 1 week ago

This is quite something like [JsonExtensionData] in System.Text.Json.

Does Tomlet provide this feature?

SamboyCoding commented 1 week ago

No, there is no such functionality built-in. What's your use-case?

laper32 commented 6 days ago

Consider this toml below:

[npc_hero_phantom_assassin]
health = 500 # builtin
mana = 300 # builtin
strength = 20 # builtin
agility = 20 # builtin
intelligence = 20 # builtin
attack_damage = 50 # builtin
attack_speed = 100 # builtin
phantom_assassin_critial_strike_damage = [2.5, 3.5, 4.5] # hero based
phantom_assassin_critial_strike_rate = 0.17 # hero based

Consider code below:

(Using System.Text.Json for easier representation)

public class TomlGameNPC
{
    [JsonPropertyName("health")]
    int Health {get; set;}

    [JsonPropertyName("mana")]
    int Mana {get; set;}

    [JsonPropertyName("strange")]
    int Strange {get; set;}

    [JsonPropertyName("agility")]
    int Agility {get; set;}

    [JsonPropertyName("intelligence")]
    int Intelligence {get; set;}

    [JsonPropertyName("attack_damage")]
    int AttackDamage {get; set;}

    [JsonPropertyName("attack_speed")]
    int AttackSpeed {get; set;}

    [JsonExtensionData]
    Dictionary<string, JsonElement> Attributes { get; set; }
}

Now as you can see, phantom_assassin_critial_strike_damage and phantom_assassin_critial_strike_rate is not general Game NPC, so we need to put all of them into Attributes.

When I need to do something more (e.g.: set hero's ability field) for Phantom Assassin, I will read TomlGameNPC.Attributes to find out these attributes and set them.