cloudevents / sdk-python

Python SDK for CloudEvents
https://pypi.org/p/cloudevents/
Apache License 2.0
284 stars 56 forks source link

Method to_structured can't control NON-ASCII chars #206

Open stamog opened 1 year ago

stamog commented 1 year ago

Expected Behavior

I am using to_structured() but there are non-ascii symbols that I want to have included. I cannot alter its behaviour.

Actual Behavior

to_structured method returns them in the form of '\u00'

Steps to Reproduce the Problem

  1. Try to pass an event containing a non-ascii symbol (Ø, ü, etc.)
  2. Print the result

Specifications

pcaldeira-md commented 1 year ago

Could anyone take a look at this? It would be nice to be able to pass a parameter like ensure_ascii=False on the methods serializing the cloud events into a JSON format.

xSAVIKx commented 1 year ago

Hey, sorry, I seemed to miss this one. I'll try to spend some time over the next couple of days, but no ETA. PRs are very welcome though :-)

pcaldeira-md commented 1 year ago

Great, thanks! 🙏

xSAVIKx commented 1 year ago

OK, this is unfortunately not possible right now as well as it is not possible to modify any JSON-related conversions.

I'd very much like to support ensure_ascii, but it will also require support for all the other possible parameters. And also this kinda forces both producer and consumer to be aware of ASCII encoding while in Python at least json.dumps and json.loads have ensure_ascii=True by default.

I believe SDK should support more than just a couple of params but IDK when this can be implemented. Ideally, I'd like to provide a way to just supply custom to_json and from_json implementations throughout the whole SDK. This allows for achieving any required flexibility.

MaryamTaj commented 11 months ago

@xSAVIKx I am interested in working on this issue, and I am trying to understand it a little better. In the comment above, you mentioned that it would be ideal to have a custom to_json, and from_json implementation throughout the whole SDK. However, in the link below, doesn't the data_marshaller parameter act as a custom implementation? https://github.com/cloudevents/sdk-python/blob/21572afb579df15b386461b740340ba912270dbf/cloudevents/conversion.py#L62