orchestr7 / ktoml

Kotlin Multiplatform parser and compile-time serializer/deserializer for TOML format (Native, JS, JVM) based on KxS
https://akuleshov7.github.io/ktoml
MIT License
456 stars 25 forks source link

How to deserialize TomlNode? #280

Open Xirado opened 2 months ago

Xirado commented 2 months ago

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

orchestr7 commented 2 months ago

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 commented 2 months ago

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

Xirado commented 2 months ago

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

akuleshov7 commented 2 months ago

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?

Xirado commented 2 months ago

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