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] = _defaultApply this function to any object that json.dumps does not know how to serialize.
ensure_asciiboolean = trueReplace non-ASCII characters with escape sequences.
sort_keysboolean = trueSort the keys in any serialized dicts.
compact`boolNone` = null
mimetypestring = application/jsonThe mimetype set in response.

Constructor

Signature

def DefaultJSONProvider()

Methods


dumps()

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

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

Parameters

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

Returns

TypeDescription
stringA JSON-formatted string representing the input object

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 json.loads.

Returns

TypeDescription
t.AnyThe Python object representation of the JSON input

response()

@classmethod
def response(
args: t.Any,
kwargs: t.Any
) - > [Response](../../wrappers/response.md?sid=src_flask_wrappers_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 with mimetype.

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=src_flask_wrappers_response)A Flask Response object containing the serialized JSON data and the configured mimetype