Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emoji characters cannot be encoded to JSON

I have a UITextView which I call messageField. The data within that messageField is POST-ed to server in JSON format. When the user types an Emoji character I am having trouble encoding the data to JSON. I am thinking Emoji uses Unicode encoding.

Is there a way to encode an Emoji character to JSON? And back from JSON to Emoji to display in a UILabel?

like image 290
janusfidel Avatar asked Aug 24 '11 15:08

janusfidel


People also ask

Are Emojis allowed in JSON?

If you want to send send an emoji item through JSON, first you need to formate DB to UTF-8 AND in IOS you need to encode for NSUTF8StringEncoding. So first make sure your DB formate to UTF-8 then encode parameters to NSUTF8StringEncoding.So here is a sample request when sending the message.

Can Emojis be encoded in UTF-8?

Emojis look like images, or icons, but they are not. They are letters (characters) from the UTF-8 (Unicode) character set. UTF-8 covers almost all of the characters and symbols in the world.

What is the encoding of emoji?

emoji requires 4 bytes to encode using UTF-8, we now see 4 characters when we interpret the file with the Windows-1258 encoding. A wrong choice of character encoding has a direct impact on what we can see and comprehend by garbling characters into an incomprehensible mess.

Is Unicode valid in JSON?

JSON data always uses the Unicode character set.


2 Answers

I use the below code to encode emoji character

NSString *uniText = [NSString stringWithUTF8String:[textview.text UTF8String]]; 
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding]; 
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ;

And the below code to decode and display in UILabel

const char *jsonString = [body UTF8String]; 
NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)]; 
NSString *goodMsg = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
like image 54
Karun Avatar answered Sep 22 '22 15:09

Karun


Edit - 2016-03-03 Please note, this answer was written in 2011 and may no longer be relevant any more.

Emoji characters are just a specific font used to render specific unicode code points. iOS uses one of the Unicode Private Use Areas for Emoji-specific code points. The only way to view these "characters" as Emoji is to have an Emoji font available as well as a machine that knows how to switch from the default text font (such as Helvetica) to the emoji font.

I don't know how you're encoding your JSON but since Emoji are just text there shouldn't be any problems as long as you transport the text in a Unicode-friendly format such as UTF-8 or UTF-16. You won't see it on the server-side or in the database (unless you view the data with the previous prerequisites) but you should be able to send the same raw bytes back and it should look the same.

Here's some posts that might help a little more:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
  • Questions about iPhone emoji and web pages
  • iPhone Emoji
like image 39
Chris Haas Avatar answered Sep 23 '22 15:09

Chris Haas