SamboyCoding / Tomlet

Zero-Dependency, model-based TOML De/Serializer for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhance README.md regarding tables

frankhommers opened this issue · comments

Hi there,

I love your lib, I think it's the best dotnet lib for parsing .toml files.
But I am struggling with the deserialization a bit. What would be the matching poco's of this structure?

[[fruit]]
  name = "apple" # I am a property in fruit table/map

  [fruit.geometry]
    shape = "round"
    note = "I am a property in geometry table/map"

  [[fruit.color]]
    name = "red"
    note = "I am an array item in apple fruit's table/map"

  [[fruit.color]]
    name = "green"
    note = "I am in the same array as red"

[[fruit]]
  name = "banana"

  [[fruit.color]]
    name = "yellow"
    note = "I am an array item in banana fruit's table/map"

Or even this:

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

I can't really figure it out by looking at your tests. It might help others as well to put the answer in README.md ;-)

Hi!

The first document displays a table-array, i.e. an array of tables. Tomlet maps each table to its own .NET object, so you can think of this as an array of objects. In this case, there's one root-level key, fruit, which would defined as an array of fruit objects. The equivalent c# would be something like

public class FruitDocumentRoot 
{
	public Fruit[] fruit;
}

public class Fruit 
{
	public string name;
	public Geometry geometry;
	public Color[] color;

	public class Geometry 
	{
		public string shape; //Or could be a 'shape' enum!
		public string note;
	}

	public class Color 
	{
		public string name;
		public string note
	}
}

The second example is of course much simpler:

public class ServersDocumentRoot 
{
	public Servers servers; //Or even, 'Dictionary<string, Server>'!
}

public class Servers 
{
	public Server alpha;
	public Server beta;
}

public class Server 
{
	public string ip;
	public string role; //Or a 'Role' enum!
}

In both cases, the potentially-confusing part is that you need a data type to represent the root of the document, with it having a single key - servers or fruit.
Hope this helps!

Yes this helps a lot! Thank you!