-
-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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
Make inst2dict and dict2inst work recursively #33137
Conversation
While testing serialization features with: var json = to_json(inst2dict(inst, true)) # recursive
var inst = dict2inst(parse_json(json)) I stumbled upon serialization "bug" pertaining to JSON parsing dictionary keys as described in #33161. If this feature gets merged then it certainly must be fixed. |
An alternative fix is to not turn dictionary keys (which can be object instances) into "dictionary instances" as seen in: for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
// Both key and value can be objects
Variant vi = _inst_to_dict_or_var(dict[E->get()], r_error, p_deep);
// Variant ki = _inst_to_dict_or_var(E->get(), r_error, p_deep); // do not support it?
Variant ki = E->get()
ret_dict[ki] = vi;
} But these can lead to unexpected serialization issues. Or just add explicit error check and call it a feature. |
Note: this was initially developed for/against @vnen given there was a "duplicate" PR fixing the same issue (#38377), I wonder what would be a good time to make this PR work against current |
This makes `inst2dict` GDScript method to turn inner instances present as properties to be converted to dictionaries recursively, including instances with nested arrays and dictionaries with key/value pair. Added a `deep` paramater to `inst2dict` so that the default behavior remains the same. Recursive conversion is disabled by default because instances may have circular references, hence must be used with caution on case-to-case basis. Crashes prevented by detecting circular references in debug builds. Updated documentation accordingly.
Rebased, but the test project fails now in current This is probably related to broken dictionaries as described in the similar the PR #38377 by @ThakeeNathees, but I'm not sure. Somehow, all the properties get converted to Not sure whether I'm motivated enough to make this work, and it would likely be safer to redo this from anew against In fact, I don't mind if @ThakeeNathees could get some insights from this PR, take both test projects and see how this can be improved, the idea is pre-approved by @vnen (IRC log):
If there's still no work done by any of the potential contributor, I might return to this when I'll actually need this feature myself. I've saved the original branch which was originally targeted for 3.2: https://github.com/Xrayez/godot/commit/9ea46abcc42b000776f5c5a5d1487fae4d10db06. |
Closes #6533.
This makes
inst2dict
GDScript method to turn inner instances present as properties to be converted to a dictionary recursively, including instances with nested arrays and dictionaries with key/value pair.Added a
deep
paramater toinst2dict
so that the default behavior remains the same. Recursive conversion is disabled by default because instances may have circular references as described in #31229 (comment), hence must be used with caution on case-to-case basis. Updated documentation accordingly.Features
inst2dict
.inst2dict
.dict2inst
(all above).recursive/deep
option forinst2dict
method.Test project
Extensive test project demonstrating all of the added features, and showing how having circular references fail, which is handled in debug builds by checking that only unique objects are converted.
inst2dict-recursive.zip