diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index f93e799..06cf91c 100644
|
a
|
b
|
from pprint import pformat
|
| 9 | 9 | |
| 10 | 10 | from django.template.base import Variable, Library, VariableDoesNotExist |
| 11 | 11 | from django.conf import settings |
| 12 | | from django.utils import formats |
| | 12 | from django.utils import formats, simplejson |
| 13 | 13 | from django.utils.dateformat import format, time_format |
| 14 | 14 | from django.utils.encoding import force_unicode, iri_to_uri |
| 15 | 15 | from django.utils.html import (conditional_escape, escapejs, fix_ampersands, |
| … |
… |
def filesizeformat(bytes):
|
| 838 | 838 | return ugettext("%s TB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024)) |
| 839 | 839 | return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024)) |
| 840 | 840 | |
| | 841 | @register.filter(is_safe=True) |
| | 842 | def json(value): |
| | 843 | """ |
| | 844 | Returns the json representation of value. |
| | 845 | |
| | 846 | Note that the HTML spec (though not XHTML) declares all text within |
| | 847 | <script> tags to be CDATA and therefore does not need escaping. |
| | 848 | Thus, when using this filter between <script> tags for such a document, |
| | 849 | you'll want to avoid escaping the output: {{ data|json|safe }} |
| | 850 | """ |
| | 851 | return simplejson.dumps(value) |
| | 852 | |
| 841 | 853 | @register.filter(is_safe=False) |
| 842 | 854 | def pluralize(value, arg=u's'): |
| 843 | 855 | """ |
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index afee499..a28bca5 100644
|
a
|
b
|
For example::
|
| 1665 | 1665 | If ``value`` is the list ``['a', 'b', 'c']``, the output will be the string |
| 1666 | 1666 | ``"a // b // c"``. |
| 1667 | 1667 | |
| | 1668 | .. templatefilter:: json |
| | 1669 | |
| | 1670 | json |
| | 1671 | ^^^^ |
| | 1672 | |
| | 1673 | .. versionadded:: 1.4 |
| | 1674 | |
| | 1675 | Coverts a python datastructure of possibly nested dictionaries and lists into |
| | 1676 | a json string. |
| | 1677 | |
| | 1678 | For example:: |
| | 1679 | |
| | 1680 | {{ value|json }} |
| | 1681 | |
| | 1682 | If ``value`` is the python dictionary with nested list |
| | 1683 | ``{'hello': ['world', 'wide']}``, the output will be the string |
| | 1684 | ``'{"hello": ["world", "wide"]}'``. |
| | 1685 | |
| | 1686 | This filter is most often used within ``<script>`` tags. Per the |
| | 1687 | HTML spec (though not XHTML) all text within ``<script>`` tags is |
| | 1688 | CDATA and therefore does not need escaping. Thus, when using this |
| | 1689 | filter in such a situation, you'll want to explicitly mark the output |
| | 1690 | as safe to avoid html-entity escaping. For example:: |
| | 1691 | |
| | 1692 | <script> |
| | 1693 | var json_data = {{ value|json|safe }}; |
| | 1694 | </script> |
| | 1695 | |
| | 1696 | Remember that JSON marked as safe will contain unescaped strings and those need |
| | 1697 | to be properly handled in the receiver. |
| | 1698 | |
| 1668 | 1699 | .. templatefilter:: last |
| 1669 | 1700 | |
| 1670 | 1701 | last |
diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py
index 54c1267..262304b 100644
|
a
|
b
|
def get_filter_tests():
|
| 106 | 106 | 'filter-lower01': ("{% autoescape off %}{{ a|lower }} {{ b|lower }}{% endautoescape %}", {"a": "Apple & banana", "b": mark_safe("Apple & banana")}, u"apple & banana apple & banana"), |
| 107 | 107 | 'filter-lower02': ("{{ a|lower }} {{ b|lower }}", {"a": "Apple & banana", "b": mark_safe("Apple & banana")}, u"apple & banana apple & banana"), |
| 108 | 108 | |
| | 109 | # json filter #17419 |
| | 110 | 'filter-json01': ("{{ data|json }}", {"data": {"hello": ["world", "universe"]}}, u'{"hello": ["world", "universe"]}'), |
| | 111 | 'filter-json02': ("{{ data|json|safe }}", {"data": {"foo": "<bar>"}}, u'{"foo": "<bar>"}'), |
| | 112 | 'filter-json03': ("{{ data|json|safe }}", {"data": ["foo", 'ba"r']}, u'["foo", "ba\\"r"]'), |
| | 113 | |
| 109 | 114 | # The make_list filter can destroy existing escaping, so the results are |
| 110 | 115 | # escaped. |
| 111 | 116 | 'filter-make_list01': ("{% autoescape off %}{{ a|make_list }}{% endautoescape %}", {"a": mark_safe("&")}, u"[u'&']"), |