Comments (13)
Hey,
thanks for the question! This library does not contain any helpers for deserializing TOML into an arbitrary object or vice-versa. This choice was inspired by SimpleJSON to keep the overall size of the library smaller. As with SimpleJSON, this library simply provides helpers to access/write values more easily, so that accesses like table["foo"]["bar"]
where foo
nor bar
won't cause exceptions right away.
At the moment you would have to implement (de)serialization yourself, but I am considering extension methods for that.
from tommy.
I appreciate the reply. It seemed like that was the case, but I just wanted to be sure, so no worries. 👍
from tommy.
Late last night I decided to have a go at this to see if I could make it work by just passing in a class containing properties, as that is what I am using in my project at the moment. I started out making some basic attributes and just used a string and an int to test, but the results were promising, even if it ends up just being for my own use-case. I added several more types of test data and I am currently working my way through them to make sure I get the conversion working properly.
Code items
---FromClass
public static class ConfigUtils
{
public static void WriteToml()
{
var testData = new TestData();
TommyExtensions.FromClass(testData, "testdata.toml");
}
}
Attributes
[TommyTableName("nametest")]
public class TestData
{
[TommyComment(" Comment for string property")]
public string TestString { get; set; } = "Test String";
[TommyComment(" Comment for int property")]
public int TestInt { get; set; } = 1;
}
Test Results - testdata.toml
[nametest]
# Comment for string property
TestString = "Test String"
# Comment for int property
TestInt = 1
Currently testing conversions
[TommyTableName("nametest")]
public class TestData
{
[TommyComment(" Comment for string property")]
public string TestString { get; set; } = "Test String";
[TommyComment(" Comment for int property")]
public int TestInt { get; set; } = 1;
[TommyComment(" Comment for ulong property")]
public ulong TestUlong { get; set; } = 12345678901234567890;
[TommyComment(" Comment for float property")]
public float TestFloat { get; set; } = 123.123f;
[TommyComment(" Comment for double property")]
public double TestDouble { get; set; } = 1234.123;
[TommyComment(" Comment for decimal property")]
public decimal TestDecimal { get; set; } = new decimal(0.11);
[TommyComment(" Comment for IntArray property")]
public int[] TestIntArray { get; set; } = new[] {1, 2, 3, 4};
[TommyComment(" Comment for List<string> property")]
public List<string> TestStringList { get; set; } = new List<string> {"string1", "string2", "string3"};
}
from tommy.
First comment
It didn't quite come out properly, but I expected as much. I am going to have to do some more digging and testing to figure out the best way to handle the data type conversion for long and float, as well as handling collections.
First try
[nametest]
# Comment for string property
TestString = "Test String"
# Comment for int property
TestInt = 1
# Comment for ulong property
TestUlong = 1.234567890123457E+19
# Comment for float property
TestFloat = 123.12300109863281
# Comment for double property
TestDouble = 1234.123
# Comment for decimal property
TestDecimal = 0.11
Edited: Second comment
After some more testing, I was able to get collections to work in the second section. Just need to get the actual number conversions to work correctly. Though, there seems to be some inconsistencies with the spacing between the comments/items, though. I am not sure exactly why yet. I was just following the example when writing it to file, so it must be how I am creating the nodes and adding them to the TomlTable.
(For good measure I added a second item for each one without a comment just to make sure all was working well)
Second test
Data:
[TommyTableName("nametest")]
public class TestData
{
[TommyComment(" Comment for string property")]
public string TestStringComment { get; set; } = "Test String";
public string TestString { get; set; } = "Test String";
[TommyComment(" Comment for int property")]
public int TestIntComment { get; set; } = 1;
public int TestInt { get; set; } = 1;
[TommyComment(" Comment for ulong property")]
public ulong TestUlongComment { get; set; } = 12345678901234567890;
public ulong TestUlong { get; set; } = 12345678901234567890;
[TommyComment(" Comment for float property")]
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};
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"};
}
Result:
[nametest]
# Comment for string property
TestStringComment = "Test String"
TestString = "Test String"
# Comment for int property
TestIntComment = 1
TestInt = 1
# Comment for ulong property
TestUlongComment = 1.234567890123457E+19
TestUlong = 1.234567890123457E+19
# Comment for float property
TestFloatComment = 123.12300109863281
TestFloat = 123.12300109863281
# 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 ]
TestIntArray = [ 1, 2, 3, 4 ]
# Comment for List<string> property
TestStringListComment = [ "string1", "string2", "string3" ]
TestStringList = [ "string1", "string2", "string3" ]
from tommy.
Here is what I have so far, would definitely appreciate any feedback (not completed yet, of course). I have got the current conversions all working properly now. As of this moment, I only added the things I was actually needing for my project. I just need to add the rest of the types, dictionaries, and some additional error checking, as well as supporting fields in addition to properties, etc.
https://github.com/instance-id/TommyExtensions/blob/main/TommyExtensions/TommyExtensions.cs
Just did some multiline comment testing, seems to work out well. 👍
Multiline comments
[TommyComment(" Comment for string property\n Testing second line comment")]
public string TestStringComment { get; set; } = "Test String";
[nametest]
# Comment for string property
# Testing second line comment
TestStringComment = "Test String"
As well as:
[TommyComment(@" If left as default (""LiteDB.db"") database will be located in the same folder as the bot's binary file.
Note, the name can be anything, but path must include/end with a filename ending in the extension .db
Ex: ""C:\database.db""")]
public string DatabasePath { get; set; }
# If left as default ("LiteDB.db") database will be located in the same folder as the bot's binary file.
# Note, the name can be anything, but path must include/end with a filename ending in the extension .db
# Ex: "C:\database.db"
DatabasePath = "Test String"
from tommy.
Update:
For the most part, I have both passing an instance of a data class containing properties able to write to file, as well as passing the class type and path to read from disk, which then returns a new instance of the class.
Write:
var path = "TestData.toml";
var testData = new TestData();
testData.StringProperty = "A String";
TommyExtensions.ToTomlFile(testData, path);
Read:
var path = "TestData.toml";
TestData testData = TommyExtensions.FromTomlFile<TestData>(path);
Edit
I believe last, but definitely not least, I have standard Dictionary<K,V> working both write and read. As of now, the final out put looks as follows.
Input class instance
[TommyTableName("tablename")]
public class TestData
{
[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 Dictionary<K,V> property")]
public Dictionary<string, string> TestDictionaryComment { get; set; } =
new Dictionary<string, string>{{"string1Key", "string1Value"}, {"string2Key", "string2Value"}};
[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; } = 448543646457048970;
public ulong TestUlong { get; set; } = 448543646457048970;
[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, 448543646457048004};
[TommyComment(@" Comment for List<ulong> property")]
public List<ulong> TestULongList { get; set; } = new List<ulong> {448543646457048001, 448543646457048002, 448543646457048003};
}
Output file
[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 = 448543646457048970
# Comment for float property
# This item should appear third as it's sort order is : 2
TestFloatComment = 123.12300109863281
# Comment for date property
TestDateComment = 2020-12-10 01:15:36
# 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 = 448543646457048970
TestFloat = 123.12300109863281
# 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, 448543646457048004 ]
# Comment for List<ulong> property
TestULongList = [ 448543646457048001, 448543646457048002, 448543646457048003 ]
# Comment for Dictionary<K,V> property
[tablename.TestDictionaryComment]
DictionaryKeys = [ "string1Key", "string2Key" ]
DictionaryValues = [ "string1Value", "string2Value" ]
from tommy.
This looks awesome!
Are you looking into merging this into Tommy repo directly? If so, I think it'd be better if you instead created a fork and made a pull request as this thread is looking less like an issue and more like feature implementation.
Moreover, with pull requests I'd be able to see and comment on the code (again, assuming you are looking into actually getting this merged).
from tommy.
Yeah, I would like to. I mostly just wanted to make sure I was doing things the proper and most efficient way, and things were working correctly before I did a PR. I still have a little cleanup to do, but as long as nothing crazy comes up, I should have that done tomorrow.
from tommy.
I mostly just wanted to make sure I was doing things the proper and most efficient way, and things were working correctly before I did a PR
In that case you can mark a PR as a draft. Either way, it would allow to review and comment the code and the PR in general. I will certainly do that once you actually post the PR, but it just may mean twice the work if there is something to change.
from tommy.
That sounds perfect, I will do that. I am just trying to figure out what might be the best way to go about actually adding it to the current project. While I was building it I had a solution with a project containing the actual extensions and one for the demo that added reference to the extensions.
https://github.com/instance-id/TommyExtensions
What do you feel would be ideal, adding a new file or folder somewhere, or just taking my new code and adding it to your current Tommy/TommyExtensions.cs?
from tommy.
Looking at the project you linked, I think there are two options both of which are fine to me
- You keep your serializer/deserializer in your own repo. In that case you'll be able to easy update it and you keep the code. In that case I would ask for the following changes:
- Rename the repo to something more fitting, as there is already
TommyExtensions
file in this repo (which also gets packaged into the NuGet package) - Change target frameworks to
net35;netstandard2
(or at least addnet35
to the current list of targets) so that the project is usable everywhere alongside the main library - Optionally, push the package on NuGet once it's done and released
- Rename the repo to something more fitting, as there is already
- Fork this repo and create a new separate project for the (de)serializer extension then make a PR. This way users who don't need the thing can leave it out easily and I'll be able to also build a separate NuGet package out of it.
If you go with the first option, I'll be more than happy to add a link to the final repo to the README of this repo so people can find it. Either way, I feel like it's better to keep the object (de)serializer as opt-in for users. Even right now I'm a bit unhappy with how I bundle TommyExtensions.cs
with the main Tommy.cs
in the NuGet package because that unnecessarily bloats the DLL for users who need simple low-level access to TOML.
Which of the two are you more comfortable with? As I mentioned, both work fine with me as long as in the end there will be a .cs
file or a NuGet package one can easily include into the project alongside Tommy.cs
to have the feature enabled.
from tommy.
You will have to forgive me, some of this is relatively new to me as I primarily do my development in C# for the Unity game engine and it is usually just small projects I make myself, simply pushing to my repo and call it a day, so I have been trying to use this as a bit of a learning experience.
That said, ever since your reply I have been trying to get net35 to work/add as a target but for whatever reason, it's just not happening in either Rider or VS. Windows says I have it, VS Installer says I have it, Rider even shows it added under dependencies, but I keep getting messages saying I don't have it when I try to build. With netstandard 2.0 it works fine, but something is up with 3.5. I will keep trying to get it figured out.
If for whatever reason I just can't get it to work I will make a PR instead with only the extensions (I created a new solution when things weren't working with 3.5, and renamed it to Tommy.Serializer and I renamed the actual class to TommySerializer, with the methods still being ToTomlFile and FromTomlFile)
from tommy.
Ok, finally got it worked out. Turns out, the issue ended up being not my IDE or configuration setup, but the .Net SDK itself. I kept trying to create new solutions, new projects, sometimes it might half work sometimes it would not even recognize the system assemblies, etc. I installed a different .Net SDK and then it worked almost straight away. I just had to make some slight adjustments to how I was getting attributes to work with .net 3.5, but as of now it is targeting and building for both 3.5 and netstandard2.0.
from tommy.
Related Issues (20)
- Generated TOML file contains unwanted hashtags between every line
- When saving an edited TOML file the last line is repeated to the next line corrupted HOT 6
- Different variable types in one array HOT 1
- TomlString doesn't allow specifying initial newline after delimiter HOT 4
- TommyExtensions MergeWith when using mergeNewValues bugged
- Nested inline TomlTable writes out of order
- Does this require a concrete class or can we set and get values directly? HOT 1
- Tommy does not properly handle ill-formed inline arrays HOT 1
- Unable to create TOML configuration from stream HOT 3
- 2.0.0 -> 3.1.1 performance degradation HOT 2
- Write layout - "As flat as possible option" HOT 2
- unable to load numbers without decimal places as double HOT 1
- Toml.WriteTo Custom Output support HOT 3
- Unable to write all nodes inside an array or a table HOT 1
- Tables that are not loaded inline as write inline
- A way to make deep coppies of a TomlTable?
- Using an indexer on Table result in error
- Parse from a string rather than a file HOT 1
- How to handle TomlLazy? HOT 2
- Configuration binding of array leads to duplicate values
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tommy.