-
-
Notifications
You must be signed in to change notification settings - Fork 223
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
JSONRenderer(default=custom_json_fallback_handler)
results in TypeError: dumps() got multiple values for keyword argument 'default'
#77
Comments
Hmm I think I had to switch from At this point JSONEncoder is 2 LoC so I don't want to do too much magic around it, since it’s easier to write an own one. That said I’ve been thinking about a better serialization for a while. I haven’t used single dispatch but would that mean that I provide something like “to_dict” and users can do a “@to_dict.register” on top of it? That actually sounds pretty great if it’d work! |
That makes sense, subclassing json.JSONEncoder probably doesn't affect other json libraries. That's basically it, they would do something like For the case at hand, |
But man, I feel like we’re on to something here because the mechanism of default fallbacks in JSON is something that filled me with rage and frustration since I’ve started using the I’ve just looked it up and found a def to_serializable(val):
"""
Transform *val* into something that can be serialized to JSON. Falls back
to str.
"""
if isinstance(val, datetime):
return val.isoformat() + "Z"
elif isinstance(val, enum.Enum):
return val.value
elif attr.has(val.__class__):
return attr.asdict(val)
elif isinstance(val, Exception):
return {
"error": val.__class__.__name__,
"args": val.args,
}
return str(val) in one of my projects (it is used both by structlog but also for web views). I’ve basically re-implemented single dispatch in a central place. I’ve just rewritten it as: @singledispatch
def to_serializable(val):
"""
Transform *val* into something that can be serialized to JSON.
"""
if attr.has(val.__class__):
return attr.asdict(val)
return str(val)
@to_serializable.register(datetime)
def s_datetime(val):
return val.isoformat() + "Z"
@to_serializable.register(enum.Enum)
def s_enum(val):
return val.value
@to_serializable.register(Exception)
def s_exc(val):
return {
"error": val.__class__.__name__,
"args": val.args,
} which works just as good but is nicely decentralized. :) |
I use the
JSONEncoder()
default argument to handle rendering datetime using ISO format. With 9dd4d65JSONRenderer()
provides adefault
keyword argument, and providing my own results in theTypeError
I described in the title.This could be fixed by having
JSONRenderer
mix the_json_fallback_handler
default intoself._dumps_kw
.Is there another way I should be doing this? I already use the
TimeStamper
processor, but I have other datetime objects that I would prefer not to render as repr. Maybe structlog could provide afunctools.singledispatch
callable as an alternative to__structlog__
methods?The text was updated successfully, but these errors were encountered: