Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
35 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
|
||
|
||
class JSONString(str): | ||
pass | ||
|
||
|
||
def checked_loads(value, **kwargs): | ||
""" | ||
Ensure that values aren't loaded twice, resulting in an encoding error. | ||
Loaded strings are wrapped in JSONString, as it is otherwise not possible | ||
to differentiate between a loaded and unloaded string. | ||
""" | ||
if isinstance(value, (list, dict, int, float, JSONString, type(None))): | ||
return value | ||
|
||
value = json.loads(value, **kwargs) | ||
if isinstance(value, str): | ||
value = JSONString(value) | ||
|
||
return value |