Ticket #17419: jsonfilter.py

File jsonfilter.py, 1.0 KB (added by Jacob Rief, 10 years ago)

Implement a safe JSON filter templatetag

Line 
1# -*- coding: utf-8 -*-
2from __future__ import unicode_literals
3import json
4from django import template
5from django.core.serializers.json import DjangoJSONEncoder
6from django.utils.html import conditional_escape
7from django.utils.safestring import mark_safe
8
9register = template.Library()
10
11
12class SafeJSONEncoder(DjangoJSONEncoder):
13 def _recursive_escape(self, o, esc=conditional_escape):
14 if isinstance(o, dict):
15 return type(o)((esc(k), self._recursive_escape(v)) for (k, v) in o.iteritems())
16 if isinstance(o, (list, tuple)):
17 return type(o)(self._recursive_escape(v) for v in o)
18 try:
19 return type(o)(esc(o))
20 except ValueError:
21 return esc(o)
22
23 def encode(self, o):
24 value = self._recursive_escape(o)
25 return super(SafeJSONEncoder, self).encode(value)
26
27
28@register.filter('json')
29def json_filter(value):
30 """
31 Returns the JSON representation of ``value`` in a safe manner.
32 """
33 return mark_safe(json.dumps(value, cls=SafeJSONEncoder))
Back to Top