Skip to content

Commit

Permalink
Skip null values when deserializing json to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Jul 3, 2014
1 parent 9a30fc7 commit 5312eed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ describe "Json serialization" do
it "does Hash(String, Int32)#from_json" do
Hash(String, Int32).from_json(%({"foo": 1, "bar": 2})).should eq({"foo" => 1, "bar" => 2})
end

it "does Hash(String, Int32)#from_json and skips null" do
Hash(String, Int32).from_json(%({"foo": 1, "bar": 2, "baz": null})).should eq({"foo" => 1, "bar" => 2})
end
end
end
7 changes: 5 additions & 2 deletions src/json/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ end
def Hash.new(pull : Json::PullParser)
hash = new
pull.read_object do |key|
hash[key] = V.new(pull)
if pull.kind == :null
pull.read_next
else
hash[key] = V.new(pull)
end
end
hash
end

0 comments on commit 5312eed

Please sign in to comment.