package documentation

Undocumented

Module tag Tagged JSON ~~~~~~~~~~~

From __init__.py:

Class ​JSONDecoder The default JSON decoder.
Class ​JSONEncoder The default JSON encoder. Handles extra types compared to the built-in json.JSONEncoder.
Function dump Serialize an object to JSON written to a file object.
Function dumps Serialize an object to a string of JSON.
Function jsonify Serialize data to JSON and wrap it in a ~flask.Response with the :mimetype:`application/json` mimetype.
Function load Deserialize an object from JSON read from a file object.
Function loads Deserialize an object from a string of JSON.
Variable dataclasses Undocumented
Function ​_dump​_arg​_defaults Inject default arguments for dump functions.
Function ​_load​_arg​_defaults Inject default arguments for load functions.
Function htmlsafe​_dump Serialize an object to JSON written to a file object, replacing HTML-unsafe characters with Unicode escapes. See htmlsafe_dumps and dumps.
Function htmlsafe​_dumps Serialize an object to a string of JSON with dumps, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with ~markupsafe.Markup.
dataclasses =

Undocumented

def _dump_arg_defaults(kwargs, app=None):
Inject default arguments for dump functions.
Parameters
kwargs:t.Dict[str, t.Any]Undocumented
app:t.Optional[Flask]Undocumented
def _load_arg_defaults(kwargs, app=None):
Inject default arguments for load functions.
Parameters
kwargs:t.Dict[str, t.Any]Undocumented
app:t.Optional[Flask]Undocumented
def dumps(obj, app=None, **kwargs):

Serialize an object to a string of JSON.

Takes the same arguments as the built-in json.dumps, with some defaults from application configuration.

Changed in version 2.0.2: decimal.Decimal is supported by converting to a string.
Changed in version 2.0: encoding is deprecated and will be removed in Flask 2.1.
Changed in version 1.0.3: app can be passed directly, rather than requiring an app context for configuration.
Parameters
obj:t.AnyObject to serialize to JSON.
app:t.Optional[Flask]Use this app's config instead of the active app context or defaults.
**kwargs:t.AnyExtra arguments passed to json.dumps.
Returns
strUndocumented
def dump(obj, fp, app=None, **kwargs):

Serialize an object to JSON written to a file object.

Takes the same arguments as the built-in json.dump, with some defaults from application configuration.

Changed in version 2.0: Writing to a binary file, and the encoding argument, is deprecated and will be removed in Flask 2.1.
Parameters
obj:t.AnyObject to serialize to JSON.
fp:t.IO[str]File object to write JSON to.
app:t.Optional[Flask]Use this app's config instead of the active app context or defaults.
**kwargs:t.AnyExtra arguments passed to json.dump.
def loads(s, app=None, **kwargs):

Deserialize an object from a string of JSON.

Takes the same arguments as the built-in json.loads, with some defaults from application configuration.

Changed in version 2.0: encoding is deprecated and will be removed in Flask 2.1. The data must be a string or UTF-8 bytes.
Changed in version 1.0.3: app can be passed directly, rather than requiring an app context for configuration.
Parameters
s:strJSON string to deserialize.
app:t.Optional[Flask]Use this app's config instead of the active app context or defaults.
**kwargs:t.AnyExtra arguments passed to json.loads.
Returns
t.AnyUndocumented
def load(fp, app=None, **kwargs):

Deserialize an object from JSON read from a file object.

Takes the same arguments as the built-in json.load, with some defaults from application configuration.

Changed in version 2.0: encoding is deprecated and will be removed in Flask 2.1. The file must be text mode, or binary mode with UTF-8 bytes.
Parameters
fp:t.IO[str]File object to read JSON from.
app:t.Optional[Flask]Use this app's config instead of the active app context or defaults.
**kwargs:t.AnyExtra arguments passed to json.load.
Returns
t.AnyUndocumented
def htmlsafe_dumps(obj, **kwargs):

Serialize an object to a string of JSON with dumps, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with ~markupsafe.Markup.

This is available in templates as the |tojson filter.

The returned string is safe to render in HTML documents and <script> tags. The exception is in HTML attributes that are double quoted; either use single quotes or the |forceescape filter.

Changed in version 2.0: Uses jinja2.utils.htmlsafe_json_dumps. The returned value is marked safe by wrapping in ~markupsafe.Markup.
Changed in version 0.10: Single quotes are escaped, making this safe to use in HTML, <script> tags, and single-quoted attributes without further escaping.
Parameters
obj:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
strUndocumented
def htmlsafe_dump(obj, fp, **kwargs):
Serialize an object to JSON written to a file object, replacing HTML-unsafe characters with Unicode escapes. See htmlsafe_dumps and dumps.
Parameters
obj:t.AnyUndocumented
fp:t.IO[str]Undocumented
**kwargs:t.AnyUndocumented
def jsonify(*args, **kwargs):

Serialize data to JSON and wrap it in a ~flask.Response with the :mimetype:`application/json` mimetype.

Uses dumps to serialize the data, but args and kwargs are treated as data rather than arguments to json.dumps.

  1. Single argument: Treated as a single value.
  2. Multiple arguments: Treated as a list of values. jsonify(1, 2, 3) is the same as jsonify([1, 2, 3]).
  3. Keyword arguments: Treated as a dict of values. jsonify(data=data, errors=errors) is the same as jsonify({"data": data, "errors": errors}).
  4. Passing both arguments and keyword arguments is not allowed as it's not clear what should happen.
from flask import jsonify

@app.route("/users/me")
def get_current_user():
    return jsonify(
        username=g.user.username,
        email=g.user.email,
        id=g.user.id,
    )

Will return a JSON response like this:

{
  "username": "admin",
  "email": "admin@localhost",
  "id": 42
}

The default output omits indents and spaces after separators. In debug mode or if JSONIFY_PRETTYPRINT_REGULAR is True, the output will be formatted to be easier to read.

Changed in version 2.0.2: decimal.Decimal is supported by converting to a string.
Changed in version 0.11: Added support for serializing top-level arrays. This introduces a security risk in ancient browsers. See :ref:`security-json`.
New in version 0.2.
Parameters
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
ResponseUndocumented