The primary goal of this library is to add automatic serialization/deserialization of a class instance to the Tommy Toml library.
(De)Serialization of a class instance to/from a file has been implemented, along with the ability to work with List, Primitive[], and basic Dictionary<primitive, primitive>. If more advanced usage is required, just let me know.
Currently, to install this extension either download the extensions file and add it to your project, or create a new C# script in your project and simply copy and paste the contents of the script. I may look into making a nuget package of it as well.
Attributes
Attribute | Usage | Result |
TommyTableName |
// Designates a class as a Toml Table and applies all
// contained properties as children of that table
[TommyTableName("mytablename")]
public class TestData { //... } |
[mytablename]
|
TommyComment |
// String value which will be used as a
// comment for the property/field
[TommyComment(" Comment for string property")]
public string TestString { get; set; } = "Test String" |
# Comment for string property
TestString = "Test String"
|
TommySortOrder |
// Determines the order in which the
// properties will be written to file
[TommySortOrder(1)]
[TommyComment(" Sort order 1")]
public float TestFloat1 { get; set; } = 234.234f;
[TommySortOrder(0)]
[TommyComment(" Sort order 0")]
public float TestFloat0 { get; set; } = 123.123f; |
# Sort order 0
TestFloat0 = 123.123
# Sort order 1
TestFloat1 = 234.234
|
TommyInclude |
// Designates a private field to be
// included by the Tommy processor
[TommyInclude]
private string testIncludeField = "I'm private, so what?"; |
testIncludeField = "I'm private, so what?"
|
TommyIgnore |
// Designates a property to be ignored
// by the Tommy processor
[TommyIgnore]
public string TestIgnoreProperty { get; set; } |
|
While attributes are included for specific situations, if a property or field is public, they will be included automatically, unless the [TommyIgnore] attribute is applied to them.
using Tommy.Serializer;
TestData testData = new TestData();
string path = "path/to/TestData.toml";
TommySerializer.ToTomlFile(testData, path);
NOTE: When outputting multiple data objects to a single file, while not required, it is advised that each data class utilize the [TommyTableName] attribute to encapsulate the data under the proper table (primarily so that you can choose their table name). If the attribute is omitted, the object's type name is used as the table name automatically.
var testData = new TestData();
var testData2 = new TestData2();
var path = "path/to/TestData.toml";
Tommy.Serializer.ToTomlFile(new object[] {testData, testData2}, path);
var testData = new TestData();
var path = "path/to/TestData.toml";
var testData2 = new TestData2();
var path2 = "path/to/TestData2.toml";
TommySerializer.ToTomlFile(testData, path);
TommySerializer.ToTomlFile(testData, path2);
var path = "path/to/TestData.toml";
TestData testData = TommySerializer.FromTomlFile<TestData>(path);
If you download the complete solution from this repo and run the Demo project, it will use the following data class and produce the output file seen below that.
Data Class
[TommyTableName("tablename")]
public class TestData
{
[TommyInclude]
private string TestIncludeProperty { get; set; } = "I should show up in the created file even when private";
[TommyInclude]
[TommySortOrder(4)]
[TommyComment(@" Comment for private field
This item should appear fifth as it's sort order is : 4")]
private string testIncludePrivateField = "I should be included even when private";
[TommyInclude]
[TommySortOrder(3)]
[TommyComment(@" Comment for public field
This item should appear fourth as it's sort order is : 3")]
public string TestIncludePublicField = "Public string Data";
[TommyIgnore]
public string TestIgnoreProperty { get; set; } = "I should not show up in the created file";
[TommyComment(" Comment for date property")]
public DateTime TestDateComment { get; set; } = DateTime.Now;
[TommyComment(" Comment for string property\n Testing second line comment\n" +
"This and subsequent items should appear after the sorted properties")]
public string TestStringComment { get; set; } = "Test String";
[TommyComment(@" This item should be a blank string : Testing null value")]
public string TestNullString { get; set; }
[TommyComment(@" Comment testing multiline verbatim strings #1
Comment testing multiline verbatim strings #2
Comment testing multiline verbatim strings #3")]
public string TestComment { get; set; } = "Test String";
[TommyComment(" Comment for bool property")]
public bool TestBoolComment { get; set; } = true;
public bool TestBool { get; set; }
[TommyComment(" Comment for int property")]
public int TestIntComment { get; set; } = 1;
public int TestInt { get; set; } = 1;
[TommySortOrder(1)]
[TommyComment(@" Comment for ulong property
This item should appear second as it's sort order is : 1")]
public ulong TestUlongComment { get; set; } = 444543646457048001;
public ulong TestUlong { get; set; } = 444543646457048001;
[TommySortOrder(2)]
[TommyComment(@" Comment for float property
This item should appear third as it's sort order is : 2")]
public float TestFloatComment { get; set; } = 123.123f;
public float TestFloat { get; set; } = 123.123f;
[TommyComment(" Comment for double property")]
public double TestDoubleComment { get; set; } = 1234.123;
public double TestDouble { get; set; } = 1234.123;
[TommyComment(" Comment for decimal property")]
public decimal TestDecimalComment { get; set; } = new decimal(0.11);
public decimal TestDecimal { get; set; } = new decimal(0.11);
[TommyComment(" Comment for IntArray property")]
public int[] TestIntArrayComment { get; set; } = new[] {1, 2, 3, 4};
[TommySortOrder(0)]
[TommyComment(@" This item should appear first as it's sort order is : 0")]
public int[] TestIntArray { get; set; } = new[] {1, 2, 3, 4};
[TommyComment(@" Comment for List<string> property")]
public List<string> TestStringListComment { get; set; } = new List<string> {"string1", "string2", "string3"};
public List<string> TestStringList { get; set; } = new List<string> {"string1", "string2", "string3"};
[TommyComment(@" Comment for ulong array property")]
public ulong[] TestULongArray { get; set; } = new ulong[] {448543646457048001, 448543646457048002, 448543646457048003};
[TommyComment(@" Comment for List<ulong> property")]
public List<ulong> TestULongList { get; set; } = new List<ulong> {448543646457048001, 448543646457048002, 448543646457048003};
[TommyComment(" Comment for Dictionary<K,V> property")]
public Dictionary<string, string> TestDictionaryComment { get; set; } =
new Dictionary<string, string>{{"string1Key", "string1Value"}, {"string2Key", "string2Value"}};
}
Output of Above Data Class
[tablename]
# This item should appear first as it's sort order is : 0
TestIntArray = [ 1, 2, 3, 4 ]
# Comment for ulong property
# This item should appear second as it's sort order is : 1
TestUlongComment = 444543646457048001
# Comment for float property
# This item should appear third as it's sort order is : 2
TestFloatComment = 123.123
# Comment for public field
# This item should appear fourth as it's sort order is : 3
TestIncludePublicField = "Public string Data"
# Comment for private field
# This item should appear fifth as it's sort order is : 4
testIncludePrivateField = "I should be included even when private"
TestIncludeProperty = "I should show up in the created file even when private"
# Comment for date property
TestDateComment = 2020-12-13 15:06:18
# Comment for string property
# Testing second line comment
# This and subsequent items should appear after the sorted properties
TestStringComment = "Test String"
# This item should be a blank string : Testing null value
TestNullString = ""
# Comment testing multiline verbatim strings #1
# Comment testing multiline verbatim strings #2
# Comment testing multiline verbatim strings #3
TestComment = "Test String"
# Comment for bool property
TestBoolComment = true
TestBool = false
# Comment for int property
TestIntComment = 1
TestInt = 1
TestUlong = 444543646457048001
TestFloat = 123.123
# Comment for double property
TestDoubleComment = 1234.123
TestDouble = 1234.123
# Comment for decimal property
TestDecimalComment = 0.11
TestDecimal = 0.11
# Comment for IntArray property
TestIntArrayComment = [ 1, 2, 3, 4 ]
# Comment for List<string> property
TestStringListComment = [ "string1", "string2", "string3" ]
TestStringList = [ "string1", "string2", "string3" ]
# Comment for ulong array property
TestULongArray = [ 448543646457048001, 448543646457048002, 448543646457048003 ]
# Comment for List<ulong> property
TestULongList = [ 448543646457048001, 448543646457048002, 448543646457048003 ]
# Comment for Dictionary<K,V> property
[tablename.TestDictionaryComment]
DictionaryKeys = [ "string1Key", "string2Key" ]
DictionaryValues = [ "string1Value", "string2Value" ]
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate (once there are some, of course).