Skip to content

Commit

Permalink
Merge ef89b58 into 1c18d3b
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Jul 12, 2023
2 parents 1c18d3b + ef89b58 commit 43ceaac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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

0 comments on commit 43ceaac

Please sign in to comment.