robotframework-thailand / robotframework-jsonlibrary

Robotframework Test Library to manipulate JSON using JSONPath
The Unlicense
48 stars 42 forks source link

Wrong character encoding for UTF-8 #59

Open luprochazka-cen63872 opened 1 year ago

luprochazka-cen63872 commented 1 year ago

Function convert_json_to_string returns wrong encoding if you use UTF-8. I have workaround:

` @staticmethod def convert_json_to_string(json_object): """Convert JSON object to string

    Arguments:
        - json_object: json as a dictionary object.

    Return new json_string

    Examples:
    | ${json_str}=  |  Convert JSON To String | ${json_obj} |
    """
    return json.dumps(json_object, **ensure_ascii=False**)`
abelikt commented 12 months ago

Hi, indeed, for some reason we are getting Utf-16 instead of Utf-8 encoded data from json.dumps. Like this German umlaut: json.dumps({"test":"über"})results in '{"test": "\\u00fcber"}'.

Here is a test for this issue:

*** Settings ***
Library   JSONLibrary

*** Test Cases ***
Test_Conversion short
    ${data} =   Set Variable    "{'test':'über'}"
    ${json} =    Convert String To Json    ${data}
    ${string} =     Convert Json To String    ${json}
    # Issue: "{'test':'\u00fcber'}" != "{'test':'über'}"
    Should Be Equal    ${string}   ${data}

I created a PR here as a workaround to forward the ensure_ascii parameter to json.dumps: https://github.com/robotframework-thailand/robotframework-jsonlibrary/pull/60

Regards, Michael