Skip to main content

Custom json serializers for the Starlette web framework.

Project description

Starlette Json

Introduction

Starlette json responses for various json serializers available in the python community.

Why:

  • Remove ujson dependency from core starlette package
  • Add adaptors for other serializers
  • Customize serializer rendering settings

Requirements

Installation

$ pip install starlette-json

Optional installs

Install at least one of these:

Usage

Response examples

from starlette.applications import Starlette
from starlette.responses import JSONResponse

from starlette_json import ORJsonResponse, UJsonResponse, RapidJsonResponse, SimpleJsonResponse

app = Starlette()
data = {'Hello': 'World'}

@app.route('/json')
def json(request):
	return JSONResponse(data)

@app.route('/orjson')
def orjson(request):
	return ORJsonResponse(data)

@app.route('/ujson')
def ujson(request):
	return UJsonResponse(data)

@app.route('/rapidjson')
def rapidjson(request):
	return RapidJsonResponse(data)

@app.route('/simplejson')
def rapidjson(request):
	return SimpleJsonResponse(data)

Custom response rendering options:

See the docs for the specific json serializer for available options

from starlette.applications import Starlette
from starlette_json import ORJsonResponse, UJsonResponse, RapidJsonResponse
import orjson

app = Starlette()
data = {'Hello': 'World'}

@app.route('/orjson')
def orjson(request):
	return ORJsonResponse(
		data,
		default=lambda x: str(x),
		option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC
	)

@app.route('/ujson')
def ujson(request):
	return UJsonResponse(
		data,
		encode_html_chars=True,
		ensure_ascii=False,
		escape_forward_slashes=False
	)

@app.route('/rapidjson')
def rapidjson(request):
	return RapidJsonResponse(data, sort_keys=True, indent=4)

@app.route('/simplejson')
def rapidjson(request):
	return SimpleJsonResponse(
		data,
		skipkeys=False,
		ensure_ascii=True,
		check_circular=True,
		allow_nan=True
	)

Json request body parsing:

from starlette.applications import Starlette
from starlette_json import ORJsonMiddleware, ORJsonResponse

app = Starlette()

app.add_middleware(ORJsonMiddleware)

@app.route('/orjson')
def orjson(request):
	body = await request.json() # Parsed with orjson
	return ORJsonResponse({'message':'ok'})

Contributing

PRs very welcome. CONTRIBUTING.md

Todo

  • Tests?

Project details


Download files

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

Source Distribution

starlette_json-20.9.18.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_json-20.9.18-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file starlette_json-20.9.18.tar.gz.

File metadata

  • Download URL: starlette_json-20.9.18.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.5

File hashes

Hashes for starlette_json-20.9.18.tar.gz
Algorithm Hash digest
SHA256 72bff092df261d6ca101660724ca7831c5e57c820e8447ede4ccceb339fab110
MD5 5afd71ed7ba254a6faeb8d7a64327a1f
BLAKE2b-256 fd2d25749c8fabb0f3446b5a66a086b2c3e2eb9529bb59127b69db2bdb8500d1

See more details on using hashes here.

File details

Details for the file starlette_json-20.9.18-py3-none-any.whl.

File metadata

  • Download URL: starlette_json-20.9.18-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.5

File hashes

Hashes for starlette_json-20.9.18-py3-none-any.whl
Algorithm Hash digest
SHA256 abe3f56e7d4a6ef454e5f8913098c8988d6aa0af0f9484e7b86f6ad446f75bb0
MD5 b23bfd0e0fb3d1034b7796e9d02e5245
BLAKE2b-256 6cbce261c6ef83ef19220568678dfd6a5ca7fe969dcdf22601fd73e84f8a95ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page