The origin of

The requirement is to convert the string passed by the front end to a dictionary that the back end (Python) uses as the argument body to request any interface.

The method I used was to use loads in json packages as shown in the following example:

import json

if __name__ == '__main__':
    test_str = '{"status": "ok"}'
    test_json = json.loads(test_str)
    print('type -----------> %s' % type(test_json))
    print('test_json -----------> %s' % test_json)
Copy the code

After running, the console output is as follows:

type -----------> <class 'dict'>
test_json -----------> {'status': 'ok'}

Process finished with exit code 0
Copy the code

You can see that the output is fine, but as a serious person, after thinking about the business application scenario, I decided to test again to see if I could successfully convert integers, floating point numbers, nested dictionaries, arrays, booleans, and null values in strings.

Leave tuples and date types alone:)

The process of exploration

Explore the code:

Import json if __name__ = = "__main__ ': # integral + floating dictionary + + nested array test test_str =' {" status" : {" number ": 123," float ": 123.321, "a list" : [1, 2, 3, "1"]}}' test_json = json.loads(test_str) print('type -----------> %s' % type(test_json)) print('test_json -----------> %s' % test_json)Copy the code

Console output:

Type -- -- -- -- -- -- -- -- -- -- - > < class 'dict > test_json -- -- -- -- -- -- -- -- -- -- - > {' status' : {' number: 123,' float ': 123.321,' list ': [1, 2, 3, '1']}} Process finished with exit code 0Copy the code

Well, so far so good.

However,

Shock!!! Amazing discovery

Core code:

Import json if __name__ == '__main__': # Boolean + null test test_str = '{"status1": true, "status2": false, "status3": null}' test_json = json.loads(test_str) print('type -----------> %s' % type(test_json)) print('test_json -----------> %s' % test_json)Copy the code

Console output:

type -----------> <class 'dict'>
test_json -----------> {'status1': True, 'status2': False, 'status3': None}

Process finished with exit code 0
Copy the code

The json.loads function converts true, false, and null to true, false, and None.

Loads this after checking the json.loads source (Ctrl + B being broken) :

    elif nextchar == 'n' and string[idx:idx + 4] == 'null':
        return None, idx + 4
    elif nextchar == 't' and string[idx:idx + 4] == 'true':
        return True, idx + 4
    elif nextchar == 'f' and string[idx:idx + 5] == 'false':
        return False, idx + 5
Copy the code

That — that code — that’s tough.

Scroll down for more surprises:

    elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
        return parse_constant('NaN'), idx + 3
    elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
        return parse_constant('Infinity'), idx + 8
    elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
        return parse_constant('-Infinity'), idx + 9
Copy the code

conclusion

Every piece of code has a little secret behind, carefully dig will get different fun and harvest.

Blog driven development, development is just for better writing!

Welcome to scan code to pay attention to my public number “intelligent automation testing”, reply: advanced test tutorial, you can get free advanced tutorial ~

I wish you all a happy life, everything goes well ~

-- TesterCopy the code