Skip to content

Commit

Permalink
Handle nested serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcsmith committed Nov 10, 2017
1 parent 33dee7d commit f986af1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions spec/lucky_web/serializer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ private class TestJSON < LuckyWeb::Serializer
end
end

private class TestNestedJSON < LuckyWeb::Serializer
def initialize(@user_name : String)
end

def render
{user: TestJSON.new(@user_name)}
end
end

describe LuckyWeb::Serializer do
describe "#to_json" do
it "calls to_json on the render data" do
user = TestJSON.new(user_name: "Rey")
TestJSON.new(user_name: "Rey").to_json.should eq({name: "Rey"}.to_json)
end

user.to_json.should eq({name: "Rey"}.to_json)
it "handles nested JSON classes" do
nested = TestNestedJSON.new(user_name: "Picard")
nested.to_json.should eq({user: {name: "Picard"}}.to_json)
end
end
end
4 changes: 2 additions & 2 deletions src/lucky_web/serializer.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
abstract class LuckyWeb::Serializer
def to_json
render.to_json
def to_json(io)
render.to_json(io)
end
end

0 comments on commit f986af1

Please sign in to comment.