dentaku icon indicating copy to clipboard operation
dentaku copied to clipboard

Support for JSON literal

Open coconup opened this issue 7 years ago • 2 comments

Would it be possible to introduce support for JSON literals, in line with what already done in #170?

The project in which I'm using dentaku allows users to manipulate pieces of JSON and interpolate their own variables into them. It would be great to be able to do something like this:

calc = Dentaku::Calculator.new
calc.evaluate('{"foo": {"bar": [ my_var ]}}', my_var: 100)
  # => {"foo": {"bar: [100]}}

coconup avatar Dec 20 '18 08:12 coconup

I think this gem only focuses on string evaluation, so what you probably want is support for associative arrays (hashes). In the situation you presented, I'd imagine that it might be enough to just write {"foo": {"bar": [ my_var ]}} directly in Ruby, without the need for any string evaluation. But if you want to be able to use hashes written as a string, then if associated arrays were supporteed you'd be able to do something like (notice that it is not a hash directly being passed to evaluate, but a string representing one):

calculator.evaluate("{'foo': {'bar': [ my_var ]}}", my_var: 100)
#=> {"foo"=>{"bar"=>[100]}}

You can see project-eutopia/keisan#63 for a similar feature implementation.

project-eutopia avatar Dec 20 '18 10:12 project-eutopia

@project-eutopia this is exactly what I need - I just forgot to enclose the JSON in my example into quotes (edited now).

coconup avatar Dec 20 '18 10:12 coconup