Skip to content

Commit

Permalink
Added indent to json.dumps.
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-keshore-seneca committed Aug 11, 2022
1 parent 0e34ba1 commit b3b55e0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion flask_dantic/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.5
0.0.6
3 changes: 2 additions & 1 deletion flask_dantic/pydantic_serializer/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def serialize(
exclude_none: bool = False,
many=False,
json_dump: bool = True,
json_indent: int = 2,
):
"""Create python dict from an object using a Pydantic Type"""
if many:
Expand All @@ -63,7 +64,7 @@ def serialize(
if not errors:
encoded_obj = jsonable_encoder(response_dict, include=include, exclude=exclude, exclude_none=exclude_none)
if json_dump:
return json.dumps(encoded_obj)
return json.dumps(encoded_obj, indent=json_indent)
return encoded_obj

else:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ def test_serialize_json_dumped_objects(db):

# Returns dumped object.
res = serialize(single_user, UserModel, json_dump=True)
assert res == json.dumps({"username": USERNAMES[0], "age": AGE, "phone": None})
assert res == json.dumps({"username": USERNAMES[0], "age": AGE, "phone": None}, indent=2)

res = serialize(users, UserModel, many=True, json_dump=True)
assert res == json.dumps([{"username": username, "age": AGE, "phone": None} for username in USERNAMES])
assert res == json.dumps([{"username": username, "age": AGE, "phone": None} for username in USERNAMES], indent=2)

res = serialize(users, UserModel, many=True, include=["username"], json_dump=True)
assert res == json.dumps([{"username": username} for username in USERNAMES])
assert res == json.dumps([{"username": username} for username in USERNAMES], indent=2)

res = serialize(users, UserModel, many=True, exclude=["age"], json_dump=True)
assert res == json.dumps([{"username": username, "phone": None} for username in USERNAMES])
assert res == json.dumps([{"username": username, "phone": None} for username in USERNAMES], indent=2)

res = serialize(users, UserModel, many=True, exclude_none=True, json_dump=True)
assert res == json.dumps([{"username": username, "age": AGE} for username in USERNAMES])
assert res == json.dumps([{"username": username, "age": AGE} for username in USERNAMES], indent=2)


def test_serialize_negative(db):
Expand Down

0 comments on commit b3b55e0

Please sign in to comment.