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

Support for JSON literal #175

Open
coconup opened this issue Dec 20, 2018 · 2 comments
Open

Support for JSON literal #175

coconup opened this issue Dec 20, 2018 · 2 comments

Comments

@coconup
Copy link

coconup commented Dec 20, 2018

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]}}
@project-eutopia
Copy link
Contributor

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.

@coconup
Copy link
Author

coconup commented Dec 20, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants