xoofx / Tomlyn

Tomlyn is a TOML parser, validator and authoring library for .NET Framework and .NET Core
BSD 2-Clause "Simplified" License
424 stars 29 forks source link

Can't serialize get-only properties #59

Open jmyersmsft opened 1 year ago

jmyersmsft commented 1 year ago

Been using Tomlyn in my product and it's working well, but I was writing some tests and came across a speed bump.

It'd be nifty if we could use something like the following to generate "ad-hoc" TOML content for my tests. I use a similar pattern for JSON:

var cargoToml = Toml.FromModel(new { package = new { name = "pkgname", version = "1.0.0" } });

This doesn't work though, because the properties of anonymous types are generated as get-only, and StandardObjectDynamicAccessor.Initialize() specifically filters out get-only properties. cargoToml in that example ends up being "[package]\r\n". I think that filtering makes sense for ToModel, but it's kind of inconvenient in scenarios where I'm only generating TOML with FromModel but not parsing it.

I know I can work around with the following, but the anonymous-types version is just a bit more concise:

Toml.FromModel(
    new TomlTable
    {
        ["package"] = new TomlTable
        {
            ["name"] = "pkgname",
            ["version"] = "1.0.0"
        }
    }));