mirror of
https://github.com/sprockets/sprockets.mixins.mediatype.git
synced 2024-12-29 11:17:10 +00:00
Add sprockets.mixins.mediatype.transcoders.JSONTranscoder.
This commit is contained in:
parent
d7f1bb1e4e
commit
ee8f645a51
4 changed files with 67 additions and 4 deletions
|
@ -19,3 +19,10 @@ Content Type Registration
|
|||
|
||||
.. autoclass:: ContentSettings
|
||||
:members:
|
||||
|
||||
Bundled Transcoders
|
||||
-------------------
|
||||
.. currentmodule:: sprockets.mixins.mediatype.transcoders
|
||||
|
||||
.. autoclass:: JSONTranscoder
|
||||
:members:
|
||||
|
|
|
@ -6,6 +6,7 @@ Version History
|
|||
- Repackage from a module into a package. Distributing raw modules inside
|
||||
of a namespace package is unreliable and questionably correct.
|
||||
- Add :func:`sprockets.mixins.mediatype.content.add_transcoder`.
|
||||
- Add :class:`sprockets.mixins.mediatype.transcoders.JSONTranscoder`
|
||||
|
||||
`1.0.4`_ (14 Sep 2015)
|
||||
----------------------
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import json
|
||||
import logging
|
||||
import signal
|
||||
|
||||
from sprockets.mixins.mediatype import content
|
||||
from sprockets.mixins.mediatype import content, transcoders
|
||||
from tornado import ioloop, web
|
||||
import msgpack
|
||||
|
||||
|
@ -22,8 +21,8 @@ def make_application(**settings):
|
|||
encoding='utf-8')
|
||||
content.add_binary_content_type(application, 'application/msgpack',
|
||||
msgpack.packb, msgpack.unpackb)
|
||||
content.add_text_content_type(application, 'application/json', 'utf-8',
|
||||
json.dumps, json.loads)
|
||||
content.add_transcoder(application, 'application/json',
|
||||
transcoders.JSONTranscoder())
|
||||
return application
|
||||
|
||||
|
||||
|
|
56
sprockets/mixins/mediatype/transcoders.py
Normal file
56
sprockets/mixins/mediatype/transcoders.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
"""Bundled media type transcoders."""
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from sprockets.mixins.mediatype import handlers
|
||||
|
||||
|
||||
class JSONTranscoder(handlers.TextContentHandler):
|
||||
"""
|
||||
JSON transcoder instance.
|
||||
|
||||
:param str content_type: the content type that this encoder instance
|
||||
implements. If omitted, ``application/json`` is used. This is
|
||||
passed directly to the ``TextContentHandler`` initializer.
|
||||
:param str default_encoding: the encoding to use if none is specified.
|
||||
If omitted, this defaults to ``utf-8``. This is passed directly to
|
||||
the ``TextContentHandler`` initializer.
|
||||
|
||||
.. attribute:: dump_options
|
||||
|
||||
Keyword parameters that are passed to :func:`json.dumps` when
|
||||
:meth:`.dumps` is called.
|
||||
|
||||
.. attribute:: load_options
|
||||
|
||||
Keyword parameters that are passed to :func:`json.loads` when
|
||||
:meth:`.loads` is called.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, content_type='application/json',
|
||||
default_encoding='utf-8'):
|
||||
super(JSONTranscoder, self).__init__(content_type, self.dumps,
|
||||
self.loads, default_encoding)
|
||||
self.dump_options = {}
|
||||
self.load_options = {}
|
||||
|
||||
def dumps(self, obj):
|
||||
"""
|
||||
Dump a :class:`object` instance into a JSON :class:`str`
|
||||
|
||||
:param object obj: the object to dump
|
||||
:return: the JSON representation of :class:`object`
|
||||
|
||||
"""
|
||||
return json.dumps(obj, **self.dump_options)
|
||||
|
||||
def loads(self, str_repr):
|
||||
"""
|
||||
Transform :class:`str` into an :class:`object` instance.
|
||||
|
||||
:param str str_repr: the UNICODE representation of an object
|
||||
:return: the decoded :class:`object` representation
|
||||
|
||||
"""
|
||||
return json.loads(str_repr, **self.load_options)
|
Loading…
Reference in a new issue