Beispiel #1
0
    def test_make_json_safe(self):
        """
        Tests whether the make_json_safe method correctly
        returns values.
        """
        # Test times
        resp = make_json_safe(datetime.datetime.now())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.date.today())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.time())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.timedelta(days=1))
        self.assertIsInstance(resp, six.text_type)

        # Test decimals
        resp = make_json_safe(decimal.Decimal('1.02'))
        self.assertEqual(resp, 1.02)
        self.assertIsInstance(resp, float)

        # Test lists
        l = [datetime.time(), datetime.date.today(), datetime.datetime.now()]
        resp = make_json_safe(l)
        self.assertIsInstance(resp, list)
        for item in resp:
            self.assertIsInstance(item, six.text_type)

        # Test dictionary
        d = dict(a=datetime.datetime.now(), b=datetime.time(), c=datetime.date.today())
        resp = make_json_safe(d)
        self.assertIsInstance(resp, dict)
        for key, value in six.iteritems(resp):
            self.assertIsInstance(value, six.text_type)
Beispiel #2
0
    def test_make_json_safe(self):
        """
        Tests whether the make_json_safe method correctly
        returns values.
        """
        # Test times
        resp = make_json_safe(datetime.datetime.now())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.date.today())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.time())
        self.assertIsInstance(resp, six.text_type)
        resp = make_json_safe(datetime.timedelta(days=1))
        self.assertIsInstance(resp, six.text_type)

        # Test decimals
        resp = make_json_safe(decimal.Decimal('1.02'))
        self.assertEqual(resp, 1.02)
        self.assertIsInstance(resp, float)

        # Test lists
        l = [datetime.time(), datetime.date.today(), datetime.datetime.now()]
        resp = make_json_safe(l)
        self.assertIsInstance(resp, list)
        for item in resp:
            self.assertIsInstance(item, six.text_type)

        # Test dictionary
        d = dict(a=datetime.datetime.now(),
                 b=datetime.time(),
                 c=datetime.date.today())
        resp = make_json_safe(d)
        self.assertIsInstance(resp, dict)
        for key, value in six.iteritems(resp):
            self.assertIsInstance(value, six.text_type)
    def serialize_model(self, model, field_dict=None):
        """
        Takes a model and serializes the fields provided into
        a dictionary.

        :param Model model: The Sqlalchemy model instance to serialize
        :param dict field_dict: The dictionary of fields to return.
        :return: The serialized model.
        :rtype: dict
        """
        response = self._serialize_model_helper(model, field_dict=field_dict)
        return make_json_safe(response)
Beispiel #4
0
 def serialize_model(self, model, fields=None):
     """
     :param model: The model or queryset that is being serialized.
     :type model: django.db.models.Model
     :param list fields: The list of fields to include in the serialization
     :return: The serialized model
     :rtype: dict
     """
     fields = fields or self.fields
     field_dict = self.dot_field_list_to_dict(fields=fields)
     response = self._serialize_model_helper(model, field_dict=field_dict)
     return make_json_safe(response)
    def serialize_model(self, model, field_dict=None):
        """
        Takes a model and serializes the fields provided into
        a dictionary.

        :param Model model: The Sqlalchemy model instance to serialize
        :param dict field_dict: The dictionary of fields to return.
        :return: The serialized model.
        :rtype: dict
        """
        response = self._serialize_model_helper(model, field_dict=field_dict)
        return make_json_safe(response)
    def serialize_model(self, obj, fields_list=None):
        """
        Takes a cqlengine.Model and jsonifies it.
        This got much easier recently.  It also,
        makes the dictionary safe to immediately call
        json.dumps on it.

        :param obj: The model instance to jsonify
        :type obj: cqlengine.Model
        :return: python dictionary with field names and values
        :rtype: dict
        """
        fields_list = fields_list or self.fields
        base = dict(obj)
        base = self.valid_fields(base, fields_list)
        return make_json_safe(base)