def main(num): try: Log.start() results = [] test_json(results, "jsons.encoder", json_encoder, num) test_json(results, "jsons.encoder (again)", json_encoder, num) test_json(results, "scrub before json.dumps", cPythonJSONEncoder().encode, num) test_json(results, "override JSONEncoder.default()", EnhancedJSONEncoder().encode, num) test_json(results, "default json.dumps", json.dumps, num) # WILL CRASH, CAN NOT HANDLE DIVERSITY OF TYPES try: test_json(results, "scrubbed ujson", ujson.dumps, num) except Exception: pass Log.note("\n{{summary}}", summary=convert.list2tab(results)) finally: Log.stop()
# You can obtain one at http://mozilla.org/MPL/2.0/. # # Author: Kyle Lahnakoski ([email protected]) # import datetime import unittest from pyLibrary import convert, jsons from pyLibrary.debugs.logs import Log from pyDots import Data, wrap from pyLibrary.env.elasticsearch import scrub from pyLibrary.jsons.encoder import pypy_json_encode as pypy_json_encode, cPythonJSONEncoder, pretty_json from pyLibrary.times.dates import Date cpython_json_encoder = cPythonJSONEncoder().encode class TestJSON(unittest.TestCase): def test_date(self): output = pypy_json_encode({"test": datetime.date(2013, 11, 13)}) Log.note("JSON = {{json}}", {"json": output}) def test_unicode1(self): output = pypy_json_encode({"comment": u"Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"}) assert output == u'{"comment": "Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"}' if not isinstance(output, unicode): Log.error("expecting unicode json")