Package: drf-orjson-renderer
Django Rest Framework ORJSON Rendererdrf_orjson_rendereris JSON renderer and parser for Django Rest Framework
using theorjsonlibrary. Backed byRust, orjson is safe, correct andfast. ⚡️In addition, unlike some performance optimized DRF renderers, It also renders
pretty printed JSON when requests are made via RestFramework's BrowsableAPI.You get:The safety of RustThe speed of orjson when requests are made withAccept: appliation/jsonHTTP
header or when requests are made with an unspecifiedAcceptheader.The convenience of formatted output when requested withAccept: text/html.The ability to pass your owndefaultfunction definition.Installationpip install drf_orjson_rendererYou can then set theORJSONRendererclass as your default renderer in yoursettings.pyREST_FRAMEWORK={"DEFAULT_RENDERER_CLASSES":("drf_orjson_renderer.renderers.ORJSONRenderer","rest_framework.renderers.BrowsableAPIRenderer",),}To modify how data is serialized, specify options in yoursettings.pyREST_FRAMEWORK={"ORJSON_RENDERER_OPTIONS":(orjson.OPT_NON_STR_KEYS,orjson.OPT_SERIALIZE_DATACLASS,orjson.OPT_SERIALIZE_NUMPY,),}Also you can set theORJSONParserclass as your default parser in yoursettings.pyREST_FRAMEWORK={"DEFAULT_PARSER_CLASSES":("drf_orjson_renderer.parsers.ORJSONParser",),}Passing Your OwndefaultFunctionBy default, theORJSONRendererwill pass adefaultfunction as a helper for
serializing objects that orjson doesn't recognize. That should cover the most
common cases found in a Django web application. If you find you have an object
it doesn't recognize you can pass your own default function by overriding theget_renderer_context()method of your view:fromrest_framework.viewsimportAPIViewfromrest_framework.responseimportResponseclassMyView(APIView):defdefault(self,obj):ifisinstance(obj,MyComplexData):returndict(obj)defget_renderer_context(self):renderer_context=super().get_renderer_context()renderer_context["default_function"]=self.defaultreturnrenderer_contextdefget(self,request,*args,**kwargs):my_complex_data=MyComplexData()returnResponse(data=my_complex_data)If you know your data is already in a format orjson nativelyrecognizesyou can get a small
performance boost by passingNoneto therenderer_context:defget_renderer_context(self):renderer_context=super().get_renderer_context()renderer_context["default_function"]=Nonereturnrenderer_contextAs of ORJSON version 3, 2-space indenting is supported in serialization. In
order to take advantage of the RestFramework Browsable API, when the
requested media type is notapplication/json, the ORJSON renderer will addorjson.OPT_INDENT_2to the options mask to pretty print your output.NumpyWhen this package was originally written ORJSON did not natively support
serializing numpy types. This package provided an encoder class that
overrides the DjangoJSONEncoder with support for numpy types. This encoder
is no longer necessary but included for backwards compatibility.fromdrf_orjson_renderer.encodersimportDjangoNumpyJSONEncoderfromrest_framework.viewsimportAPIViewclassMyView(APIView):defget_renderer_context(self):renderer_context=super().get_renderer_context()renderer_context["django_encoder_class"]=DjangoNumpyJSONEncoderreturnrenderer_contextBenchmarksSee theorjson Benchmarksfor more information