On 11/25/21 17:05, Stephan Hoyer wrote:
Hi Qianqian,
What is your concrete proposal for NumPy here?
Are you suggesting new methods or functions like to_json/from_json in NumPy itself?
that would work - either define a subclass of JSONEncoder to serialize ndarray and allow users to pass it to cls in json.dump, or, as you mentioned, define to_json/from_json like pandas DataFrame would save people from writing customized codes/formats.
I am also wondering if there is a more automated way to tell json.dump/dumps to use a default serializer for ndarray without using cls=...? I saw a SO post mentioned about a method called "__serialize__" in a class, but can't find it in the official doc. I am wondering if anyone is aware of the method defining a default json serializer in an object?
As far as I can tell, reading/writing in your custom JSON format already works with your jdata library.ideally, I was hoping the small jdata encoder/decoder functions can be integrated into numpy; it can help avoid the "TypeError: Object of type ndarray is not JSON serializable" in json.dump/dumps without needing additional modules; more importantly, it simplifies users experience in exchanging complex arrays (complex valued, sparse, special shapes) with other programming environments.