intridea/multi_json

to_json clobbers emoji

benhamill opened this issue Β· 1 comments

The JsonGem adapter uses JsonCommon, which called to_json in it's dump method. I recently discovered that this causes problems marshalling and unmarshalling emoji.

Behold:

2.2.5 :024 > x
 => {:message=>"πŸ‘©πŸ‘©β€πŸŽ€πŸŽƒπŸ˜ͺπŸ˜”πŸ˜ŸπŸ˜πŸ˜³πŸ˜ŽπŸ™πŸ˜€πŸ˜•πŸ˜€πŸ˜\u{1F921}πŸ–β˜ΊοΈπŸ˜£πŸ‘ΏπŸ˜Š"}
2.2.5 :025 > MultiJson.adapter
 => MultiJson::Adapters::JsonGem
2.2.5 :026 > MultiJson.dump(x)
 => "{\"message\":\"\\uf469\\u200d\\uf3a4\\uf383\\uf62a\\uf614\\uf61f\\uf60d\\uf633\\uf60e\\uf64f\\uf600\\uf615\\uf600\\uf60f\\uf921\\uf590\\u263a\\ufe0f\\uf623\\uf47f\\uf60a\"}"
2.2.5 :027 > MultiJson.load(MultiJson.dump(x))
 => {"message"=>"ο‘©οŽ€οŽƒο˜ͺο˜”ο˜Ÿο˜ο˜³ο˜Žο™ο˜€ο˜•ο˜€ο˜ε΅ο–β˜ΊοΈο˜£ο‘Ώο˜Š"}

I think this is just a one-line change in json_common.rb from to_json to JSON.dump. But maybe that's not true for all the things that use JsonCommon? Is this a change y'all would be interested in seeing?

rwz commented

I can't recreate this bug:

[4] pry(main)> MultiJson.adapter
=> MultiJson::Adapters::JsonGem
[5] pry(main)> x = {:message=>"πŸ‘©πŸ‘©β€πŸŽ€πŸŽƒπŸ˜ͺπŸ˜”πŸ˜ŸπŸ˜πŸ˜³πŸ˜ŽπŸ™πŸ˜€πŸ˜•πŸ˜€πŸ˜\u{1F921}πŸ–β˜ΊοΈπŸ˜£πŸ‘ΏπŸ˜Š"}
=> {:message=>"πŸ‘©πŸ‘©β€πŸŽ€πŸŽƒπŸ˜ͺπŸ˜”πŸ˜ŸπŸ˜πŸ˜³πŸ˜ŽπŸ™πŸ˜€πŸ˜•πŸ˜€πŸ˜πŸ€‘πŸ–β˜ΊοΈπŸ˜£πŸ‘ΏπŸ˜Š"}
[6] pry(main)> MultiJson.dump(x)
=> "{\"message\":\"πŸ‘©πŸ‘©β€πŸŽ€πŸŽƒπŸ˜ͺπŸ˜”πŸ˜ŸπŸ˜πŸ˜³πŸ˜ŽπŸ™πŸ˜€πŸ˜•πŸ˜€πŸ˜πŸ€‘πŸ–β˜ΊοΈπŸ˜£πŸ‘ΏπŸ˜Š\"}"
[7] pry(main)> MultiJson.load(MultiJson.dump(x))
=> {"message"=>"πŸ‘©πŸ‘©β€πŸŽ€πŸŽƒπŸ˜ͺπŸ˜”πŸ˜ŸπŸ˜πŸ˜³πŸ˜ŽπŸ™πŸ˜€πŸ˜•πŸ˜€πŸ˜πŸ€‘πŸ–β˜ΊοΈπŸ˜£πŸ‘ΏπŸ˜Š"}

What version of json gem are you using? Does ugrading the gem solve the issue for you?