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

Extend convert_json_to_string to forward ensure_ascii to json.dumps #60

Open
wants to merge 2 commits 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
6 changes: 4 additions & 2 deletions JSONLibrary/jsonlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,22 @@ def delete_object_from_json(self, json_object, json_path):
return json_object_cpy

@staticmethod
def convert_json_to_string(json_object, indent=None):
def convert_json_to_string(json_object, indent=None, ensure_ascii=True):
"""Convert JSON object to string

Arguments:
- json_object: json as a dictionary object.
- indent: indent level for pretty-printing, see indent argument of python's [https://docs.python.org/3/library/json.html#json.dump|json.dump()] for details
- ensure_ascii: Make sure there is only ascii. Escape all non-ascii characters.

Return new json_string

Examples:
| ${json_str}= | Convert JSON To String | ${json_obj} |
| ${json_str}= | Convert JSON To String | ${json_obj} | indent=${4} |
| ${json_str}= | Convert JSON To String | ${json_obj} | indent=${4} | ensure_ascii=False |
"""
return json.dumps(json_object, indent=indent)
return json.dumps(json_object, indent=indent, ensure_ascii=ensure_ascii)

@staticmethod
def convert_string_to_json(json_string):
Expand Down
12 changes: 12 additions & 0 deletions acceptance/JSONLibrary.robot
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ TestConvertJSONToString
${json_str}= Convert Json To String ${json_obj_input}
Should Be String ${json_str}

TestEnsureAsciiDefault
${data} = Set Variable "{'test':'ueber'}"
${json} = Convert String To Json ${data}
${string} = Convert Json To String ${json}
Should Be Equal ${string} ${data}

TestEnsureAsciiFalse
${data} = Set Variable "{'test':'über'}"
${json} = Convert String To Json ${data}
${string} = Convert Json To String ${json} ensure_ascii=False
Should Be Equal ${string} ${data}

TestDumpJSONToFile
[Documentation] Dumps Json to file
Dump Json to file ${TEMPDIR}/sample_dump.json ${json_obj_input}
Expand Down