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

Handle empty strings as keys properly #12

Merged
merged 1 commit into from
Feb 16, 2019
Merged
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 lib/jaxon/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ defmodule Jaxon.Path do
"[#{i}]"
end

defp do_encode_segment("") do
~s("")
end

defp do_encode_segment(s) when is_binary(s) do
if(String.contains?(s, ["*", "$", "]", "[", ".", "\""])) do
"\"#{String.replace(s, "\"", "\\\"")}\""
Expand Down
1 change: 1 addition & 0 deletions test/jaxon_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule JaxonTest do
test "objects" do
assert decode!(~s({})) == %{}
assert decode!(~s({"number": 2})) == %{"number" => 2}
assert decode!(~s({"": 2})) == %{"" => 2}
assert decode!(~s({"nested": {}})) == %{"nested" => %{}}
assert decode!(~s({"nested": {"nested": 2}})) == %{"nested" => %{"nested" => 2}}
end
Expand Down
3 changes: 2 additions & 1 deletion test/path_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule JaxonPathTest do
assert encode!([:root, :all]) == "$[*]"
assert encode!([:root, :all, :all]) == "$[*][*]"
assert encode!([:root, "$", :all]) == "$.\"$\"[*]"
assert encode!([:root, "", :all]) == "$[*]"
assert encode!([:root, "", :all]) == ~s($.""[*])

assert_raise(EncodeError, "`:whoops` is not a valid JSON path segment", fn ->
encode!([:root, :whoops, "test", 0])
Expand All @@ -26,6 +26,7 @@ defmodule JaxonPathTest do
assert parse!("$.\"nested\"[0]") == [:root, "nested", 0]
assert parse!("$.\"nested\".0") == [:root, "nested", "0"]
assert parse!("$.\"$\".0") == [:root, "$", "0"]
assert parse!(~s($.""[0])) == [:root, "", 0]

assert_raise(ParseError, ~r/Ending quote not found.*/, fn ->
parse!("$.\"nested.0")
Expand Down
2 changes: 2 additions & 0 deletions test/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule JaxonEventStreamTest do
"empty_array": [],
"empty_object": {},
"bool1": true,
"": "empty",
"bool2": false,
"null": null,
"person": {
Expand All @@ -49,6 +50,7 @@ defmodule JaxonEventStreamTest do
stream = Util.chunk_binary(@json_stream, chunk_size)

assert [1] == query(stream, "$.numbers[0]")
assert ["empty"] == query(stream, ~s($.""))
assert [nil] == query(stream, "$.null")
assert [2] == query(stream, "$.numbers[1]")
assert [[1, 2, -1]] == query(stream, "$.numbers")
Expand Down