Title: Centralize all `de_json`'s into `TelegramObject` · Issue #5189 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: Centralize all `de_json`'s into `TelegramObject` · Issue #5189 · python-telegram-bot/python-telegram-bot
X Title: Centralize all `de_json`'s into `TelegramObject` · Issue #5189 · python-telegram-bot/python-telegram-bot
Description: What kind of feature are you missing? Where do you notice a shortcoming of PTB? Something I've wanted to do for a while is reduce duplication of de_json throughout the library. As of today, de_json is typically doing 4 things: Converting...
Open Graph Description: What kind of feature are you missing? Where do you notice a shortcoming of PTB? Something I've wanted to do for a while is reduce duplication of de_json throughout the library. As of today, de_json...
X Description: What kind of feature are you missing? Where do you notice a shortcoming of PTB? Something I've wanted to do for a while is reduce duplication of de_json throughout the library. As of today, de_...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/5189
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Centralize all `de_json`'s into `TelegramObject`","articleBody":"### What kind of feature are you missing? Where do you notice a shortcoming of PTB?\n\nSomething I've wanted to do for a while is reduce duplication of `de_json` throughout the library. As of today, `de_json` is typically doing 4 things:\n\n- Converting unix timestamps to datetimes\n- Converting dict -> TelegramObject's obviously\n- Handling Sequence inputs with `TO.de_list`\n- Decide which class would be created depending on a field like `type` (i.e. `ChatMember` classes and the like). I'll hereby refer to these kinds as _`_de_json` delegators_.\n\n### Describe the solution you'd like\n\nI propose handling `de_json` by doing type introspection of classes. With that, one can know what kind of _transformation_ (i.e. lambdas) to apply for every field. \n\nType inspection, however, is an expensive operation on every `de_json` call. But if you combine that with per class caching, there would be ~no runtime cost of this approach compared to what we have today.\n\n## Implementation details\n\n### The primary cache\n\nThe basic idea is to introduce a class variable, `__DE_JSON_PLAN__`, which is going to be the per-class dict cache of field names -> transformations. This will be populated on the _first_ call to `de_json` for that particular class. `de_json` will then simply iterate through the data and apply the transformation if it's needed.\n\n### Handling `de_json` delegators\n\nFor these classes, we'll still have to manually define a mapping as another class variable (`__DE_JSON_DISPATCH__`). This would be telling `de_json` what field name to delegate on, e.g. `status` or `type`, and the mapping of that value -> class. On every `de_json` call, this mapping is used to decide which class is going to be instantiated.\n\n### Supporting deprecated fields from the API\n\n`de_json` currently also removes a few deprecated fields like `can_send_media_messages` and places them into `api_kwargs`. With this implementation, a subclass can simply define another class variable, `__REMOVED_API_FIELDS__`, which will hold that information. `de_json` will move the data to `api_kwargs`.\n\n### Describe alternatives you've considered\n\nUsing another (de)serialization library like mentioned in #2698. Adding another third party dependency however is not very appealing, specially in this day and age.\n\n### Additional context\n\nI have a draft PoC up at #5186. Initial performance benchmarks show -15% to a +20% performance gain. See that PR description for details on that benchmark\n\nAny thoughts / alternative approaches are welcome!","author":{"url":"https://github.com/harshil21","@type":"Person","name":"harshil21"},"datePublished":"2026-03-31T10:29:22Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/5189"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9d42f071-ccd8-4e97-f0ce-b177af499f17 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BED6:13B36F:421A0D:52A4E4:6B02A22E |
| html-safe-nonce | bf90a6a62e14b77c9e22b75bd0bf9feb01036029194daa97532ab6bf996c8cf9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRUQ2OjEzQjM2Rjo0MjFBMEQ6NTJBNEU0OjZCMDJBMjJFIiwidmlzaXRvcl9pZCI6IjM0NjI5NTA5MzYyMDM0MDM4MjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fbdf3191e7ffa58dd32784b6eba0186c9423bd0a8e2d66cf89fec646b6be3a60 |
| hovercard-subject-tag | issue:4177981660 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/5189/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bbd1190159761568cbcf721f07865a3574564dbca84fa580904df6df7cc9899c/python-telegram-bot/python-telegram-bot/issues/5189 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bbd1190159761568cbcf721f07865a3574564dbca84fa580904df6df7cc9899c/python-telegram-bot/python-telegram-bot/issues/5189 |
| og:image:alt | What kind of feature are you missing? Where do you notice a shortcoming of PTB? Something I've wanted to do for a while is reduce duplication of de_json throughout the library. As of today, de_json... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | harshil21 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width