Skip to content
Myr

update tojson docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent fcf014f · Browse files · 2 files+43 added−43 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+43 added−43 removed

  • other:+43 added−43 removed

2 files+43 added−43 removed

modified:src/jinja2/filters.py+16 added-24 removed
1249 unmodified lines
125012511252@evalcontextfilter1253def do_tojson(eval_ctx, value, indent=None):1254 """Dumps a structure to JSON so that it's safe to use in ``<script>``1255 tags. It accepts the same arguments and returns a JSON string. Note that1256 this is available in templates through the ``|tojson`` filter which will1257 also mark the result as safe. Due to how this function escapes certain1258 characters this is safe even if used outside of ``<script>`` tags.1254 """Serialize an object to a string of JSON, and mark it safe to1255 render in HTML. This filter is only for use in HTML documents.12561260 The following characters are escaped in strings:1257 The returned string is safe to render in HTML documents and1258 ``<script>`` tags. The exception is in HTML attributes that are1259 double quoted; either use single quotes or the ``|forceescape``1260 filter.12611262 - ``<``1263 - ``>``1264 - ``&``1265 - ``'``1262 :param value: The object to serialize to JSON.1263 :param indent: The ``indent`` parameter passed to ``dumps``, for1264 pretty-printing the value.12651267 This makes it safe to embed such strings in any place in HTML with the1268 notable exception of double quoted attributes. In that case single1269 quote your attributes or HTML escape it in addition.12701271 The indent parameter can be used to enable pretty printing. Set it to1272 the number of spaces that the structures should be indented with.12731274 Note that this filter is for use in HTML contexts only.12751266 .. versionadded:: 2.91267 """1268 policies = eval_ctx.environment.policies1279 dumper = policies["json.dumps_function"]1280 options = policies["json.dumps_kwargs"]1269 dumps = policies["json.dumps_function"]1270 kwargs = policies["json.dumps_kwargs"]12711272 if indent is not None:1282 options = dict(options)1283 options["indent"] = indent1284 return htmlsafe_json_dumps(value, dumper=dumper, **options)1273 kwargs = kwargs.copy()1274 kwargs["indent"] = indent12751276 return htmlsafe_json_dumps(value, dumps=dumps, **kwargs)127712781279def prepare_map(args, kwargs):1280 context = args[0]1281 seq = args[1]1282
115 unmodified lines
modified:src/jinja2/utils.py+27 added-19 removed
606 unmodified lines
607608 return autoescape609610611def htmlsafe_json_dumps(obj, dumper=None, **kwargs):612 """Works exactly like :func:`dumps` but is safe for use in ``<script>``613 tags. It accepts the same arguments and returns a JSON string. Note that614 this is available in templates through the ``|tojson`` filter which will615 also mark the result as safe. Due to how this function escapes certain616 characters this is safe even if used outside of ``<script>`` tags.611def htmlsafe_json_dumps(obj, dumps=None, **kwargs):612 """Serialize an object to a string of JSON with :func:`json.dumps`,613 then replace HTML-unsafe characters with Unicode escapes and mark614 the result safe with :class:`~markupsafe.Markup`.615618 The following characters are escaped in strings:616 This is available in templates as the ``|tojson`` filter.617620 - ``<``621 - ``>``622 - ``&``623 - ``'``618 The following characters are escaped: ``<``, ``>``, ``&``, ``'``.619625 This makes it safe to embed such strings in any place in HTML with the626 notable exception of double quoted attributes. In that case single627 quote your attributes or HTML escape it in addition.620 The returned string is safe to render in HTML documents and621 ``<script>`` tags. The exception is in HTML attributes that are622 double quoted; either use single quotes or the ``|forceescape``623 filter.624625 :param obj: The object to serialize to JSON.626 :param dumps: The ``dumps`` function to use. Defaults to627 ``env.policies["json.dumps_function"]``, which defaults to628 :func:`json.dumps`.629 :param kwargs: Extra arguments to pass to ``dumps``. Merged onto630 ``env.policies["json.dumps_kwargs"]``.631632 .. versionchanged:: 3.0633 The ``dumper`` parameter is renamed to ``dumps``.634635 .. versionadded:: 2.9636 """629 if dumper is None:630 dumper = json.dumps631 rv = (632 dumper(obj, **kwargs)637 if dumps is None:638 dumps = json.dumps639640 return Markup(641 dumps(obj, **kwargs)642 .replace("<", "\\u003c")643 .replace(">", "\\u003e")644 .replace("&", "\\u0026")645 .replace("'", "\\u0027")646 )638 return Markup(rv)647648649class Cycler:650 """Cycle through values by yield them one at a time, then restarting
96 unmodified lines