quart.json.provider module
- class quart.json.provider.DefaultJSONProvider(app: Quart)
Bases:
quart.json.provider.JSONProviderProvide JSON operations using Python’s built-in
jsonlibrary. Serializes the following additional data types: -datetime.datetimeanddatetime.dateareserialized to RFC 822 strings. This is the same as the HTTP date format.
uuid.UUIDis serialized to a string.dataclasses.dataclassis 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, orNoneout of debug mode, theresponse()output will not add indentation, newlines, or spaces. IfFalse, orNonein 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_keysattributes.- 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
Responseobject with it. The response mimetype will be “application/json” and can be changed withmimetype. IfcompactisFalseor 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,Noneis 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:
objectA 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
Quartand setjson_provider_classto a provider class, or setapp.jsonto an instance of the class. :param app: An application instance. This will be stored as aweakref.proxyon the_appattribute.- 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
Responseobject with theapplication/jsonmimetype.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,Noneis 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.