Skip to main content

JSONTag

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

Attributes​

AttributeTypeDescription
serializer[TaggedJSONSerializer](taggedjsonserializer.md?sid=src_flask_json_tag_taggedjsonserializer)The TaggedJSONSerializer instance associated with this tagger.
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=src_flask_json_tag_taggedjsonserializer)
) - > None

Parameters​

NameTypeDescription
serializer[TaggedJSONSerializer](taggedjsonserializer.md?sid=src_flask_json_tag_taggedjsonserializer)The serializer instance to associate with this tagger.

Signature​

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

Parameters​

NameTypeDescription
serializer[TaggedJSONSerializer](taggedjsonserializer.md?sid=src_flask_json_tag_taggedjsonserializer)The serializer instance this tagger is associated with for handling nested serialization

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 inspect for type compatibility

Returns​

TypeDescription
boolTrue if the value matches the type handled by this tagger, 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 be transformed into a JSON-compatible format

Returns​

TypeDescription
t.AnyA JSON-serializable representation (e.g., dict, list, str) of the input value

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 raw JSON data extracted from the tagged structure

Returns​

TypeDescription
t.AnyThe reconstructed Python object of the original specific type

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 wrapped in a tagged JSON dictionary

Returns​

TypeDescription
dict[str, t.Any]A dictionary containing a single key (the tag) mapping to the JSON-serializable value