1 # -*- coding: utf-8 -*-
2 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
5 from __future__ import unicode_literals
7 from .variables import SsiVariable, SsiExpect
11 if isinstance(o, SsiVariable):
12 return {'__var__': o.definition}
13 if isinstance(o, SsiExpect):
14 return {'__expect__': o.name}
15 raise TypeError(o, 'not JSON serializable')
18 def _json_obj_hook(obj):
19 keys = list(obj.keys())
20 if keys == ['__var__']:
21 return SsiVariable(*obj['__var__'])
22 if keys == ['__expect__']:
23 return SsiExpect(obj['__expect__'])
27 def json_encode(obj, **kwargs):
28 return json.JSONEncoder(
29 default=_json_default,
30 separators=(',', ':'),
34 def json_decode(data, **kwargs):
35 return json.loads(data, object_hook=_json_obj_hook, **kwargs)