JSON parsing with Poison

I’m looking at parsing json with the Poison library. Can anyone tell me what the difference is between Poison.decode!() and Poison.Parser.parse!(). The README says:

For example, if you were interested purely in the speed of parsing JSON without a decoding step, you could simply call Poison.Parser.parse .

You probably just want Poison.decode!(). parseing is just ensuring the JSON format is correct. decodeing will do that and convert the data to Elixir/Erlang terms.

1 Like

If there is not a hugely important reason for using Poison, better use Jason.

Here’s what I’m seeing:

iex(2)> Poison.decode! ~s|{"people": [{"name": "Devin Torres", "age": 27}]}|
%{"people" => [%{"age" => 27, "name" => "Devin Torres"}]}

iex(3)> Poison.Parser.parse! ~s|{"people": [{"name": "Devin Torres", "age": 27}]}|, %{}
%{"people" => [%{"age" => 27, "name" => "Devin Torres"}]}

I see no difference in the output, and they are both elixir terms.

Well, because I don’t understand Poison, and Poison’s README is no help, I’ll give Jason a shot.

Poison.decode! literally forwards to Poison.Parser.parse! if you don’t use the as: … option so that they return the same value is to be expected. But if you use that option it’s not only doing decoding, but also fitting the json data into the data format you’ve supplied.

1 Like

And the reason being? Is Jason hugely better than Poison and if so in what ways?

It’s faster for one thing. Forgot the other reasons :003: But nowadays almost everything is integrated with it – Phoenix included – which is also a reason with a good weight.

Right - one is incredibly fast with its wicked-speed and the other one is blazing fast! :smiley: