support jsonify filter #1651

Merged
merged 1 commit into from Oct 24, 2013

Projects

None yet

7 participants

@liufengyun
Contributor

Now jekyll supports automatically loading yaml files under _data/ directory. To use the data in front-end to build rich client apps, there should be an easy way to convert the data into JavaScript.

The jsonify filter fulfills that purpose.

For example, following code exports site.data.projects for front-end usage.

window.projects = {{ site.data.projects | jsonify }}
@parkr parkr and 1 other commented on an outdated diff Oct 22, 2013
test/test_filters.rb
@@ -98,5 +98,9 @@ def initialize
should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things")
end
+
+ should "convert object to json" do
+ assert_equal "{\"name\":\"test\"}", @filter.jsonify({name: "test"})
@parkr
parkr Oct 22, 2013 Member

Your jsonify method also allows for Array's. Can you please add in an array example here, too? :)

@liufengyun
liufengyun Oct 22, 2013 Contributor

Good point, a new test case is added for array.

@parkr
Member
parkr commented Oct 22, 2013

I like it!

Or perhaps a white-listed plugin, @benbalter?

@parkr
Member
parkr commented Oct 22, 2013

Additionally, it appears this doesn't work until Ruby 1.8.7, which we can't deprecate before Jekyll v2.0.0. Can you please investigate?

@parkr parkr commented on an outdated diff Oct 22, 2013
test/test_filters.rb
@@ -98,5 +98,15 @@ def initialize
should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things")
end
+
+ context "jsonify filter" do
+ should "convert hash to json" do
+ assert_equal "{\"age\":18}", @filter.jsonify({age: 18})
+ end
+
+ should "convert array to json" do
+ assert_equal "[1,2]", @filter.jsonify([1, 2])
@parkr
parkr Oct 22, 2013 Member

What happens if I have an array of objects? Can you please add that in?

@parkr parkr and 1 other commented on an outdated diff Oct 22, 2013
test/test_filters.rb
@@ -98,5 +98,15 @@ def initialize
should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things")
end
+
+ context "jsonify filter" do
+ should "convert hash to json" do
+ assert_equal "{\"age\":18}", @filter.jsonify({"age" => 18})
@parkr
parkr Oct 22, 2013 Member

This syntax is incompatible with 1.8.7. Please change to:

{ :age => 18 }
@liufengyun
liufengyun Oct 22, 2013 Contributor

{ "age" => 8 } is valid in 1.8.7.

@parkr parkr and 1 other commented on an outdated diff Oct 22, 2013
test/test_filters.rb
@@ -98,5 +98,15 @@ def initialize
should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things")
end
+
+ context "jsonify filter" do
+ should "convert hash to json" do
+ assert_equal "{\"age\":18}", @filter.jsonify({"age" => 18})
+ end
+
+ should "convert array to json" do
+ assert_equal "[1,2]", @filter.jsonify([1, 2])
@parkr
parkr Oct 22, 2013 Member

What happens if I have an array of objects? Maybe add in a second assert that is an array of objects? That's the most-likely use-case for this in Jekyll (JSONify posts/pages data)

@liufengyun
liufengyun Oct 22, 2013 Contributor

OK, done.

@liufengyun
Contributor

@parkr the test is fixed for 1.8.7. I forgot 1.8.7 doesn't support {age: 8}. I have changed it to {"age" => 8}.

@parkr
Member
parkr commented Oct 22, 2013

Awesome! This PR LGTM. @mattr-?

@benbalter
Contributor

Would love to see this in core. There's an up upstream ticket, but looks like they didn't contribute back.

@mattr- mattr- merged commit f697d12 into jekyll:master Oct 24, 2013

1 check passed

default The Travis CI build passed
Details
@mattr- mattr- added a commit that referenced this pull request Oct 24, 2013
@mattr- mattr- Update history to reflect merge of #1651 112aa81
@maul-esel
Contributor

What about the json filter documented here ? And, if I just do {{ site.data.projects }}, doesn't it already output json anyway?

@liufengyun
Contributor

@maul-esel, {{ site.data.members }} outputs something like following, which is not json:

{"name"=>"Jack", "age"=>27, "blog"=>"http://example.com/jack"}

The link you pointed to is related to this ticket as @benbalter mentioned above. The json filter is not in the Liquid core.

@maul-esel
Contributor

OK thanks, I wasn't aware of that.

@kanitw
kanitw commented Jan 6, 2014

Is there a way to make this work with _posts too?

@mattr-
Member
mattr- commented Jan 6, 2014

{{ site.posts | jsonify }} should work just fine, unless I misunderstand what you're asking.

@tekknolagi

@mattr- I don't believe that works, unless the design is to simply have a JSON array. Should it not return an array of objects?

@XhmikosR XhmikosR referenced this pull request in twbs/bootstrap Dec 5, 2015
Merged

Fix docs/search.json. #18432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment