def test_direct(self):
     self.assertEqual(
         json.dumps(dct2),
         json.dumps(json.RawJSON(json.dumps(dct2))))
     self.assertEqual(
         dct2,
         json.loads(json.dumps(json.RawJSON(json.dumps(dct2)))))
 def test_list(self):
     self.assertEqual(
         json.dumps([dct2]),
         json.dumps([json.RawJSON(json.dumps(dct2))]))
     self.assertEqual(
         [dct2],
         json.loads(json.dumps([json.RawJSON(json.dumps(dct2))])))
Esempio n. 3
0
 def default(self, obj):
     if isinstance(obj, ObjectId):
         # BSON/Mongo ObjectId is rendered as a string
         return str(obj)
     if callable(obj):
         # when SCHEMA_ENDPOINT is active, 'coerce' rule is likely to
         # contain a lambda/callable which can't be jSON serialized
         # (and we probably don't want it to be exposed anyway). See #790.
         return "<callable>"
     if isinstance(obj, DBRef):
         retval = OrderedDict()
         retval["$ref"] = obj.collection
         retval["$id"] = str(obj.id)
         if obj.database:
             retval["$db"] = obj.database
         return json.RawJSON(json.dumps(retval))
     if isinstance(obj, decimal128.Decimal128):
         return str(obj)
     # delegate rendering to base class method
     return super(MongoJSONEncoder, self).default(obj)
    'key1': 'value1'
}

dct2 = {
    'key2': 'value2',
    'd1': dct1
}

dct3 = {
    'key2': 'value2',
    'd1': json.dumps(dct1)
}

dct4 = {
    'key2': 'value2',
    'd1': json.RawJSON(json.dumps(dct1))
}


class TestRawJson(unittest.TestCase):
    def test_normal_str(self):
        self.assertNotEqual(json.dumps(dct2), json.dumps(dct3))

    def test_raw_json_str(self):
        self.assertEqual(json.dumps(dct2), json.dumps(dct4))
        self.assertEqual(dct2, json.loads(json.dumps(dct4)))

    def test_list(self):
        self.assertEqual(
            json.dumps([dct2]),
            json.dumps([json.RawJSON(json.dumps(dct2))]))
import unittest
import simplejson as json

dct1 = {'key1': 'value1'}

dct2 = {'key2': 'value2', 'd1': dct1}

dct3 = {'key2': 'value2', 'd1': json.dumps(dct1)}

dct4 = {'key2': 'value2', 'd1': json.RawJSON(json.dumps(dct1))}


class TestRawJson(unittest.TestCase):
    def test_normal_str(self):
        self.assertNotEqual(json.dumps(dct2), json.dumps(dct3))

    def test_raw_json_str(self):
        self.assertEqual(json.dumps(dct2), json.dumps(dct4))
        self.assertEqual(dct2, json.loads(json.dumps(dct4)))

    def test_list(self):
        self.assertEqual(json.dumps([dct2]),
                         json.dumps([json.RawJSON(json.dumps(dct2))]))
        self.assertEqual([dct2],
                         json.loads(
                             json.dumps([json.RawJSON(json.dumps(dct2))])))

    def test_direct(self):
        self.assertEqual(json.dumps(dct2),
                         json.dumps(json.RawJSON(json.dumps(dct2))))
        self.assertEqual(