Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

-[NSString jsonStringValue] not handling unicode chars #2

Open
smorr opened this issue Mar 7, 2011 · 2 comments
Open

-[NSString jsonStringValue] not handling unicode chars #2

smorr opened this issue Mar 7, 2011 · 2 comments

Comments

@smorr
Copy link

smorr commented Mar 7, 2011

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

Possible Solution:
replace
default:
-- [jsonString appendFormat:@"%c", nextChar];
++ [jsonString appendString:[NSString stringWithCharacters:&nextChar length:1]];
break;
or
default:
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)

@billgarrison
Copy link

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];

@billgarrison
Copy link

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants