SimpleJSON and NumPy array

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

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

9条回答
  •  盖世英雄少女心
    2020-12-04 11:31

    You can also answer this with just a function passed into json.dumps in this way:

    json.dumps(np.array([1, 2, 3]), default=json_numpy_serializer)
    

    With

    import numpy as np
    
    def json_numpy_serialzer(o):
        """ Serialize numpy types for json
    
        Parameters:
            o (object): any python object which fails to be serialized by json
    
        Example:
    
            >>> import json
            >>> a = np.array([1, 2, 3])
            >>> json.dumps(a, default=json_numpy_serializer)
    
        """
        numpy_types = (
            np.bool_,
            # np.bytes_, -- python `bytes` class is not json serializable     
            # np.complex64,  -- python `complex` class is not json serializable  
            # np.complex128,  -- python `complex` class is not json serializable
            # np.complex256,  -- special handling below
            # np.datetime64,  -- python `datetime.datetime` class is not json serializable
            np.float16,
            np.float32,
            np.float64,
            # np.float128,  -- special handling below
            np.int8,
            np.int16,
            np.int32,
            np.int64,
            # np.object_  -- should already be evaluated as python native
            np.str_,
            np.timedelta64,
            np.uint8,
            np.uint16,
            np.uint32,
            np.uint64,
            np.void,
        )
    
        if isinstance(o, np.ndarray):
            return o.tolist()
        elif isinstance(o, numpy_types):        
            return o.item()
        elif isinstance(o, np.float128):
            return o.astype(np.float64).item()
        # elif isinstance(o, np.complex256): -- no python native for np.complex256
        #     return o.astype(np.complex128).item() -- python `complex` class is not json serializable 
        else:
            raise TypeError("{} of type {} is not JSON serializable".format(repr(o), type(o)))
    

    validated:

    need_addition_json_handeling = (
        np.bytes_,
        np.complex64,  
        np.complex128, 
        np.complex256, 
        np.datetime64,
        np.float128,
    )
    
    
    numpy_types = tuple(set(np.typeDict.values()))
    
    for numpy_type in numpy_types:
        print(numpy_type)
    
        if numpy_type == np.void:
            # complex dtypes evaluate as np.void, e.g.
            numpy_type = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))])
        elif numpy_type in need_addition_json_handeling:
            print('python native can not be json serialized')
            continue
    
        a = np.ones(1, dtype=nptype)
        json.dumps(a, default=json_numpy_serialzer)
    

提交回复
热议问题