SimpleJSON and NumPy array

后端 未结 9 1388
挽巷
挽巷 2020-12-04 10:56

What is the most efficient way of serializing a numpy array using simplejson?

9条回答
  •  误落风尘
    2020-12-04 11:31

    This shows how to convert from a 1D NumPy array to JSON and back to an array:

    try:
        import json
    except ImportError:
        import simplejson as json
    import numpy as np
    
    def arr2json(arr):
        return json.dumps(arr.tolist())
    def json2arr(astr,dtype):
        return np.fromiter(json.loads(astr),dtype)
    
    arr=np.arange(10)
    astr=arr2json(arr)
    print(repr(astr))
    # '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
    dt=np.int32
    arr=json2arr(astr,dt)
    print(repr(arr))
    # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    

    Building on tlausch's answer, here is a way to JSON-encode a NumPy array while preserving shape and dtype of any NumPy array -- including those with complex dtype.

    class NDArrayEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, np.ndarray):
                output = io.BytesIO()
                np.savez_compressed(output, obj=obj)
                return {'b64npz' : base64.b64encode(output.getvalue())}
            return json.JSONEncoder.default(self, obj)
    
    
    def ndarray_decoder(dct):
        if isinstance(dct, dict) and 'b64npz' in dct:
            output = io.BytesIO(base64.b64decode(dct['b64npz']))
            output.seek(0)
            return np.load(output)['obj']
        return dct
    
    # Make expected non-contiguous structured array:
    expected = np.arange(10)[::2]
    expected = expected.view('

提交回复
热议问题