cdevents / sdk-python

Python SDK for CDEvents
Apache License 2.0
4 stars 8 forks source link

Translate to and from json #29

Open dan-han-101 opened 1 year ago

dan-han-101 commented 1 year ago

Expected Behavior

Users often interact with objects in json format.

Would it make sense to update this SDK so that users can easily transform json strings into objects whose types match those defined in the CDEvent spec?

We currently have some code that looks like this

some_json_str = '...' # this could be consumed from kafka or some http service

typed_msg = CDEventMsgFactory.from_json(some_json_str) 
if isinstance(typed_msg, Taskrunstarted):
       # do something...
elif isinstance(typed_msg, Taskrunfinished):
       # do something...
etc...        

Along with verifying that the json is valid, we can also do some additional checks, like checking enum values, as specified in the schema.

We can then also go back to json with something like

new_json_str = CDEventMsgFactory.to_json(typed_msg) 

Actual Behavior

We wrote our own internal libraries, but we think it would be good to share this code, for others to re-use.

Steps to Reproduce the Problem

n/a

Specifications