From: Armin Ronacher Date: Fri, 6 Jan 2017 20:33:51 +0000 (+0100) Subject: Ported tojson filter. Fixes #458 X-Git-Tag: 2.9~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e71a130607fe5d21e2ee2544f4c6c90e8d5591b5;p=thirdparty%2Fjinja.git Ported tojson filter. Fixes #458 --- diff --git a/CHANGES b/CHANGES index 62ca6b08..5f69137d 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,8 @@ Version 2.9 the string is barely truncated at all. - Change the logic for macro autoescaping to be based on the runtime autoescaping information at call time instead of macro define time. +- Ported a modified version of the `tojson` filter from Flask to Jinja2 + and hooked it up with the new policy framework. Version 2.8.2 ------------- diff --git a/docs/api.rst b/docs/api.rst index 107acd65..8bf0fdfe 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -565,6 +565,18 @@ Example:: The default target that is issued for links from the `urlize` filter if no other target is defined by the call explicitly. +``json.dumps_function``: + If this is set to a value other than `None` then the `tojson` filter + will dump with this function instead of the default one. Note that + this function should accept arbitrary extra arguments which might be + passed in the future from the filter. Currently the only argument + that might be passed is `indent`. The default dump function is + ``json.dumps``. + +``json.dumps_kwargs``: + Keyword arguments to be passed to the dump function. The default is + ``{'sort_keys': True}``. + Utilities --------- diff --git a/jinja2/defaults.py b/jinja2/defaults.py index bdb538d5..90ccb65f 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -41,8 +41,10 @@ DEFAULT_NAMESPACE = { # default policies DEFAULT_POLICIES = { - 'urlize.rel': 'noopener', - 'urlize.target': None, + 'urlize.rel': 'noopener', + 'urlize.target': None, + 'json.dumps_function': None, + 'json.dumps_kwargs': {'sort_keys': True}, } diff --git a/jinja2/filters.py b/jinja2/filters.py index 33e0ff14..05c2fc45 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -15,7 +15,7 @@ from random import choice from itertools import groupby from collections import namedtuple from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \ - unicode_urlencode + unicode_urlencode, htmlsafe_json_dumps from jinja2.runtime import Undefined from jinja2.exceptions import FilterArgumentError from jinja2._compat import imap, string_types, text_type, iteritems @@ -916,6 +916,39 @@ def do_rejectattr(*args, **kwargs): return select_or_reject(args, kwargs, lambda x: not x, True) +@evalcontextfilter +def do_tojson(eval_ctx, value, indent=None): + """Dumps a structure to JSON so that it's safe to use in ``