ktoml icon indicating copy to clipboard operation
ktoml copied to clipboard

How to deserialize TomlNode?

Open Xirado opened this issue 1 year ago • 5 comments

I need to dynamically traverse a toml file since i cannot deserialize the whole thing, but have not found a way to deserialize a TomlNode into a kotlinx Serializable class. Is it possible? If not, i think adding this functionality would be beneficial

Xirado avatar Sep 23 '24 15:09 Xirado

I need to dynamically traverse a toml file since i cannot deserialize the whole thing, but have not found a way to deserialize a TomlNode into a kotlinx Serializable class. Is it possible? If not, i think adding this functionality would be beneficial

Hey! Yes, we have a partial serialization. It is very easy

orchestr7 avatar Sep 25 '24 20:09 orchestr7

I think this is your scenario. Check this part in the readmeimage

orchestr7 avatar Sep 25 '24 20:09 orchestr7

I think this is your scenario. Check this part in the readmeimage

Yes, i read about this, but shouldn't there be a more efficient way? Every call will cause the whole file to be parsed again, which is very inefficient, TomlTable already has all the information, so i think there should be a way to deserialize that aswell since it only requires reading the file once

Xirado avatar Sep 25 '24 20:09 Xirado

Yes, i read about this, but shouldn't there be a more efficient way? Every call will cause the whole file to be parsed again, which is very inefficient, TomlTable already has all the information, so i think there should be a way to deserialize that aswell since it only requires reading the file once

Hm, may be I am getting this wrong, but if your file is changing dynamically and you re-read it again and again, then there anyway will be same parsing.

May be you can give some small example of the input and API, so we can implement it?

akuleshov7 avatar Sep 26 '24 09:09 akuleshov7

Yes, i read about this, but shouldn't there be a more efficient way? Every call will cause the whole file to be parsed again, which is very inefficient, TomlTable already has all the information, so i think there should be a way to deserialize that aswell since it only requires reading the file once

Hm, may be I am getting this wrong, but if your file is changing dynamically and you re-read it again and again, then there anyway will be same parsing.

May be you can give some small example of the input and API, so we can implement it?

The file isn't changing, but i need to read the file as such

[a]
foo = "foo"
bar = "bar"

[b.a]
foo = "foo"
bar = "bar"

[b.b]
foo = "foo"
bar = "bar"

[x.y.z]
foo = "foo"
bar = "bar"

and then decode the file as such that i get Map<String, Foobar> where the String represents the path, so e.g. x.y.z

And i already have the correct TomlTables i want to convert to Foobar, it's just that ktoml offers no way to do this conversion directly without using partial deserialization which is highly inefficient if you invoke it multiple times which is required here

Xirado avatar Sep 26 '24 11:09 Xirado