-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
518e62a
commit 38c10b4
Showing
3 changed files
with
68 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from typing import Any, Callable | ||
|
||
|
||
class EventEncoder(json.JSONEncoder): | ||
""" | ||
Encode an event in JSON. | ||
""" | ||
|
||
_convert_callable: Callable[[Any], Any] | ||
""" | ||
Function that will be called to serialize callables. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
convert_callable: Callable[[Any], Any], | ||
*args: Any, | ||
**kwargs: Any, | ||
): | ||
""" | ||
Create a new EventEncoder. | ||
Args: | ||
convert_callable: A function that will be called to serialize callables | ||
*args: Arguments to pass to the JSONEncoder constructor | ||
**kwargs: Args to pass to the JSONEncoder constructor | ||
""" | ||
super().__init__(*args, **kwargs) | ||
self._convert_callable = convert_callable | ||
|
||
def default(self, o: Any): | ||
if callable(o): | ||
return self._convert_callable(o) | ||
else: | ||
return super().default(o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters