michalmuskala / jason

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

Encodings of integers, floats, booleans, nil should not be quoted #102

Closed everettvody closed 4 years ago

everettvody commented 4 years ago

While using the encode function, I noticed that is apparently converting all its output to a string. I don't believe this is technically correct, as JSON supports a handful of data types including integers, floats, boolean, and nulls.

Expected behavior:

iex> Jason.encode!(123)
123
iex> Jason.encode!(4.5)
4.5
iex> Jason.encode!(true)
true

Actual behavior is that these values become quoted strings:

iex> Jason.encode!(123)
"123"
iex> Jason.encode!(4.5)
"4.5"
iex> Jason.encode!(true)
"true"

Handling of nil/null's is trickier.... but they are not quoted in JSON either.

Related: Issue #81 and Issue #71

OvermindDL1 commented 4 years ago

They don't get quoted to json strings, they get converted to json (which is a string itself. To see the actual output pipe the output of Jason.encode!/1 to IO.puts() like:

iex(21)> Jason.encode!(123) |> IO.puts()
123
:ok
iex(22)> Jason.encode!(4.5) |> IO.puts()
4.5
:ok
iex(23)> Jason.encode!(true) |> IO.puts()
true
:ok

Those other issues are entirely unrelated.

I.E. you are not seeing them as json strings, you are seeing them as elixir values, which get pretty-printed as string in the console. Actually print it to see how it actually looks.

michalmuskala commented 4 years ago

As noted above the behaviour is correct. The values are converted to JSON, which is a string in itself. If they were quoted in JSON they would look, for example, like "\"123\"".

everettvody commented 4 years ago

Sorry to bother you guys -- I shouldn't have trusted the iex output. I didn't think to pipe it through IO.puts -- thanks for the explanation!