Wrapper function for encoding the provided object. This function automatically select appropriate JSON library based on the configuration value. This function should be used everywhere in the code base where json.dumps() behavior is desired.
(obj, indent=None, sort_keys=False)
| 101 | |
| 102 | |
| 103 | def json_encode(obj, indent=None, sort_keys=False): |
| 104 | """ |
| 105 | Wrapper function for encoding the provided object. |
| 106 | |
| 107 | This function automatically select appropriate JSON library based on the configuration value. |
| 108 | |
| 109 | This function should be used everywhere in the code base where json.dumps() behavior is desired. |
| 110 | """ |
| 111 | json_library = DEFAULT_JSON_LIBRARY |
| 112 | |
| 113 | if json_library == "json": |
| 114 | return json_encode_native_json(obj=obj, indent=indent, sort_keys=sort_keys) |
| 115 | elif json_library == "orjson": |
| 116 | return json_encode_orjson(obj=obj, indent=indent, sort_keys=sort_keys) |
| 117 | else: |
| 118 | raise ValueError("Unsupported json_library: %s" % (json_library)) |
| 119 | |
| 120 | |
| 121 | def json_decode(data): |