What is the most efficient way of serializing a numpy array using simplejson?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’d use
simplejson.dumps(somearray.tolist())as the most convenient approach (if I was still usingsimplejsonat all, which implies being stuck with Python 2.5 or earlier; 2.6 and later have a standard library modulejsonwhich works the same way, so of course I’d use that if the Python release in use supported it;-).In a quest for greater efficiency, you could subclass json.JSONEncoder (in
json; I don’t know if the oldersimplejsonalready offered such customization possibilities) and, in thedefaultmethod, special-case instances ofnumpy.arrayby turning them into list or tuples “just in time”. I kind of doubt you’d gain enough by such an approach, in terms of performance, to justify the effort, though.