michalmuskala / jason

A blazing fast JSON parser and generator in pure Elixir.
Other
1.62k stars 169 forks source link

valid json decode error #88

Closed mydearxym closed 5 years ago

mydearxym commented 5 years ago

hi there ~

got error when i decode a value with ()

iex(2)> Jason.decode!(~S({"text": "(or more complex structure)"}))
...(2)>
OvermindDL1 commented 5 years ago

Really? Jason.decode!(~S[{"text": "(or more complex structure)"}]) works just fine here.

Note that in your specific example your string closing character is ), yet you have a ) inside it, so then you start a string with ", so it gets parsed as:

# Jason.decode!(~S({"text": "(or more complex structure)"}))
Jason.decode!("{\"text\": \"(or more complex structure""}))

Which as you see leaves an open " on the end of the line, waiting for you to type the rest. Remember that whatever character you use to delineate the string ((/) in this case) you need to escape within the string itself, or use a different delineator.

As for Jason, I'm not seeing any error with Jason and () values nor could I see it being possible given the code in Jason?

iex> Jason.decode!(~S[{"text": "(or more complex structure)"}])
%{"text" => "(or more complex structure)"}
michalmuskala commented 5 years ago

This is the issue with the use of the ~S syntax. If ( is used as a delimiter, the closing delimiter ) must be escaped inside the string:

Jason.decode!(~S({"text": "(or more complex structure\)"}))

Otherwise, the parser treats the quote following the ) as opening of another string and that's why it waits for further input (since that string is never closed).