paiden / Nett

.Net library for TOML
MIT License
223 stars 27 forks source link

Support jagged array and array of tables conversion in Nett.AspNet #75

Closed twaalewijn closed 5 years ago

twaalewijn commented 5 years ago

In one of the programs I'm trying to add TOML configuration support to it needs to convert/bind a section that sort of looks like the following JSON and YAML example.

JSON

{
  "profiles": [
    {
      "name": "profile1",
      "setting1": "somevalue"
    },
    {
      "name": "profile2",
      "setting1": "someothervalue"
    }
  ]
}

YAML

profiles:
  - name: "profile1"
    setting1: "somevalue"
  - name: "profile2"
    setting1: "someothervalue"

TOML If I understand the TOML spec correctly the above config should be defined as an array of tables like this:

[[profiles]]
name = "profile1"
setting1 = "somevalue"

[[profiles]]
name = "profile2"
setting1 = "someothervalue"

Or with an inlined array of tables:

profiles = [ { name = "profile1", setting1 = "somevalue" },
             { name = "profile2", setting1 = "someothervalue" } ]

But the above example currently throws an InvalidOperationException since the ProviderDictionaryConverter doesn't support processing TomlTableArrays.

Both the JSON and YAML libraries I'm using produce the following data when converting the example:

{ "profiles:0:name", "profile1" },
{ "profiles:0:setting1", "somevalue" },
{ "profiles:1:name", "profile2" },
{ "profiles:1:setting1", "someothervalue" }

Which suggests converting a TomlTableArray should use similar rules as converting a TomlArray, namely appending the current key with the array index of the current item.

Jagged arrays

Jagged arrays can also be supported the same way by recursively calling ProcessArray and appending current index to the full key: JSON

{
  "jagged": [ [ 1 ], [ 2, 3 ], [ 4 ] ]
}

YAML

jagged:
  - - 1
  - - 2
    - 3
  - - 4

TOML

jagged = [ [ 1 ], [ 2, 3 ], [ 4 ] ]

Produces

{ "jagged:0:0", "1" },
{ "jagged:1:0", "2" },
{ "jagged:1:1", "3" },
{ "jagged:2:0", "4" }
paiden commented 5 years ago

Released with 0.12.0