-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request] make the toml encoder extensible #11
Comments
I've checked in a first version of this feature in commit 2199128. Does this look like it will fit your use cases? |
It is not working as expected. Running
I have
I would expect this output:
The problems I see are:
|
The first issue is a straightforward logic error, which should be fixed in 24054b1. I'm not sure what to think about the second. The json module acts as you describe, only calling the default method in the case of a non-encodable object. On the other hand, calling it for every level seems to enable more use cases; you can imagine a situation where you want to tweak the data on a higher level even though the original data is still technically encodable. The downside, of course, is that it hurts performance to call that method more often than is needed. I'll think about this and maybe do some profiling to determine the extent of the performance hit. |
I agree that your proposed protocol is more versatile than the one of the json module, and probably you will not notice a big performance degradation. My only concern is that users accustomed with the standard lib behaviour could be surprised by the new protocol. Maybe a different method name (like |
I found another path which leads to unexpected results (tested with 24054b1):
which errors with
|
default() only gets called separately on the values, not the keys. (This is consistent with the You can use the default method here, but it has to be explicit:
This rectifies the keys at the stage above, when default() is called on the dict as a value. (Supporting cases like this is one reason I wanted to call default() on every level of the tree, rather than just unsupported types.) As I think about this, though, I think it's probably a good idea to use a different protocol than default(). It's good to maintain interface compatibility with JSONEncoder, but it's also a protocol that's really designed to be used only for default objects, rather than for every level of the parse tree. I'll have to think about what design is best here. (Also, a naive implementation of the default() protocol almost doubles time spent on small encoding calls, and I suspect a different protocol could be more amenable to optimization.) |
you're right... also I have no strong opinion on the best protocol... I proposed |
The standard library json encoder is extensible, a feature that I find very handy:
so that
What about implementing a similar protocol also in qtoml?
The text was updated successfully, but these errors were encountered: