Skip to content

Avoid Object of type DAEmpty is not JSON serializable error#902

Closed
BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
SuffolkLITLab:daempty_json_serialization_issue
Closed

Avoid Object of type DAEmpty is not JSON serializable error#902
BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
SuffolkLITLab:daempty_json_serialization_issue

Conversation

@BryceStevenWilley
Copy link
Copy Markdown
Contributor

Due to a slight change in 0f5b49a (not mentioned in the description, so I don't know what the intention was for changing it), as_dict() is checked for and called before to_json() when safe_json is converting objects to JSON serializeable types.

However, when calling getattr() on a DAEmpty, it will return another DAEmpty object. Since DAEmptys are also callable objects that return another DAEmpty, calling as_dict() first will still leave a DAEmpty in the output, which is not safely JSON serializeable, leaving the issues raised in #877 unfixed.

Adding a special case for DAEmpty to call to_json() avoids this issue. Other approaches that could fix the issue include:

  • Just moving the hasattr(the_object, "to_json")... check before the as_dict one. While this is the simplest solution, it adds an implicit assumption in the code that will be easily missed on future refactorings of this function.
  • Making sure that the results of calling as_dict are still JSON safe. This would cause an infinite recursion on DAEmptys, and either wouldn't fix error or would result in max level nested entries for DAEmptys.
  • Add a as_dict function to DAEmpty. This would work, but as_dict isn't used elsewhere in docassemble code much.

Due to a slight change in jhpyle@0f5b49a
(not mentioned in the description, so I don't know what the intention was for changing it),
`as_dict()` is checked for and called before `to_json()` when `safe_json` is
converting objects to JSON serializeable types.

However, when calling `getattr()` on a `DAEmpty`, it will return another `DAEmpty` object.
Since `DAEmpty`s are also callable objects that return another `DAEmpty`, calling `as_dict()`
first will still leave a `DAEmpty` in the output, which is not safely JSON serializeable,
leaving the issues raised in jhpyle#877 unfixed.

Adding a special case for DAEmpty to call `to_json()` avoids this issue.
Other approaches that could fix the issue include:

* Just moving the `hasattr(the_object, "to_json")...` check before the `as_dict` one.
  While this is the simplest solution, it adds an implicit assumption in the code that
  will be easily missed on future refactorings of this function.
* Making sure that the results of calling `as_dict` are still JSON safe.
  This would cause an infinite recursion on DAEmptys, and either wouldn't fix error
  or would result in max level nested entries for DAEmptys.
* Add a `as_dict` function to DAEmpty. This would work, but `as_dict` isn't used
  elsewhere in docassemble code much.
@BryceStevenWilley
Copy link
Copy Markdown
Contributor Author

Also just now realizing that this was addressed by taking the 3rd option (add as_dict to DAEmpty) in d36a609 and 25240c6 (changes to_dict to as_dict). Tested and it works for my cases. Closing

@BryceStevenWilley BryceStevenWilley deleted the daempty_json_serialization_issue branch March 5, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant