Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore Poison.Parser.parse/2 #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{})
%{"name" => "Devin Torres", "age" => 27}
iex> Poison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{keys: :atoms!})
%{name: "Devin Torres", age: 27}
iex> Poison.Parser.parse(~s({"name": "Devin Torres", "age": 27}), %{})
{:ok, %{"name" => "Devin Torres", "age" => 27}}
iex> Poison.Parser.parse(~s({"name": "Devin Torres", "age": 27}), %{keys: :atoms!})
{:ok, %{name: "Devin Torres", age: 27}}
```

Note that `keys: :atoms!` reuses existing atoms, i.e. if `:name` was not
Expand Down
7 changes: 7 additions & 0 deletions lib/poison/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ defmodule Poison.Parser do
reraise %ParseError{value: iodata}, stacktrace()
end

def parse(iodata, options) do
{:ok, parse!(iodata, options)}
rescue
error in [ParseError] ->
{:error, error}
end

defp value("\"" <> rest, pos, _keys) do
string_continue(rest, pos + 1, [])
end
Expand Down
48 changes: 48 additions & 0 deletions test/poison/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,55 @@ defmodule Poison.ParserTest do
assert parse!(~s({"foo": "bar"}), %{keys: :atoms}) == %{foo: "bar"}
end

test "parse/2" do
assert_parse_error("Unexpected end of input at position 1", fn ->
parse("-")
end)

assert_parse_error("Unexpected end of input at position 1", fn ->
parse(~s("))
end)

assert_parse_error("Unexpected end of input at position 1", fn ->
parse("{")
end)

assert_parse_error("Unexpected end of input at position 1", fn ->
parse("[")
end)

assert_parse_error("Unexpected end of input at position 0", fn ->
parse("")
end)

hash = :erlang.phash2(:crypto.strong_rand_bytes(8))

assert_parse_error(
~s(Cannot parse value at position 3: "key#{hash}"),
fn ->
parse(~s({"key#{hash}": null}), %{keys: :atoms!})
end
)

assert parse("0") == {:ok, 0}
assert parse(~s("\\"\\\\\\/\\b\\f\\n\\r\\t")) == {:ok, ~s("\\/\b\f\n\r\t)}
assert parse("{}") == {:ok, %{}}
assert parse("[]") == {:ok, []}
assert parse(" [ ] ") == {:ok, []}
assert parse(~s({"foo": "bar"}), %{keys: :atoms!}) == {:ok, %{foo: "bar"}}
assert parse(~s({"foo": "bar"}), %{keys: :atoms}) == {:ok, %{foo: "bar"}}
end

defp parse!(iodata) do
parse!(iodata, %{})
end

defp parse(iodata) do
parse(iodata, %{})
end

defp assert_parse_error(message, fun) do
{:error, %ParseError{} = error} = fun.()
assert Exception.message(error) == message
end
end