From c62029bffd0acf62863a3d383b9250280507cf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Wed, 26 May 2021 13:45:24 +0200 Subject: [PATCH] fixed a bug with serializing lists --- .gitignore | 2 +- docs/index.rst | 2 +- docs/usage.rst | 26 ++++++++++++++++++++++++++ minijson/exceptions.pyx | 11 +++++++---- minijson/routines.pyx | 7 ++++--- tests/test_minijson.py | 11 +++++++++++ 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 docs/usage.rst diff --git a/.gitignore b/.gitignore index 25ff4b0..dec61f1 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 7bb946e..040f8b4 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 0000000..0c73cb9 --- /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 0a2baac..19ccbe7 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 73f8fab..2dc8422 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 efc64d5..44d528e 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): -- GitLab