Skip to main content

JSONTag

Base class for defining type tags for :class:TaggedJSONSerializer.

Attributes

AttributeTypeDescription
keystr = ""The tag to mark the serialized object with. If empty, this tag is only used as an intermediate step during tagging.

Constructor

Signature

def JSONTag(
serializer: [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer)
) - > None

Parameters

NameTypeDescription
serializer[TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer)

Methods


check()

@classmethod
def check(
value: t.Any
) - > bool

Check if the given value should be tagged by this tag.

Parameters

NameTypeDescription
valuet.AnyThe Python object to check for tagging suitability.

Returns

TypeDescription
boolReturns True if the value should be tagged by this tag, False otherwise.

to_json()

@classmethod
def to_json(
value: t.Any
) - > t.Any

Convert the Python object to an object that is a valid JSON type. The tag will be added later.

Parameters

NameTypeDescription
valuet.AnyThe Python object to convert to a JSON-compatible type.

Returns

TypeDescription
t.AnyThe JSON-serializable representation of the Python object.

to_python()

@classmethod
def to_python(
value: t.Any
) - > t.Any

Convert the JSON representation back to the correct type. The tag will already be removed.

Parameters

NameTypeDescription
valuet.AnyThe JSON representation of the object to convert back to a Python type.

Returns

TypeDescription
t.AnyThe Python object reconstructed from its JSON representation.

tag()

@classmethod
def tag(
value: t.Any
) - > dict[str, t.Any]

Convert the value to a valid JSON type and add the tag structure around it.

Parameters

NameTypeDescription
valuet.AnyThe Python object to be converted to a JSON type and tagged.

Returns

TypeDescription
dict[str, t.Any]A dictionary representing the tagged JSON object, with the tag key and the JSON-serializable value.