Sometimes I get JSON from a backend that contains unencoded/invalid characters. For example, they might contain an (unencoded) character with ASCII code 31, which means that when I try to do jsonDecode, it fails. Don't ask why the JSON-encoding on the backend is broken, it just is.
Anyway, I was wondering if there is a way to replace all unencoded characters with their encoded representations so I will be able to decode the JSON?
The solution I came up with is this. It basically checks every character in the JSON and if it is outside the "safe range" of ASCII 32-255, it JSON-encodes the character, strips any surrounding
""and adds the JSON-encoded representation instead of the unencoded character.It could probably be improved, but it seems to do the job at least.