Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability for .json() to accept kwargs. model.json() can now crea… #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions qcelemental/models/basemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def serialize(
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
**kwargs,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only hesitation is because I don't know if serialize was written w/o kwargs as a defensive measure. Certainly the indentation use to which you've put kwargs is reasonable and harmless, though. @bennybp, any complications to the database if formats can change through this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this will cause any problems on my end

) -> Union[bytes, str]:
r"""Generates a serialized representation of the model

Expand All @@ -144,28 +145,30 @@ def serialize(
If True, skips fields that have set or defaulted values equal to the default.
exclude_none
If True, skips fields that have value ``None``.
kwargs
For passing additional values like indent to serialization functions

Returns
-------
~typing.Union[bytes, str]
The serialized model.
"""

kwargs = {}
dict_kwargs = {}
if include:
kwargs["include"] = include
dict_kwargs["include"] = include
if exclude:
kwargs["exclude"] = exclude
dict_kwargs["exclude"] = exclude
if exclude_unset:
kwargs["exclude_unset"] = exclude_unset
dict_kwargs["exclude_unset"] = exclude_unset
if exclude_defaults:
kwargs["exclude_defaults"] = exclude_defaults
dict_kwargs["exclude_defaults"] = exclude_defaults
if exclude_none:
kwargs["exclude_none"] = exclude_none
dict_kwargs["exclude_none"] = exclude_none

data = self.dict(**kwargs)
data = self.dict(**dict_kwargs)

return serialize(data, encoding=encoding)
return serialize(data, encoding=encoding, **kwargs)

def json(self, **kwargs):
# Alias JSON here from BaseModel to reflect dict changes
Expand Down
8 changes: 8 additions & 0 deletions qcelemental/tests/test_model_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ def test_failed_operation(result_data_fixture, request):
assert "its all good" in failed_json


def test_model_json_allows_kwargs(result_data_fixture):
result = qcel.models.AtomicResult(**result_data_fixture)
no_indent = result.json()
assert "\n" not in no_indent
indent = result.json(indent=2)
assert "\n" in indent


def test_result_properties_array(request):
lquad = [1, 2, 3, 2, 4, 5, 3, 5, 6]

Expand Down
12 changes: 8 additions & 4 deletions qcelemental/util/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,23 @@ def default(self, obj: Any) -> Any:
return json.JSONEncoder.default(self, obj)


def json_dumps(data: Any) -> str:
def json_dumps(data: Any, **kwargs) -> str:
r"""Safe serialization of a Python dictionary to JSON string representation using all known encoders.

Parameters
----------
data : Any
A encodable python object.
kwargs : Any
Keyword arguments for json.dumps()

Returns
-------
str
A JSON representation of the data.
"""

return json.dumps(data, cls=JSONArrayEncoder)
return json.dumps(data, cls=JSONArrayEncoder, **kwargs)


def json_loads(data: str) -> Any:
Expand Down Expand Up @@ -313,7 +315,7 @@ def msgpack_loads(data: str) -> Any:
## Helper functions


def serialize(data: Any, encoding: str) -> Union[str, bytes]:
def serialize(data: Any, encoding: str, **kwargs) -> Union[str, bytes]:
r"""Encoding Python objects using the provided encoder.

Parameters
Expand All @@ -322,6 +324,8 @@ def serialize(data: Any, encoding: str) -> Union[str, bytes]:
A encodable python object.
encoding : str
The type of encoding to perform: {'json', 'json-ext', 'msgpack-ext'}
kwargs: Any
For passing additional kwargs to serialization functions, such as indent

Returns
-------
Expand All @@ -330,7 +334,7 @@ def serialize(data: Any, encoding: str) -> Union[str, bytes]:

"""
if encoding.lower() == "json":
return json_dumps(data)
return json_dumps(data, **kwargs)
elif encoding.lower() == "json-ext":
return jsonext_dumps(data)
elif encoding.lower() == "msgpack":
Expand Down