Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Easily json dump types and classes

Project description


[![Build Status](](


pip install moarjson

Basic usage

import moarjson as json

from datetime import datetime

json.dumps( # Fails

# Tell json to convert datetime objects to str first
json.register(datetime, str)

json.dumps( # Success

moarjson keeps track of all the types you want to encode to json.

import moarjson as json

from datetime import datetime

# Also available as decorator
def convert_datetime(obj):
return obj.strftime(format='%Y-%m-%d %H:%M')

# OR with lambda
json.register(datetime, lambda x: x.strftime(format='%Y-%m-%d %H:%M'))

Django models

Django user model field:

from django.contrib.auth.models import User

def convert_user(user):
return {
'username': user.username

def user_view(request):
# Json encoded user object
json_dump = json.dumps(User.objects.get(pk=1))

return HttpResponse(json_dump, content_type='application/json')

User.objects.all() is an iterable, so register iterables that first.
Convert any iterable to a list::

import collections
json.register(collections.Iterable, list)

def user_list_view(request):
# List of users
json_dump = json.dumps(User.objects.all())

return HttpResponse(json_dump, content_type='application/json')


### Code validation

The code is valid if it passes **flake8**.
Imports are sorted with **isort**.

### Testing

For testing **py.test** is used.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for moarjson, version 0.3.4
Filename, size File type Python version Upload date Hashes
Filename, size moarjson-0.3.4-py2.py3-none-any.whl (4.4 kB) File type Wheel Python version 2.7 Upload date Hashes View hashes
Filename, size moarjson-0.3.4.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page