Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

-[NSString jsonStringValue] not handling unicode chars #2

smorr opened this Issue Mar 7, 2011 · 2 comments


None yet
2 participants

smorr commented Mar 7, 2011

Strings such as später are being encoded as sp‰ter

Possible Solution:
-- [jsonString appendFormat:@"%c", nextChar];
++ [jsonString appendString:[NSString stringWithCharacters:&nextChar length:1]];
if (nextChar >0x7e){
[jsonString appendFormat: @"\u%04x", nextChar];
else {
[jsonString appendFormat:@"%c", nextChar];

Former will place the unicode char into the string itself (eg später)
latter will escape the unicode char (eg sp\u00e4ter)

The problem here is the %c string format specifier. It should be %C to handle 16-bit Unicode.

So that statement should read:
[jsonString appendFormat:@"%C", nextChar];

The -jsonStringValue method converts all Unicode characters above the UTF8 range into escaped hex representation, so später will always get JSON encoded as "sp\u004xter". The assumption is that the caller wants UTF-8 encoded JSON.

JSON can be encoded as UTF-16 or UTF-32, but then it must be represented as binary data. BSJSONAdditions only generates UTF-8 JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment