Ordered TOML

Hello!

Is there anyway to preserve file order when reading a TOML in Julia? Like building an array or creating an OrderedDict straight from a TOML file?

My goal is to make a program that reads an input file, performs an optimization routine based on the input, and writes the output in the same format as the input. Thus maintaining preserving the ordering from the input file is greatly desired for the iterative analysis.

Thanks in advance!

what exactly do you mean with the file order of a TOML file? if you’re simply referring to the line order, you can just read it in as any other file.

for example:

write("test.toml", "A = 1\nB = 2\nC = 3") # make fake TOML file

readlines("test.toml") |>
    x -> split.(x, " = ") |>
    v -> [x[1] => x[2] for x in v] # array with same order as file

You can kind of use the sorted=false, by=identity kwargs to TOML.print but that isn’t an automatic fix for keeping the same order in as you get out.

I did a crappy fork of the TOML package years ago, making a few small changes so it would return an OrderedDict (since Dict does not guarantee order).

I had to too much to do back then to do a proper contribution, but perhaps I could take a stab at it or simply share you my package (which it’s frozen on a version of the TOML package from years ago).

It may be possible to have an API where you do something like TOML.parse(OrderedDict, ...) which would give you ordered dicts back. I will look into it.

A bit of a proof of concept allow passing a dict type to the TOML parser - Pull Request #62691 - JuliaLang/julia - GitHub

I’d be really interested to take a look at this!

This looks great! Can’t wait to see it merged!