quart.json.provider module
- class quart.json.provider.DefaultJSONProvider(app: Quart)
Bases:
quart.json.provider.JSONProvider
Provide JSON operations using Python’s built-in
json
library. Serializes the following additional data types: -datetime.datetime
anddatetime.date
areserialized to RFC 822 strings. This is the same as the HTTP date format.
uuid.UUID
is serialized to a string.dataclasses.dataclass
is passed todataclasses.asdict()
.Markup
(or any object with a__html__
method) will call the__html__
method to get a string.
- compact: bool | None = None
If
True
, orNone
out of debug mode, theresponse()
output will not add indentation, newlines, or spaces. IfFalse
, orNone
in debug mode, it will use a non-compact representation.
- static default(object_: Any) Any
Apply this function to any object that
json.dumps()
does not know how to serialize. It should return a valid JSON type or raise aTypeError
.
- dumps(object_: Any, **kwargs: Any) str
Serialize data as JSON to a string. Keyword arguments are passed to
json.dumps()
. Sets some parameter defaults from thedefault
,ensure_ascii
, andsort_keys
attributes.- Parameters
object – The data to serialize.
kwargs – Passed to
json.dumps()
.
- ensure_ascii = True
Replace non-ASCII characters with escape sequences. This may be more compatible with some clients, but can be disabled for better performance and size.
- loads(object_: str | bytes, **kwargs: Any) Any
Deserialize data as JSON from a string or bytes.
- Parameters
object – Text or UTF-8 bytes.
kwargs – Passed to
json.loads()
.
- mimetype = 'application/json'
The mimetype set in
response()
.
- response(*args: Any, **kwargs: Any) Response
Serialize the given arguments as JSON, and return a
Response
object with it. The response mimetype will be “application/json” and can be changed withmimetype
. Ifcompact
isFalse
or debug mode is enabled, the output will be formatted to be easier to read. Either positional or keyword arguments can be given, not both. If no arguments are given,None
is serialized.- Parameters
args – A single value to serialize, or multiple values to treat as a list to serialize.
kwargs – Treat as a dict to serialize.
- sort_keys = True
Sort the keys in any serialized dicts. This may be useful for some caching situations, but can be disabled for better performance. When enabled, keys must all be strings, they are not converted before sorting.
- class quart.json.provider.JSONProvider(app: Quart)
Bases:
object
A standard set of JSON operations for an application. Subclasses of this can be used to customize JSON behavior or use different JSON libraries.
To implement a provider for a specific library, subclass this base class and implement at least
dumps()
andloads()
. All other methods have default implementations.To use a different provider, either subclass
Quart
and setjson_provider_class
to a provider class, or setapp.json
to an instance of the class. :param app: An application instance. This will be stored as aweakref.proxy
on the_app
attribute.- dump(object_: Any, fp: IO[str], **kwargs: Any) None
Serialize data as JSON and write to a file.
- Parameters
object – The data to serialize.
fp – A file opened for writing text. Should use the UTF-8 encoding to be valid JSON.
kwargs – May be passed to the underlying JSON library.
- dumps(object_: Any, **kwargs: Any) str
Serialize data as JSON.
- Parameters
object – The data to serialize.
kwargs – May be passed to the underlying JSON library.
- load(fp: IO, **kwargs: Any) Any
Deserialize data as JSON read from a file. :param fp: A file opened for reading text or UTF-8 bytes. :param kwargs: May be passed to the underlying JSON library.
- loads(object_: str | bytes, **kwargs: Any) Any
Deserialize data as JSON.
- Parameters
s – Text or UTF-8 bytes.
kwargs – May be passed to the underlying JSON library.
- response(*args: Any, **kwargs: Any) Response
Serialize the given arguments as JSON, and return a
Response
object with theapplication/json
mimetype.The
jsonify()
function calls this method for the current application. Either positional or keyword arguments can be given, not both. If no arguments are given,None
is serialized.- Parameters
args – A single value to serialize, or multiple values to treat as a list to serialize.
kwargs – Treat as a dict to serialize.