-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
kwarg for specifying date format as ISO date string instead of python datetime #3879
Comments
Just wondering did you tried to use custom json encoder import json
import datetime
class CustomJSONEncoder(json.JSONEncoder):
# For more details see: https://docs.python.org/3/library/json.html#module-json
def default(self, obj):
if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
return super().default(obj)
xxx = {"def": datetime.datetime(2021, 2, 1), "def2": datetime.date(2021, 2, 1), }
with open("custom.json", "w") as f:
f.write(json.dumps(xxx, cls=CustomJSONEncoder, indent=4)) |
Hi @dogversioning thanks for reaching out. I brought this feature request up for discussion with the team, and this isn't something that they plan to implement. There is overhead associated with adding and maintaining a new argument like this, and using workarounds like the one described above should be sufficient to address your use case. I'm going to close this as something that the team is not considering at this time but please feel free to add comments explaining other use cases to highlight why this is needed and maybe we can revisit the request at some point in the future. |
|
@tim-finnigan that makes sense to me - I haven't swung back around on this yet, but I get the tradeoff, and while I haven't tried it yet @Taragolis 's approach makes sense. Would you consider adding something like the above as a note in documentation? Someone else on my team expressed this same desire (which is why I opened this in the first place), and if there's a known workaround, it might help to gently point users in this direction. |
Thanks for following up — I think examples involving custom classes generally aren't documented. But if this is a common use case that you and others would benefit from seeing, maybe that could go here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/examples.html. You or someone else are welcome to create a PR for the maintainers to consider. |
Describe the feature
Adding an additional keyword argument (pehaps
DateFormat
with string values, orAsISODate
as a boolean) for calls retrieving dictionary configs from AWS resources. Would also be nice for create/update methods.Use Case
When working with configurations, and especially when writing them to disk in a human readable format, it is convenient to pipe these to a JSON object - this also creates a helpful way to test responses outside of boto (with the AWS CLI tool, or with raw HTTP posts, as examples). Since these currently come back as python datetimes, I have found myself recently doing a lot of schema inspection to identify date related keys, and then manually casting these to ISO date strings, before serializing out to JSON. As an example, with Glue tables:
It would be helpful to be able to specify which form these dates were returned in.
Proposed Solution
From an end user's perspective, I think this would make the most sense as a session/client-level argument? I think you'd generally want one format or the other, depending on your use case.
Other Information
No response
Acknowledgements
SDK version used
1.28.56
Environment details (OS name and version, etc.)
MacOS
The text was updated successfully, but these errors were encountered: