[GH-ISSUE #940] Provide optional Encoder argument to CacheFileHandler #566

Closed
opened 2026-02-27 23:23:24 +03:00 by kerem · 0 comments
Owner

Originally created by @shawncruz on GitHub (Feb 12, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/940

Is your feature request related to a problem? Please describe.

Currently, the CacheFileHandler implementation of save_token_to_cache doesn't include an option for a custom serializer. The problem with this is that every object that's passed in must be serializable by the default JSONEncoder. However, in my use-case, I'm working with an object that has a value type decimal.Decimal, which is not JSON serializable out of the box. For example:

cache_handler = CacheFileHandler()
cache_handler.save_token_to_cache({"testing": decimal.Decimal(123)})

Will result in a TypeError: TypeError: Object of type Decimal is not JSON serializable

Describe the solution you'd like
A clear and concise description of what you want to happen.

Provide an optional argument to CacheFileHandler called encoder_cls that provides a custom encoder implementation for the token object provided to save_token_to_cache:

class CacheFileHandler(CacheHandler):
    def __init__(self,
                 cache_path=None,
                 username=None,
                 encoder_cls=None): # new argument

    def save_token_to_cache(self, token_info):
        try:
            f = open(self.cache_path, "w")
            f.write(json.dumps(token_info, cls=self.encoder_cls))
            f.close()
        except IOError:
            logger.warning('Couldn\'t write token to cache at: %s',
                           self.cache_path)

Describe alternatives you've considered

An alternative to this is to have the client encode with a custom encoder, then decode before passing into save_token_to_cache. The save_token_to_cache will then encode a second time:

# Client code

class CustomEncoder(json.JSONEncoder):
    # custom encoder implementation ...

cache_handler.save_token_to_cache(json.loads(json.dumps(token, cls=CustomEncoder))
Originally created by @shawncruz on GitHub (Feb 12, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/940 **Is your feature request related to a problem? Please describe.** Currently, the `CacheFileHandler` implementation of `save_token_to_cache` doesn't include an option for a custom serializer. The problem with this is that every object that's passed in must be serializable by the default JSONEncoder. However, in my use-case, I'm working with an object that has a value type decimal.Decimal, which is not JSON serializable out of the box. For example: ```python cache_handler = CacheFileHandler() cache_handler.save_token_to_cache({"testing": decimal.Decimal(123)}) ``` Will result in a TypeError: `TypeError: Object of type Decimal is not JSON serializable` **Describe the solution you'd like** A clear and concise description of what you want to happen. Provide an optional argument to `CacheFileHandler` called `encoder_cls` that provides a custom encoder implementation for the token object provided to `save_token_to_cache`: ```python class CacheFileHandler(CacheHandler): def __init__(self, cache_path=None, username=None, encoder_cls=None): # new argument def save_token_to_cache(self, token_info): try: f = open(self.cache_path, "w") f.write(json.dumps(token_info, cls=self.encoder_cls)) f.close() except IOError: logger.warning('Couldn\'t write token to cache at: %s', self.cache_path) ``` **Describe alternatives you've considered** An alternative to this is to have the client encode with a custom encoder, then decode before passing into `save_token_to_cache`. The `save_token_to_cache` will then encode a second time: ```python # Client code class CustomEncoder(json.JSONEncoder): # custom encoder implementation ... cache_handler.save_token_to_cache(json.loads(json.dumps(token, cls=CustomEncoder)) ```
kerem 2026-02-27 23:23:24 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#566
No description provided.