Samboy063.Tomlet
1.3.3
See the version list below for details.
dotnet add package Samboy063.Tomlet --version 1.3.3
NuGet\Install-Package Samboy063.Tomlet -Version 1.3.3
<PackageReference Include="Samboy063.Tomlet" Version="1.3.3" />
paket add Samboy063.Tomlet --version 1.3.3
#r "nuget: Samboy063.Tomlet, 1.3.3"
// Install Samboy063.Tomlet as a Cake Addin #addin nuget:?package=Samboy063.Tomlet&version=1.3.3 // Install Samboy063.Tomlet as a Cake Tool #tool nuget:?package=Samboy063.Tomlet&version=1.3.3
Tomlet
A TOML library for .NET
Tomlet is a library for the TOML configuration file format. It's targeting TOML v1.0.0.
Currently supported features are as follows:
- Primitive key-value pair reading
- Table Reading
- Inline Table Reading
- Array Reading
- Table-Array Reading
- Primitive key-value pair writing
- Table Writing
- Inline Table Writing
- Array Writing
- Table-Array Writing
- Full Unit Tests for everything supported here.
A word on dotted keys
The TOML specification allows for dotted keys (e.g. a.b.c = "d"
, which creates a table a
, containing a table b
, containing the key c
with the string value d
),
as well as quoted dotted keys (e.g. a."b.c" = "d"
, which creates a table a
, containing the key b.c
, with the string value d
).
Tomlet correctly handles both of these cases, but there is room for ambiguity in calls to TomlTable#GetValue
and its sibling methods.
For ease of internal use, GetValue
and ContainsKey
will interpret keys as-above, with key names containing dots requiring the key name to be quoted. So a call to ContainsKey("a.b")
will look for a table a
containing a key b
.
Note that, if you mistakenly do this, and there is a value mapped to the key a
which is NOT a table, a TomlContainsDottedKeyNonTableException
will be thrown.
However, for a more convenient API, calls to specific typed variants of GetValue
(GetString
, GetInteger
, GetDouble
, etc.) will assume keys are supposed to be quoted. That is, a call to
GetString("a.b")
will look for a single key a.b
, not a table a
containing key b
.
Usage
Serialize a runtime object to TOML
var myClass = new MyClass("hello world", 1, 3);
TomlDocument tomlDoc = Tomlet.DocumentFrom(myClass); //TOML document representation. Can be serialized using the SerializedValue property.
string tomlString = Tomlet.TomlStringFrom(myClass); //Formatted TOML string. Equivalent to Tomlet.DocumentFrom(myClass).SerializedValue
Deserialize TOML to a runtime object
string myString = GetTomlStringSomehow(); //Web request, read file, etc.
var myClass = Tomlet.To<MyClass>(myString); //Requires a public, zero-argument constructor on MyClass.
Console.WriteLine(myClass.configurationFileVersion); //Or whatever properties you define.
Parse a TOML File
TomlParser.ParseFile
is a utility method to parse an on-disk toml file. This just uses File.ReadAllText, so I/O errors will be thrown up to your calling code.
TomlDocument document = TomlParser.ParseFile(@"C:\MyFile.toml");
//You can then convert this document to a runtime object, if you so desire.
var myClass = Tomlet.To<MyClass>(document);
Parse Arbitrary TOML input
Useful for parsing e.g. the response of a web request.
TomlParser parser = new TomlParser();
TomlDocument document = parser.Parse(myTomlString);
Creating your own mappers.
By default, serialization will call ToString on an object, and deserialization will piece together an object field-by-field using reflection, excluding fields marked as
[NonSerialized]
, and using a parameterless constructor to instantiate the object.
This approach should work for most model classes, but should something more complex be used, such as storing a colour as an integer/hex string, or if you have a more compact/proprietary method of serializing your classes, then you can override this default using code such as this:
// Example: UnityEngine.Color stored as an integer in TOML. There is no differentiation between 32-bit and 64-bit integers, so we use TomlLong.
Tomlet.RegisterMapper<Color>(
//Serializer (toml value from class)
color => new TomlLong(color.a << 24 | color.r << 16 | color.g << 8 | color.b),
//Deserializler (class from toml value)
tomlValue => {
if(!(tomlValue is TomlLong tomlLong))
throw new TomlTypeMismatchException(typeof(TomlLong), tomlValue.GetType(), typeof(Color))); //Expected type, actual type, context (type being deserialized)
int a = tomlLong.Value >> 24 & 0xFF;
int r = tomlLong.Value >> 16 & 0xFF;
int g = tomlLong.Value >> 8 & 0xFF;
int b = tomlLong.Value & 0xFF;
return new Color(r, g, b, a);
}
);
Calls to Tomlet.RegisterMapper
can specify either the serializer or deserializer as null
, in which case the default (de)serializer will be used.
Retrieve data from a TomlDocument
TomlDocument document; // See above for how to obtain one.
int someInt = document.GetInteger("myInt");
string someString = document.GetString("myString");
// TomlArray implements IEnumerable<TomlValue>, so you can iterate over it or use LINQ.
foreach(var value in document.GetArray("myArray")) {
Console.WriteLine(value.StringValue);
}
//It also has an index operator, so you can do this
Console.WriteLine(document.GetArray("myArray")[0]);
List<string> strings = document.GetArray("stringArray").Select(v => (TomlString) v).ToList();
//Retrieving sub-tables. TomlDocument is just a special TomlTable, so you can
//call GetSubTable on the resulting TomlTable too.
string subTableString = document.GetSubTable("myTable").GetString("aString");
//Date-Time objects. There's no API for these (yet)
var dateValue = document.GetValue("myDateTime");
if(dateValue is TomlOffsetDateTime tomlODT) {
DateTimeOffset date = tomlODT.Value;
Console.WriteLine(date); //27/05/1979 00:32:00 -07:00
} else if(dateValue is TomlLocalDateTime tomlLDT) {
DateTime date = tomlLDT.Value;
Console.WriteLine(date.ToUniversalTime()); //27/05/1979 07:32:00
} else if(dateValue is TomlLocalDate tomlLD) {
DateTime date = tomlLD.Value;
Console.WriteLine(date.ToUniversalTime()); //27/05/1979 00:00:00
} else if(dateValue is TomlLocalTime tomlLT) {
TimeSpan time = tomlLT.Value;
Console.WriteLine(time); //07:32:00.9999990
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 is compatible. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.7
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net5.0
- No dependencies.
NuGet packages (10)
Showing the top 5 NuGet packages that depend on Samboy063.Tomlet:
Package | Downloads |
---|---|
Swordfish.Library
Package Description |
|
Swordfish
Package Description |
|
Zaabee.Tomlet
The wrappers and extensions methods for Tomlet |
|
AuroraModularis.Settings.Toml
An extension for AuroraModularis to use TOML files instead of json |
|
Aoxe.Extensions.Configuration.Flattener.Tomlet
The Aoxe Configuration flattener implement by Tomlet. |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on Samboy063.Tomlet:
Repository | Stars |
---|---|
LavaGang/MelonLoader
The World's First Universal Mod Loader for Unity Games compatible with both Il2Cpp and Mono
|
|
sinai-dev/UnityExplorer
An in-game UI for exploring, debugging and modifying IL2CPP and Mono Unity games.
|
|
MCCTeam/Minecraft-Console-Client
Lightweight console for Minecraft chat and automated scripts
|
Version | Downloads | Last updated |
---|---|---|
5.4.0 | 11,109 | 7/29/2024 |
5.3.1 | 8,229 | 3/20/2024 |
5.3.0 | 138,266 | 1/3/2024 |
5.2.0 | 10,218 | 7/16/2023 |
5.1.3 | 578 | 6/6/2023 |
5.1.2 | 4,910 | 1/20/2023 |
5.1.1 | 309 | 1/17/2023 |
5.1.0 | 320 | 1/15/2023 |
5.0.1 | 9,900 | 11/30/2022 |
5.0.0 | 2,977 | 9/21/2022 |
4.0.0 | 503 | 9/18/2022 |
3.2.2 | 2,347 | 8/8/2022 |
3.2.1 | 455 | 8/5/2022 |
3.2.0 | 561 | 8/1/2022 |
3.1.4 | 464 | 7/24/2022 |
3.1.3 | 17,892 | 1/18/2022 |
3.1.2 | 386 | 1/4/2022 |
3.1.1 | 318 | 12/29/2021 |
3.1.0 | 578 | 12/26/2021 |
3.0.1 | 282 | 12/24/2021 |
3.0.0 | 263 | 12/23/2021 |
2.2.0 | 313 | 12/18/2021 |
2.1.0 | 3,178 | 11/25/2021 |
2.0.2 | 598 | 10/16/2021 |
2.0.1 | 442 | 10/10/2021 |
2.0.0 | 460 | 8/16/2021 |
1.3.5 | 770 | 7/7/2021 |
1.3.4 | 497 | 6/20/2021 |
1.3.3 | 6,359 | 6/10/2021 |
1.3.2 | 455 | 5/23/2021 |
1.3.1 | 377 | 5/4/2021 |
1.3.0 | 331 | 5/3/2021 |
1.2.0 | 392 | 4/21/2021 |
1.1.2 | 371 | 4/18/2021 |
1.1.1 | 348 | 4/18/2021 |
1.1.0 | 365 | 4/18/2021 |
1.0.2 | 303 | 4/18/2021 |
1.0.1 | 314 | 4/18/2021 |
1.0.0 | 333 | 4/18/2021 |