Subclass of standard Django JSON encoder1 that can encode some numpy types (integers, floats and arrays).
- Supported Python versions are:
3.6
,3.7
and3.7-dev
. - Supported Django versions are:
2.0
and2.1
(version1.11
should also work).
pip install --upgrade django-numpy-json-encoder
# -*- encoding: utf-8 -*-
# ! python3
import numpy as np
from django.http import JsonResponse
from django.views import View
from django_numpy_json_encoder.numpy_encoder import NumpyJSONEncoder
class ExampleNumpyArrayJSONView(View):
# noinspection PyMethodMayBeStatic
def post(self, *args, **kwargs):
numpy_array = np.random.rand(8, 42).astype(np.float32)
return JsonResponse(data={'array': numpy_array},
encoder=NumpyJSONEncoder,
safe=True)
The MIT License (MIT)