Skip to content
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

Add a new filter fromjson #3298

Merged
merged 1 commit into from Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/concerns/liquid_interpolatable.rb
Expand Up @@ -263,6 +263,12 @@ def json(input)
JSON.dump(input)
end

def fromjson(input)
JSON.parse(input.to_s)
rescue StandardError
nil
end

def hex_encode(input)
input.to_s.unpack1('H*')
end
Expand Down
10 changes: 10 additions & 0 deletions spec/concerns/liquid_interpolatable_spec.rb
Expand Up @@ -303,6 +303,16 @@ def @filter.to_xpath_roundtrip(string)
end
end

describe 'fromjson' do
let(:agent) { Agents::InterpolatableAgent.new(name: "test") }

it 'should parse a JSON string' do
agent.interpolation_context['json'] = '{"array": ["a", "b", "c"], "number": 42}'
agent.options['key'] = '{% assign obj = json | fromjson %}{{ obj["array"][1] }} and {{ obj.number }}'
expect(agent.interpolated['key']).to eq('b and 42')
end
end

context 'as_object' do
let(:agent) { Agents::InterpolatableAgent.new(name: "test") }

Expand Down