mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-02 08:35:14 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
570d42ea99 |
@@ -3,6 +3,7 @@
|
|||||||
require 'cgi'
|
require 'cgi'
|
||||||
require 'base64'
|
require 'base64'
|
||||||
require 'bigdecimal'
|
require 'bigdecimal'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module StandardFilters
|
module StandardFilters
|
||||||
@@ -99,6 +100,30 @@ module Liquid
|
|||||||
input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
|
input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @liquid_public_docs
|
||||||
|
# @liquid_type filter
|
||||||
|
# @liquid_category string
|
||||||
|
# @liquid_summary
|
||||||
|
# JSON encodes the input.
|
||||||
|
# @liquid_syntax variable | json_encode.
|
||||||
|
# @liquid_return [string]
|
||||||
|
def json_encode(input)
|
||||||
|
JSON.generate(input)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @liquid_public_docs
|
||||||
|
# @liquid_type filter
|
||||||
|
# @liquid_category string
|
||||||
|
# @liquid_summary
|
||||||
|
# JSON decodes an input.
|
||||||
|
# @liquid_syntax string | json_decode.
|
||||||
|
# @liquid_return [string]
|
||||||
|
def json_decode(input)
|
||||||
|
JSON.parse(input.to_s) unless input.nil?
|
||||||
|
rescue JSON::ParserError, TypeError => e
|
||||||
|
raise Liquid::ArgumentError, e.message
|
||||||
|
end
|
||||||
|
|
||||||
# @liquid_public_docs
|
# @liquid_public_docs
|
||||||
# @liquid_type filter
|
# @liquid_type filter
|
||||||
# @liquid_category string
|
# @liquid_category string
|
||||||
|
|||||||
@@ -170,6 +170,30 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal('<strong>Hulk</strong>', @filters.escape_once('<strong>Hulk</strong>'))
|
assert_equal('<strong>Hulk</strong>', @filters.escape_once('<strong>Hulk</strong>'))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_json_encode
|
||||||
|
assert_equal('{"name":"Fizz Buzz","id":1235}', @filters.json_encode({"name"=>"Fizz Buzz", "id"=>1235}))
|
||||||
|
assert_equal('[1,2,3,5]', @filters.json_encode([1, 2, 3, 5]))
|
||||||
|
|
||||||
|
assert_equal("null", @filters.json_encode(nil))
|
||||||
|
assert_equal("1", @filters.json_encode(1))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_json_decode
|
||||||
|
assert_equal({"name"=>"Fizz Buzz", "id"=>1235}, @filters.json_decode('{"name":"Fizz Buzz","id":1235}'))
|
||||||
|
|
||||||
|
assert_template_result('{"foo"=>{"bar"=>[1, 2, 3]}}', '{% assign json_result = json_data | json_decode %}{{ json_result }}', {"json_data" => '{"foo":{"bar":[1,2,3]}}'})
|
||||||
|
assert_template_result('{"bar"=>[1, 2, 3]}', '{% assign json_result = json_data | json_decode %}{{ json_result.foo }}', {"json_data" => '{"foo":{"bar":[1,2,3]}}'})
|
||||||
|
assert_template_result('123', '{% assign json_result = json_data | json_decode %}{{ json_result.foo.bar }}', {"json_data" => '{"foo":{"bar":[1,2,3]}}'})
|
||||||
|
assert_template_result('123', '{% assign json_result = json_data | json_decode %}{{ json_result.foo["bar"] }}', {"json_data" => '{"foo":{"bar":[1,2,3]}}'})
|
||||||
|
assert_template_result('1', '{% assign json_result = json_data | json_decode %}{{ json_result.foo.bar[0] }}', {"json_data" => '{"foo":{"bar":[1,2,3]}}'})
|
||||||
|
|
||||||
|
assert_equal(1, @filters.json_decode(1))
|
||||||
|
assert_nil(@filters.json_decode(nil))
|
||||||
|
assert_raises(Liquid::ArgumentError) do
|
||||||
|
@filters.json_decode('{"name":"BrokenFragment')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_base64_encode
|
def test_base64_encode
|
||||||
assert_equal('b25lIHR3byB0aHJlZQ==', @filters.base64_encode('one two three'))
|
assert_equal('b25lIHR3byB0aHJlZQ==', @filters.base64_encode('one two three'))
|
||||||
assert_equal('', @filters.base64_encode(nil))
|
assert_equal('', @filters.base64_encode(nil))
|
||||||
|
|||||||
Reference in New Issue
Block a user