From 9a15a090288f82882fea94b3ccc41a54da685160 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 5 Nov 2014 11:37:34 -0800 Subject: [PATCH 1/3] Add {{ | jsonify }} support for hashes. --- lib/jekyll/filters.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 424815f9..f215e0b8 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -328,10 +328,14 @@ module Jekyll def as_liquid(item) case item + when String, Numeric, nil + item.to_liquid + when Hash + Hash[item.map { |k, v| [as_liquid(k), as_liquid(v)] }] when Array item.map{ |i| as_liquid(i) } else - item.respond_to?(:to_liquid) ? item.to_liquid : item + item.respond_to?(:to_liquid) ? as_liquid(item.to_liquid) : item end end end From b29fd6d380d13fc5c7bcb3eedf968a6c13b571b0 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 5 Nov 2014 11:37:51 -0800 Subject: [PATCH 2/3] Add tests. Need to fix up the assertions to match the output. --- test/test_filters.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index f12fc171..ed14f9e1 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -148,13 +148,24 @@ class TestFilters < Test::Unit::TestCase assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}]) end - should "call #to_liquid " do - class AThing < Struct.new(:name) - def to_liquid - { "name" => name, :v => 1 } - end + class M < Struct.new(:message) + def to_liquid + [message] end - assert_equal "[{\"name\":\"Jeremiah\",\"v\":1},{\"name\":\"Smathers\",\"v\":1}]", @filter.jsonify([AThing.new("Jeremiah"), AThing.new("Smathers")]) + end + class T < Struct.new(:name) + def to_liquid + { "name" => name, :v => 1, :thing => M.new({:kay => "jewelers"}) } + end + end + + should "call #to_liquid " do + assert_equal "[{\"name\":\"Jeremiah\",\"v\":1},{\"name\":\"Smathers\",\"v\":1}]", @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) + end + + should "handle hashes with all sorts of weird keys and values" do + my_hash = { "posts" => Array.new(5) { |i| T.new(i) } } + assert_equal "{}", @filter.jsonify(my_hash) end end From 18930b01f679e52969335ce7f3f693b935722836 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 5 Nov 2014 11:39:15 -0800 Subject: [PATCH 3/3] Fix up the assertions. --- test/test_filters.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index ed14f9e1..7325e90e 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -160,12 +160,14 @@ class TestFilters < Test::Unit::TestCase end should "call #to_liquid " do - assert_equal "[{\"name\":\"Jeremiah\",\"v\":1},{\"name\":\"Smathers\",\"v\":1}]", @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) + expected = "[{\"name\":\"Jeremiah\",\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]},{\"name\":\"Smathers\",\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]}]" + assert_equal expected, @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) end should "handle hashes with all sorts of weird keys and values" do my_hash = { "posts" => Array.new(5) { |i| T.new(i) } } - assert_equal "{}", @filter.jsonify(my_hash) + expected = "{\"posts\":[{\"name\":0,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]},{\"name\":1,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]},{\"name\":2,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]},{\"name\":3,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]},{\"name\":4,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}]}]}" + assert_equal expected, @filter.jsonify(my_hash) end end