Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Incorrect behaviour when serializing array of tables within table. #14

Closed
Simie opened this issue Oct 18, 2016 · 0 comments
Closed

Incorrect behaviour when serializing array of tables within table. #14

Simie opened this issue Oct 18, 2016 · 0 comments

Comments

@Simie
Copy link

Simie commented Oct 18, 2016

I created this repro case:

public class SubTable
{
    public class ListTable
    {
        public int SomeValue { get; set; } = 5;

        public override string ToString()
        {
            return $"ListTable({SomeValue})";
        }
    }

    public List<ListTable> Values { get; set; } = new List<ListTable>();

    public override string ToString()
    {
        return $"SubTable({string.Join(",", Values.Select(p => p.ToString()))})";
    }
}

public class RootTable
{
    public SubTable SubTable { get; set; } = new SubTable();

    public override string ToString()
    {
        return $"RootTable({SubTable})";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var root = new RootTable();
        root.SubTable.Values.AddRange(new[]
            {new SubTable.ListTable() {SomeValue = 1}, new SubTable.ListTable() {SomeValue = 5}});

        Console.WriteLine("Before: {0}", root);


        Console.WriteLine("=== Output TOML: ===");
        var toml = Nett.Toml.WriteString(root);
        Console.WriteLine(toml);
        Console.WriteLine("=== END TOML ===");

        var readRoot = Nett.Toml.ReadString(toml).Get<RootTable>();

        Console.WriteLine("After: {0}", readRoot);

        Console.ReadKey(true);
    }
}

When running I get this output:

Before: RootTable(SubTable(ListTable(1),ListTable(5)))
=== Output TOML: ===

[SubTable]

[[Values]]
SomeValue = 1
[[Values]]
SomeValue = 5

=== END TOML ===
After: RootTable(SubTable())

The deserialized object does not match the original object.
I'm new to TOML so I may be wrong, but I'd expect the output TOML to be this:

[SubTable]

[[SubTable.Values]]
SomeValue = 1
[[SubTable.Values]]
SomeValue = 5

And indeed, reading this TOML returns the correct values:

var testRead =
    Nett.Toml.ReadString(
            "[SubTable]\r\n\r\n[[SubTable.Values]]\r\nSomeValue = 1\r\n[[SubTable.Values]]\r\nSomeValue = 5")
        .Get<RootTable>();

Returns RootTable(SubTable(ListTable(1),ListTable(5)))

@paiden paiden closed this as completed in f80f3e5 Oct 22, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant