def test_list(self): list_to_json = [74, True, False, None, [1, 2], {"key": 4}] self.assertTrue(to_json(list_to_json) == dumps(list_to_json))
def test_integer(self): self.assertEqual(to_json(50), dumps(50))
def test_float(self): self.assertEqual(to_json(3.4), dumps(3.4))
def test_none(self): self.assertEqual(to_json(None), dumps(None))
def test_string(self): self.assertEqual(to_json("str"), dumps("str"))
def test_dict(self): dict_to_json = {1: "one", 2: 'two'} self.assertTrue(to_json(dict_to_json) == dumps(dict_to_json))
def test_bool(self): self.assertEqual(to_json(True), dumps(True))
def test_list(self): list_from_json = to_json([74, True, False, None, [1, 2], {"key": 4}]) self.assertTrue(from_json(list_from_json) == loads(list_from_json))
def test_none(self): self.assertEqual(from_json(to_json(None)), loads(dumps(None)))
def test_bool(self): self.assertEqual(from_json(to_json(True)), loads(dumps(True)))
def test_dict(self): dict_from_json = to_json({1: "one", 2: 'two'}) self.assertTrue(from_json(dict_from_json) == loads(dict_from_json))
quot = not quot continue if text[i] == '.': is_float = True tokens.append(text[i]) raise IOError("Неверный формат") def get_token(text, start): for i in range(start, len(text)): if text[i] == ' ' or text[i] == ',': continue if text[i] == '}' or text[i] == ']': raise IOError("{}".format(i)) if text[i] == '[': return json_to_list(text, i) elif text[i] == '{': return json_to_dict(text, i) else: return json_to_basic(text, i) if __name__ == "__main__": b = [74, True, False, None, [1, 2], {"key": 4}] d = {1: "one", 2: 'two'} c = to_json(b) print(c) print(from_json(to_json(True))) print(dumps(b)) print(loads(dumps(b)))