diff --git a/.gitignore b/.gitignore index 25ff4b01b4e18745fa9d6b63b7397dda4fe19023..dec61f13bc5dabaf9b5fcd63a3de67d5d118a603 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ __pycache__/ # C extensions *.so - +.idea/ # Distribution / packaging .Python build/ diff --git a/docs/index.rst b/docs/index.rst index 7bb946ea1cc3c7ebaa8d947ef216df3ded372922..040f8b4fb5957738111449a34c980e61ab9f58e2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -10,7 +10,7 @@ Welcome to MiniJSON's documentation! :maxdepth: 2 :caption: Contents: - + usage Indices and tables ================== diff --git a/docs/usage.rst b/docs/usage.rst new file mode 100644 index 0000000000000000000000000000000000000000..0c73cb94bdd572bb890f214970fa5393cb126c77 --- /dev/null +++ b/docs/usage.rst @@ -0,0 +1,26 @@ +Usage +===== + +MiniJSON implements the same interface as json or yaml, namely: + +.. autofunction:: minijson.loads + +.. autofunction:: minijson.dumps + +.. autofunction:: minijson.dump + +.. autofunction:: minijson.parse + +For serializing objects you got the following: + +.. autofunction:: minijson.dumps_object + +.. autofunction:: minijson.loads_object + +And the following exceptions: + +.. autoclass:: minijson.MiniJSONError + +.. autoclass:: minijson.EncodingError + +.. autoclass:: minijson.DecodingError diff --git a/minijson/exceptions.pyx b/minijson/exceptions.pyx index 0a2baac919725b9207f2c6c4fa49950ce40c2ab7..19ccbe7d993cc66e2a60dd7506f34a7ef2cba72f 100644 --- a/minijson/exceptions.pyx +++ b/minijson/exceptions.pyx @@ -1,11 +1,14 @@ class MiniJSONError(ValueError): - """Base class for MiniJSON errors""" - pass + """ + Base class for MiniJSON errors. + + Note that it inherits from :code:`ValueError`. + """ + class EncodingError(MiniJSONError): """Error during encoding""" - pass + class DecodingError(MiniJSONError): """Error during decoding""" - pass diff --git a/minijson/routines.pyx b/minijson/routines.pyx index 73f8fab156fa8b1e5a50368b27e4f0903ad27d31..2dc84222c70394b2c9f3831a88e61c4e598b80b9 100644 --- a/minijson/routines.pyx +++ b/minijson/routines.pyx @@ -65,8 +65,8 @@ cpdef tuple parse(bytes data, int starting_position): return string_length+1, data[starting_position+1:starting_position+string_length+1].decode('utf-8') except UnicodeDecodeError as e: raise DecodingError('Invalid UTF-8') from e - elif value_type & 0xF0 == 0b01000000: - list_length = value_type & 0xF + elif value_type & 0xF0 == 0x40: + elements = value_type & 0xF offset = 1 e_list = [] for i in range(elements): @@ -74,10 +74,11 @@ cpdef tuple parse(bytes data, int starting_position): offset += length e_list.append(elem) return offset, e_list - elif value_type & 0xF0 == 0b01010000: + elif value_type & 0xF0 == 0x50: e_dict = {} offset = 1 elements = value_type & 0xF + for i in range(elements): length, b_field_name = parse_cstring(data, starting_position+offset) s_field_name = b_field_name.decode('utf-8') diff --git a/tests/test_minijson.py b/tests/test_minijson.py index efc64d595bc9f73b6118586ad942bf0c56659679..44d528e741734a84b49039999c5f6218c082e53a 100644 --- a/tests/test_minijson.py +++ b/tests/test_minijson.py @@ -3,6 +3,17 @@ from minijson import dumps, loads, dumps_object, loads_object, EncodingError, De class TestMiniJSON(unittest.TestCase): + + def test_lists(self): + a = [1, 2, 3] + b = dumps(a) + print(f'Serialized {b}') + c = loads(b) + self.assertEqual(a, c) + + a = [None]*256 + self.assertRaises(EncodingError, lambda: dumps(a)) + def test_exceptions(self): a = {} for i in range(65535):