-
Notifications
You must be signed in to change notification settings - Fork 24
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
Publishing data with datetime throws TypeError #527
Comments
➤ Automation for Jira commented: The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3841 |
Hi, @Andrioden we will look into it. Meanwhile, can you try serializing it externally, sending it as an |
Link to reference DateTime serialization implementation - https://stackoverflow.com/a/22238613 |
@sacOO7 - Thanks for the quick response. Changing the data contract is not a good option as i use it for api http server->client data transfer as well. I also prefer str dates over int dates as part of the contracts as they are more readable/debuggable. Instead I went with the following workaround as hinted by you. EventPublisher - My server helper class class EventPublisher:
@staticmethod
def _json_serializer(obj: dict) -> str:
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
async def publish_to_all_async(self, name: str, data: dict) -> None:
_client = AblyRest(config.ably_server_api_key)
channel = _client.channels.get("global")
await channel.publish(name, json.dumps(data, default=self._json_serializer))
await _client.close()
EventSubscriber - My client web/javascript helper class export class EventSubscriber {
/**
* @param {string} apiKey
*/
static async init(apiKey) {
const ably = new Ably.Realtime.Promise(apiKey)
await ably.connection.once("connected")
console.log("Event subscriber connected!")
await ably.channels
.get("global")
.subscribe((message) => this.processMessage(message))
}
/**
* @param {Ably.Types.Message} message
*/
static processMessage(message) {
const dataJson = JSON.parse(message.data)
// Do stuff with data...
}
} Any permanent solution that you implement should:
|
Yeah, we will try to implement the same 👍 |
This is going to take a good amount of time looking at the wide use of |
Here should be a minimal script to reproduce it
ably_test.py
Run with
python .\ably_test.py
, throwsEnvironment:
┆Issue is synchronized with this Jira Task by Unito
The text was updated successfully, but these errors were encountered: