-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add custom JSON encoder for RocketPy objects
- This improves the file saving/reading methods - Sorry for implementing a new feature in this PR, it it feels like the right thing to do right now. I'm running late sorry.
- Loading branch information
1 parent
5fefdf9
commit ccf32a4
Showing
2 changed files
with
63 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Defines a custom JSON encoder for RocketPy objects.""" | ||
|
||
import json | ||
import types | ||
|
||
import numpy as np | ||
|
||
from rocketpy.mathutils.function import Function | ||
|
||
|
||
class RocketPyEncoder(json.JSONEncoder): | ||
"""NOTE: This is still under construction, please don't use it yet.""" | ||
|
||
def default(self, o): | ||
if isinstance( | ||
o, | ||
( | ||
np.int_, | ||
np.intc, | ||
np.intp, | ||
np.int8, | ||
np.int16, | ||
np.int32, | ||
np.int64, | ||
np.uint8, | ||
np.uint16, | ||
np.uint32, | ||
np.uint64, | ||
), | ||
): | ||
return int(o) | ||
elif isinstance(o, (np.float_, np.float16, np.float32, np.float64)): | ||
return float(o) | ||
elif isinstance(o, np.ndarray): | ||
return o.tolist() | ||
elif hasattr(o, "to_dict"): | ||
return o.to_dict() | ||
# elif isinstance(o, Function): | ||
# return o.__dict__() | ||
elif isinstance(o, (Function, types.FunctionType)): | ||
return repr(o) | ||
else: | ||
return json.JSONEncoder.default(self, o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters