Exemple #1
0
 def test_encode_truefalse(self):
     """Should still work for a complicated example
     that has nothing to do with datetimes."""
     self.assertEqual(
         jsondt.dumps(
             {True: False, False: True},
             sort_keys=True
         ),
         '{"false": true, "true": false}')
     self.assertEqual(
         jsondt.dumps(
             {2: 3.0, 4.0: 5, False: 1, 6: True},
             sort_keys=True
         ),
         '{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
Exemple #2
0
 def test_dump_embed_obj_control(self):
     """Should dump a dictionary of dictionaries."""
     record = jsondt.dumps(WORF, control=True)
     self.assertEqual(
         record,
         WORF_TEXT_CONTROL
     )
Exemple #3
0
 def test_dump_embed_obj(self):
     """Should dump a dictionary of dictionaries."""
     record = jsondt.dumps(WORF)
     self.assertEqual(
         record,
         WORF_TEXT
     )
Exemple #4
0
 def test_dump_datetime_control(self):
     """Datetime should dump without control code."""
     now = datetime.fromisoformat("2019-08-17T07:54:22.175")
     record = {'ctime': now}
     self.assertEqual(
         jsondt.dumps(record),
         '{"ctime": "2019-08-17T07:54:22.175000"}')
Exemple #5
0
    def test_dump_second_default(self):
        """Should be able to provide a second default."""
        def default(obj):
            if isinstance(obj, set):
                newlist = list(obj)
                newlist.sort()
                return newlist
            raise TypeError

        stringified = jsondt.dumps(RESTAURANT, default=default)

        self.assertEqual(
            stringified,
            RESTAURANT_TEXT,
        )
Exemple #6
0
    def test_dump_subclass(self):
        """Should be able to subclass JSONEncoder."""
        class SetEncoder(jsondt.JSONEncoder):
            """Little Encoder that supports sets."""
            # pylint: disable=method-hidden
            def default(self, o):
                if isinstance(o, set):
                    newlist = list(o)
                    newlist.sort()
                    return newlist
                return super().default(o)

        stringified = jsondt.dumps(RESTAURANT, cls=SetEncoder)

        self.assertEqual(
            stringified,
            RESTAURANT_TEXT,
        )
Exemple #7
0
 def test_dumps(self):
     """Should work for a blank object."""
     self.assertEqual(jsondt.dumps({}), '{}')
Exemple #8
0
 def test_dump_unknown_type(self):
     """Should still reject completely unknown types."""
     record = {'names': {'Zeth', 'Green'}}
     with self.assertRaises(TypeError):
         jsondt.dumps(record)