def test_json(self): EUR = MoneyMaker('EUR') renderer = JSONRenderer() data = {'amount': EUR('1.23')} rendered_json = renderer.render(data, 'application/json') self.assertDictEqual({'amount': "€ 1.23"}, json.loads(rendered_json.decode('utf-8')))
def rest_json(value, arg=None): """ Renders a `ReturnDict` as used by the REST framework into a safe JSON string. """ if not value: return mark_safe('{}') if not isinstance(value, (dict, OrderedDict, list, tuple)): msg = "Given value must be of type dict, OrderedDict, list or tuple but it is {}." raise ValueError(msg.format(value.__class__.__name__)) data = JSONRenderer().render(value) return mark_safe(data)
def render(self, context): try: cart = CartModel.objects.get_from_request(context['request']) serializer = CartSerializer(instance=cart, context=context, label='dropdown', with_items=self.with_items) cart_data = JSONRenderer().render(serializer.data) except CartModel.DoesNotExist: cart_data = {'total_quantity': 0, 'num_items': 0} context.update({ 'cart_as_json': mark_safe(force_text(cart_data)), 'has_dropdown': self.with_items != CartItems.without, }) return self.get_template().render(context)
def test_json_renderer(): renderer = JSONRenderer() data = {'amount': EUR('1.23')} rendered_json = renderer.render(data, 'application/json') assert {'amount': "€ 1.23"} == json.loads(rendered_json.decode('utf-8'))
def test_json(self): EUR = MoneyMaker("EUR") renderer = JSONRenderer() data = {"amount": EUR("1.23")} rendered_json = renderer.render(data, "application/json") self.assertDictEqual({"amount": "€ 1.23"}, json.loads(rendered_json.decode("utf-8")))
def test_json(self): Money = MoneyMaker('EUR') renderer = JSONRenderer() data = {'amount': Money('1.23')} rendered_json = renderer.render(data, 'application/json') self.assertDictEqual({'amount': '€ 1.23'}, json.loads(rendered_json))