def test_restore_type(self): some_class = NestedClass() json = parser.to_json(some_class) restored_class = parser.from_json(json, globals()) self.assertEqual(type(some_class), type(restored_class))
def test_two_way_list(self): list = ["Wow", None, "Boom", 5, False, 4.2] json = parser.to_json(list) restored_list = parser.from_json(json, globals()) self.assertEqual(list, restored_list)
def test_nested_class(self): nested_class = NestedClass([NestedClass(), NestedClass()]) json = parser.to_json(nested_class) restored_class = parser.from_json(json, globals()) self.assertEqual(len(nested_class.inner_objects), len(restored_class.inner_objects))
def test_default_loads_comparison(self): some_object = [None, 5, ["hey", "Ouch"]] default_json = json.dumps(some_object) my_json = parser.to_json(some_object) default_restored = json.loads(default_json) my_restored = parser.from_json(my_json, globals()) self.assertEqual(default_restored, my_restored)
if __name__ == '__main__': tests_result = unittest.TextTestRunner(verbosity=0).run(suite()) if not tests_result.wasSuccessful(): exit(1) kevin_the_kid = Person("Kevin", 13, None, None) duck_hunter = Job("CuackDuck Co.", "Hunter") oliver_the_teen = Person("Oliver", 19.5, duck_hunter, None) sony_programmer = Job("Sony", "Programmer") johny_the_daddy = Person("John", 45, sony_programmer, [ kevin_the_kid, oliver_the_teen]) json = parser.to_json(johny_the_daddy) print("Json:\n"+json, end="\n\n") restored_johny = parser.from_json(json, globals()) print("Is Johny old: " + str(restored_johny.isOld), end="\n") print("How many kids does Johny have: " + str(len(restored_johny.kids)), end="\n") print("Where does Johny work: at " + restored_johny.job.company_name, end="\n\n") raw_collection_json = parser.to_json([False, True]) print("Json:\n"+raw_collection_json, end="\n\n") print("Collection: " + str(parser.from_json(raw_collection_json, globals)))
def test_broken_json(self): with self.assertRaises(Exception): parser.from_json("Not a Json", globals())