Reputation: 2587
I am working on importing a file from a legacy C++ codebase. The file itself is technically a JSON but one of the values is obfuscated with an XOR function in the C++ codebase before I receive it and looks something like this.
{"version": 15, "data": "C\u0016Q45\u0010 46QY\\\u0011\n\u0019a\u0003\u0019}\u001apg"}
The value for "data" is technically a UTF8 string.
I know the algorithm used to decode that data into a usable JSON string. However, I am unable to get swift to parse this into a [String : String] apparently due to formatting issues.
Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 31."
So far I have managed to get the specific data in question by isolating the subdata
let encryptedData = data.subdata(in: dataPrefixLength ..< dataLength - 2)
Even still, I cannot seem to get swift to parse this into a String when I attempt to do so with UTF8 encoding.
String(data: encryptedData, encoding: .utf8)
It's also worth noting that the String description seems alright in the debugger when inspecting the data itself.
I'd really appreciate any advice. Thanks!
Upvotes: 1
Views: 350
Reputation: 78855
From your example it looks as if the obfuscation generates binary data and the binary data is put into a string. That's not allowed in JSON. Therefore, it's invalid JSON and any parser with a reasonable amount of validation will reject it.
As an example, take the start of the string:
"C\u0016
It starts with the character "C". That's valid.
Then an escaped character with a hexadecimal values follows: 0016
. However, U+0016 is not a valid Unicode codepoint. Therefore it's rejected. It's probably supposed to be the binary byte 0x16. But you can't put that into a JSON string.
You have two options:
Fix the source of the data, e.g. by using a Base64 encoding before putting binary data into JSON.
Write your own JSON parser to handle the invalid JSON.
Upvotes: 2