Reputation: 1
Basically, when JSON takes in a string it will convert things like '
or &
to their Unicode value. I'm trying to store the JSON value as it goes out, and when it comes back in later, compare it with the JSON value. However, when I send out something like "Let's Party
" it comes back "Let\u0027s Party
"
So basically, I'm looking to convert all JSON Unicode to their specific Unicode values before storing it or sending it out.
Upvotes: 0
Views: 1189
Reputation: 536369
I'm looking to convert all JSON Unicode to their specific Unicode values
I doubt you want to convert all characters to \u
-escapes. In that case Let's party
would become \u004c\u0065\u0074\u0027\u0073\u0020\u0050\u0061\u0072\u0074\u0079
.
There is nothing special about the apostrophe or ampersand that means it has to be encoded in JSON, although some encoders do so anyway (it can have advantages for using JSON inside another wrapper context where those characters are sepecial).
It looks like you want to match the exact output that another encoder produces. To do that you'd have to determine the full set of characters that that encoder decides to escape, and either alter or configure your own JSON encoder to match that. For some characters that could be as simple as doing a string replace: for example as '
may only legitimately appear in JSON as part of a string literal it would be safe to replace with \u0027
after the encoding. This is ugly and fragile though.
It is generally a bad idea to rely on the exact encoding choices of a JSON serialiser. The JSON values {"a": "'", "b": 0.0}
and {"b": 0, a: "\u0027"}
represent the same data and should generally be treated as equal. For comparison purposes it is usually better to parse the JSON and check the content piece-by-piece, or re-serialise using your own encoder and compare that output (assuming your JSON encoder is deterministic).
Upvotes: 1