Skip to content

Commit

Permalink
Add range object serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
claydugo committed Sep 17, 2024
1 parent d533ed7 commit 1584f97
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ro_json/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def slice_hook(dct):
return dct
return slice(dct['start'], dct['stop'], dct['step'])

def range_hook(dct):
if not isinstance(dct, dict):
return dct
if not '__range__' in dct:
return dct
return range(dct['start'], dct['stop'], dct['step'])


class EnumInstanceHook(ClassInstanceHookBase):
"""
Expand Down
14 changes: 14 additions & 0 deletions ro_json/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,20 @@ def slice_encode(obj, primitives=False):
('step', obj.step),
))

def range_encode(obj, primitives=False):
if not isinstance(obj, range):
return obj

if primitives:
return [obj.start, obj.stop, obj.step]
else:
return hashodict((
('__range__', True),
('start', obj.start),
('stop', obj.stop),
('step', obj.step),
))

class ClassInstanceEncoder(JSONEncoder):
"""
See `class_instance_encoder`.
Expand Down
6 changes: 4 additions & 2 deletions ro_json/nonp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from .encoders import TricksEncoder, json_date_time_encode, \
class_instance_encode, json_complex_encode, json_set_encode, numeric_types_encode, numpy_encode, \
nonumpy_encode, nopandas_encode, pandas_encode, noenum_instance_encode, \
enum_instance_encode, pathlib_encode, bytes_encode, slice_encode # keep 'unused' imports
enum_instance_encode, pathlib_encode, bytes_encode, slice_encode, range_encode # keep 'unused' imports
from .decoders import TricksPairHook, \
json_date_time_hook, ClassInstanceHook, \
json_complex_hook, json_set_hook, numeric_types_hook, json_numpy_obj_hook, \
json_nonumpy_obj_hook, \
nopandas_hook, pandas_hook, EnumInstanceHook, \
noenum_hook, pathlib_hook, nopathlib_hook, json_bytes_hook, slice_hook # keep 'unused' imports
noenum_hook, pathlib_hook, nopathlib_hook, json_bytes_hook, slice_hook, range_hook # keep 'unused' imports


ENCODING = 'UTF-8'
Expand All @@ -33,6 +33,7 @@
class_instance_encode,
bytes_encode,
slice_encode,
range_encode,
]

DEFAULT_HOOKS = [
Expand All @@ -43,6 +44,7 @@
_cih_instance,
json_bytes_hook,
slice_hook,
range_hook,
]


Expand Down
13 changes: 13 additions & 0 deletions tests/test_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ro_json import dumps, loads

def test_range():
original_range = range(0, 10, 2)
json_range = dumps(original_range)
loaded_range = loads(json_range)
assert original_range == loaded_range

def test_range_no_step():
original_range = range(0, 5)
json_range = dumps(original_range)
loaded_range = loads(json_range)
assert original_range == loaded_range

0 comments on commit 1584f97

Please sign in to comment.