Closed sofakingworld closed 4 years ago
This is about escapes - there's a difference in behaviour in Elixir """
and Ruby's '
.
iex(3)> json = """
...(3)> {"name": "A \"B\""}
...(3)> """
"{\"name\": \"A \"B\"\"}\n"
vs
irb(main):001:0> '{"name": "A \"B\""}'
=> "{\"name\": \"A \\\"B\\\"\"}"
To get the same value in Elixir the easiest is to use ~S
iex(1)> json = ~S|{"name": "A \"B\""}|
"{\"name\": \"A \\\"B\\\"\"}"
iex(2)> Jason.decode(json)
{:ok, %{"name" => "A \"B\""}}
By the way, found some weird behavior:
json = ~S|{"name": "A \"B\""}|
Jason.decode(inspect(json))
# => {:ok, "{\"name\": \"A \\\"B\\\"\"}"}
This behavior is entirely correct. Inspect accidentally turns a string containing JSON into a single string representation of that that also happens to be a valid JSON. Decoding reverses that.
This would be the same as:
> JSON.decode!(inspect("a"))
"a"
It won't always happen, but in limited cases such as this where the only conversion is escaping quotes JSON.decode!
happens to be the reverse of inspect
.
Jason version: 1.2.1
JSON
Jason.decode result:
Ruby JSON result
Chrome console