Skip to main content

DefaultJSONProvider

Provide JSON operations using Python's built-in :mod:json library. Serializes the following additional data types:

  • :class:datetime.datetime and :class:datetime.date are serialized to :rfc:822 strings. This is the same as the HTTP date format.
  • :class:uuid.UUID is serialized to a string.
  • :class:dataclasses.dataclass is passed to :func:dataclasses.asdict.
  • :class:~markupsafe.Markup (or any object with a __html__ method) will call the __html__ method to get a string.

Attributes

AttributeTypeDescription
defaultt.Callable[[t.Any], t.Any]Apply this function to any object that :meth:json.dumps does not know how to serialize. It should return a valid JSON type or raise a TypeError.
ensure_ascii= TrueReplace non-ASCII characters with escape sequences. This may be more compatible with some clients, but can be disabled for better performance and size.
sort_keys= TrueSort 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.
compact`boolNone` = None
mimetype= "application/json"The mimetype set in :meth:response.

Methods


dumps()

@classmethod
def dumps(
obj: t.Any,
kwargs: t.Any
) - > str

Serialize data as JSON to a string. Keyword arguments are passed to :func:json.dumps. Sets some parameter defaults from the :attr:default, :attr:ensure_ascii, and :attr:sort_keys attributes.

Parameters

NameTypeDescription
objt.AnyThe data to serialize.
kwargst.AnyPassed to :func:json.dumps.

Returns

TypeDescription
strThe JSON serialized string.

loads()

@classmethod
def loads(
s: str | bytes,
kwargs: t.Any
) - > t.Any

Deserialize data as JSON from a string or bytes.

Parameters

NameTypeDescription
s`strbytes`
kwargst.AnyPassed to :func:json.loads.

Returns

TypeDescription
t.AnyThe deserialized Python object.

response()

@classmethod
def response(
args: t.Any,
kwargs: t.Any
) - > [Response](../../wrappers/response.md?sid=flask_wrappers_response)

Serialize the given arguments as JSON, and return a :class:~flask.Response object with it. The response mimetype will be "application/json" and can be changed with :attr:mimetype. If :attr:compact is False 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

NameTypeDescription
argst.AnyA single value to serialize, or multiple values to treat as a list to serialize.
kwargst.AnyTreat as a dict to serialize.

Returns

TypeDescription
[Response](../../wrappers/response.md?sid=flask_wrappers_response)A Flask Response object containing the JSON serialized data.