1 | # -*- coding: utf-8 -*-
|
---|
2 | from __future__ import unicode_literals
|
---|
3 | import json
|
---|
4 | from django import template
|
---|
5 | from django.core.serializers.json import DjangoJSONEncoder
|
---|
6 | from django.utils.html import conditional_escape
|
---|
7 | from django.utils.safestring import mark_safe
|
---|
8 |
|
---|
9 | register = template.Library()
|
---|
10 |
|
---|
11 |
|
---|
12 | class 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')
|
---|
29 | def 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))
|
---|