{"id":18339055,"url":"https://github.com/xoofx/tomlyn","last_synced_at":"2026-04-02T11:54:55.397Z","repository":{"id":38713478,"uuid":"168931002","full_name":"xoofx/Tomlyn","owner":"xoofx","description":"Tomlyn is a TOML parser, validator and authoring library for .NET Framework and .NET Core","archived":false,"fork":false,"pushed_at":"2025-03-11T10:34:56.000Z","size":469,"stargazers_count":461,"open_issues_count":35,"forks_count":31,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-06T08:01:41.635Z","etag":null,"topics":["csharp","dotnet","dotnet-core","toml","toml-parser","toml-validation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xoofx.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":".github/FUNDING.yml","license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["xoofx"]}},"created_at":"2019-02-03T09:54:12.000Z","updated_at":"2025-04-02T08:39:13.000Z","dependencies_parsed_at":"2025-01-04T12:37:27.781Z","dependency_job_id":"eec2f35f-bedc-4353-a7b5-a504ed4e285b","html_url":"https://github.com/xoofx/Tomlyn","commit_stats":{"total_commits":150,"total_committers":16,"mean_commits":9.375,"dds":0.5266666666666666,"last_synced_commit":"a4faf8c015d76f512481ba291700f1a077dad818"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FTomlyn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FTomlyn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FTomlyn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FTomlyn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xoofx","download_url":"https://codeload.github.com/xoofx/Tomlyn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248708338,"owners_count":21148973,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","dotnet-core","toml","toml-parser","toml-validation"],"created_at":"2024-11-05T20:16:20.057Z","updated_at":"2026-04-02T11:54:55.390Z","avatar_url":"https://github.com/xoofx.png","language":"C#","funding_links":["https://github.com/sponsors/xoofx"],"categories":[],"sub_categories":[],"readme":"# Tomlyn [![ci](https://github.com/xoofx/Tomlyn/actions/workflows/ci.yml/badge.svg)](https://github.com/xoofx/Tomlyn/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/xoofx/Tomlyn/badge.svg?branch=main)](https://coveralls.io/github/xoofx/Tomlyn?branch=main) [![NuGet](https://img.shields.io/nuget/v/Tomlyn.svg)](https://www.nuget.org/packages/Tomlyn/)\n\n\u003cimg align=\"right\" width=\"256px\" height=\"256px\" src=\"img/Tomlyn.png\"\u003e\n\nTomlyn is a high-performance .NET [TOML](https://toml.io/en/) 1.1 parser, round-trippable syntax tree, and `System.Text.Json`-style object serializer - NativeAOT ready.\n\n#### \u003cimg src=\"https://xoofx.github.io/SharpYaml/img/SharpYaml.png\" alt=\"SharpYaml\" height=\"32\" style=\"vertical-align: text-bottom; margin-right: .45rem;\" /\u003e Looking for YAML support? Check out [SharpYaml](https://xoofx.github.io/SharpYaml/).\n\n\u003e **Note**: Tomlyn v1 is a major redesign with breaking changes from earlier versions. It uses a **`System.Text.Json`-style API** with `TomlSerializer`, `TomlSerializerOptions`, and resolver-based metadata (`ITomlTypeInfoResolver`). See the [migration guide](https://xoofx.github.io/Tomlyn/docs/migration) for details.\n\n## ✨ Features\n\n- **`System.Text.Json`-style API**: familiar surface with `TomlSerializer`, `TomlSerializerOptions`, `TomlTypeInfo\u003cT\u003e`\n- **TOML 1.1.0 only**: Tomlyn v1 targets [TOML 1.1.0](https://toml.io/en/v1.1.0) and does **not** support TOML 1.0\n- **Source generation**: NativeAOT / trimming friendly via `TomlSerializerContext` and `[TomlSerializable]` roots\n- **Cross-project polymorphism**: register derived types at runtime or on a source-generated context when base and derived types live in different assemblies\n- **`System.Text.Json` attribute interop**: reuse `[JsonPropertyName]`, `[JsonIgnore]`, `[JsonRequired]`, `[JsonConstructor]`, `[JsonObjectCreationHandling]`, and polymorphism attributes\n- **Flexible collection input**: opt a collection member into accepting either a single TOML value or an array via `[TomlSingleOrArray]`\n- **Allocation-free parsing pipeline**: incremental `TomlLexer` → `TomlParser` with precise spans for errors\n- **Low-level access**: full lexer/parser API plus a lossless, trivia-preserving syntax tree (`SyntaxParser` → `DocumentSyntax`)\n- **Reflection control**: reflection-based POCO mapping is available, but can be disabled for NativeAOT via a feature switch / MSBuild property\n\n## 📐 Requirements\n\nTomlyn targets `net8.0`, `net10.0`, and `netstandard2.0`.\n\n- Consuming the NuGet package works on any runtime that supports `netstandard2.0` (including .NET Framework) or modern .NET (`net8.0+`).\n- Building Tomlyn from source requires the .NET 10 SDK.\n\n## 📦 Install\n\n```sh\ndotnet add package Tomlyn\n```\n\nTomlyn ships the source generator in-package (`analyzers/dotnet/cs`) - no extra package needed.\n\n## 🚀 Quick Start\n\n```csharp\nusing Tomlyn;\n\n// Serialize\nvar toml = TomlSerializer.Serialize(new { Name = \"Ada\", Age = 37 });\n\n// Deserialize\nvar person = TomlSerializer.Deserialize\u003cPerson\u003e(toml);\n```\n\n### Untyped model (`TomlTable`)\n\n```csharp\nusing Tomlyn;\nusing Tomlyn.Model;\n\nvar toml = @\"global = \"\"this is a string\"\"\n# This is a comment of a table\n[my_table]\nkey = 1 # Comment a key\nvalue = true\nlist = [4, 5, 6]\n\";\n\nvar model = TomlSerializer.Deserialize\u003cTomlTable\u003e(toml)!;\nvar global = (string)model[\"global\"]!;\n\nConsole.WriteLine(global);\nConsole.WriteLine(TomlSerializer.Serialize(model));\n```\n\n### Options\n\n```csharp\nusing System.Text.Json;\nusing Tomlyn;\n\nvar options = new TomlSerializerOptions\n{\n    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,\n    PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,\n    WriteIndented = true,\n    IndentSize = 4,\n    MaxDepth = 64,\n    DefaultIgnoreCondition = TomlIgnoreCondition.WhenWritingNull,\n};\n\nvar toml = TomlSerializer.Serialize(config, options);\nvar model = TomlSerializer.Deserialize\u003cMyConfig\u003e(toml, options);\n```\n\nBy default, `PropertyNamingPolicy` is `null`, meaning CLR member names are used as-is for TOML mapping keys (same default as `System.Text.Json`).\n`MaxDepth = 0` uses the built-in default of `64`.\n`PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace` matches `System.Text.Json`: read-only properties are not populated unless you opt into `Populate` via options or `[JsonObjectCreationHandling]`.\n\n### Source Generation\n\n```csharp\nusing System.Text.Json.Serialization;\nusing Tomlyn.Serialization;\n\npublic sealed class MyConfig\n{\n    public string? Global { get; set; }\n}\n\n[TomlSourceGenerationOptions(\n    PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,\n    PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace)]\n[TomlSerializable(typeof(MyConfig))]\ninternal partial class MyTomlContext : TomlSerializerContext\n{\n}\n\nvar config = TomlSerializer.Deserialize(toml, MyTomlContext.Default.MyConfig);\nvar tomlOut = TomlSerializer.Serialize(config, MyTomlContext.Default.MyConfig);\n```\n\n### Cross-Project Polymorphism\n\nWhen a base type lives in one project and derived types live in another, you can register derived types without putting `[TomlDerivedType]` on the base type.\n\n**Reflection path**:\n\n```csharp\nusing Tomlyn;\n\nvar options = new TomlSerializerOptions\n{\n    PolymorphismOptions = new TomlPolymorphismOptions\n    {\n        TypeDiscriminatorPropertyName = \"kind\",\n        DerivedTypeMappings = new Dictionary\u003cType, IReadOnlyList\u003cTomlDerivedType\u003e\u003e\n        {\n            [typeof(Animal)] =\n            [\n                new(typeof(Cat), \"cat\"),\n                new(typeof(Dog), \"dog\"),\n            ],\n        },\n    },\n};\n```\n\n**Source generation path**:\n\n```csharp\nusing Tomlyn.Serialization;\n\n[TomlSerializable(typeof(Animal))]\n[TomlDerivedTypeMapping(typeof(Animal), typeof(Cat), \"cat\")]\n[TomlDerivedTypeMapping(typeof(Animal), typeof(Dog), \"dog\")]\ninternal partial class MyTomlContext : TomlSerializerContext\n{\n}\n```\n\nUse `TomlPolymorphismOptions.DerivedTypeMappings` and `[TomlDerivedTypeMapping]` additively with existing base-type attributes. Base-type registrations still take precedence when the same discriminator or derived type is registered more than once.\n\n### Single Value Or Array Collections\n\n```csharp\nusing System.Text.Json.Serialization;\nusing Tomlyn.Serialization;\n\npublic sealed class PackagingConfiguration\n{\n    public PackagingConfiguration()\n    {\n        RuntimeIdentifiers = new List\u003cstring\u003e();\n    }\n\n    [TomlSingleOrArray]\n    [JsonPropertyName(\"rid\")]\n    public List\u003cstring\u003e RuntimeIdentifiers { get; }\n}\n```\n\nWith `[TomlSingleOrArray]`, both of these TOML payloads are valid:\n\n```toml\nrid = \"win-x64\"\n```\n\n```toml\nrid = [\"win-x64\", \"linux-x64\"]\n```\n\nFor mutable read-only collection members, Tomlyn appends into the existing collection instance. Without `[TomlSingleOrArray]`, collection members still require a TOML array.\n\n### Reflection Control\n\nReflection fallback can be disabled globally before first serializer use:\n\n```csharp\nAppContext.SetSwitch(\"Tomlyn.TomlSerializer.IsReflectionEnabledByDefault\", false);\n```\n\nWhen publishing with NativeAOT (`PublishAot=true`), the Tomlyn NuGet package disables reflection-based serialization by default.\nYou can override the default by setting the following MSBuild property in your app project:\n\n```xml\n\u003cPropertyGroup\u003e\n  \u003cTomlynIsReflectionEnabledByDefault\u003etrue\u003c/TomlynIsReflectionEnabledByDefault\u003e\n\u003c/PropertyGroup\u003e\n```\n\n## 📖 Documentation\n\n- [User guide](site/readme.md)\n- Website (Lunet): https://xoofx.github.io/Tomlyn\n\n## 🪪 License\n\nThis software is released under the [BSD-Clause 2 license](https://opensource.org/licenses/BSD-2-Clause). \n\n## 🤗 Author\n\nAlexandre Mutel aka [xoofx](http://xoofx.github.io).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoofx%2Ftomlyn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxoofx%2Ftomlyn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoofx%2Ftomlyn/lists"}