/django-numpy-json-encoder

Django JSON encoder with numpy support

Primary LanguagePythonMIT LicenseMIT

Django numpy JSON encoder

PyPi

MIT

TravisCI

Coverage

Supported Python implementations

Supported Python versions

Introduction

Subclass of standard Django JSON encoder1 that can encode some numpy types (integers, floats and arrays).

Installation

  • Supported Python versions are: 3.6, 3.7 and 3.7-dev.
  • Supported Django versions are: 2.0 and 2.1 (version 1.11 should also work).
pip install --upgrade django-numpy-json-encoder

Usage

# -*- 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)

License

The MIT License (MIT)


  1. https://docs.djangoproject.com/en/dev/topics/serialization/#djangojsonencoder