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

support jsonify filter #1651

Merged
merged 1 commit into from Oct 24, 2013

Conversation

Projects
None yet
8 participants
@liufengyun
Contributor

liufengyun commented Oct 22, 2013

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

View changes

test/test_filters.rb Outdated
@@ -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"})

This comment has been minimized.

@parkr

parkr Oct 22, 2013

Member

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

This comment has been minimized.

@liufengyun

liufengyun Oct 22, 2013

Contributor

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

@parkr

This comment has been minimized.

Member

parkr commented Oct 22, 2013

I like it!

Or perhaps a white-listed plugin, @benbalter?

@parkr

This comment has been minimized.

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

View changes

test/test_filters.rb Outdated
end
should "convert array to json" do
assert_equal "[1,2]", @filter.jsonify([1, 2])

This comment has been minimized.

@parkr

parkr Oct 22, 2013

Member

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

@parkr

View changes

test/test_filters.rb Outdated
context "jsonify filter" do
should "convert hash to json" do
assert_equal "{\"age\":18}", @filter.jsonify({"age" => 18})

This comment has been minimized.

@parkr

parkr Oct 22, 2013

Member

This syntax is incompatible with 1.8.7. Please change to:

{ :age => 18 }

This comment has been minimized.

@liufengyun

liufengyun Oct 22, 2013

Contributor

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

@parkr

View changes

test/test_filters.rb Outdated
end
should "convert array to json" do
assert_equal "[1,2]", @filter.jsonify([1, 2])

This comment has been minimized.

@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)

This comment has been minimized.

@liufengyun

liufengyun Oct 22, 2013

Contributor

OK, done.

@liufengyun

This comment has been minimized.

Contributor

liufengyun commented Oct 22, 2013

@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

This comment has been minimized.

Member

parkr commented Oct 22, 2013

Awesome! This PR LGTM. @mattr-?

@benbalter

This comment has been minimized.

Contributor

benbalter commented Oct 22, 2013

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

mattr- added a commit that referenced this pull request Oct 24, 2013

Merge pull request #1651 from liufengyun/jsonify
support jsonify filter

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

1 check passed

default The Travis CI build passed
Details

mattr- added a commit that referenced this pull request Oct 24, 2013

@maul-esel

This comment has been minimized.

Contributor

maul-esel commented Oct 24, 2013

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

@liufengyun

This comment has been minimized.

Contributor

liufengyun commented Oct 24, 2013

@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

This comment has been minimized.

Contributor

maul-esel commented Oct 24, 2013

OK thanks, I wasn't aware of that.

@kanitw

This comment has been minimized.

kanitw commented Jan 6, 2014

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

@mattr-

This comment has been minimized.

Member

mattr- commented Jan 6, 2014

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

@tekknolagi

This comment has been minimized.

tekknolagi commented Jan 15, 2014

@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 Dec 5, 2015

Merged

Fix docs/search.json. #18432

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017

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