Code Monkey home page Code Monkey logo

Comments (2)

tisba avatar tisba commented on July 18, 2024 1

If you closely compare the code you've put into your browser console, you'll notice it is not the same you have in your Ruby code.

It's a quoting issue. I think your example can be simplified to this:

data = { "foo" => %q(") }
str_to_eval = %Q|JSON.parse('#{data.to_json}')|
puts str_to_eval

which prints: JSON.parse('{"foo":"\""}'). Which is not a valid JSON string after JS evaluation. You have to keep in mind, that the \ needs to be escaped as well as we want the literal "forward slash". While the previous JS code snippet won't work, but JSON.parse('{"foo":"\\""}') does. This has nothing to to with mini_racer or Node.js or Chrome.

Depending on your use case, it might be easier to define a function and use Context#call, so you don't have to worry so much about properly embedding the escaped string into the code.

Alternatively you can do the following. I'm not sure if there is a nicer way to do that, but this works:

data = { 'body' => '{"param1":"some value","param2":"some other value"}' } # your example input
data == JSON.parse(MiniRacer::Context.new.eval(data.to_json.to_json))
=> true

The String#to_json trick will make sure the String is properly escaped and it will look like this: "{\"body\":\"{\\\"param1\\\":\\\"some value\\\",\\\"param2\\\":\\\"some other value\\\"}\"}" (everything here is printed as literal characters).

Escaping across so many layers is always confusing 😅 Hope that helps understanding the issue!

from mini_racer.

phstc avatar phstc commented on July 18, 2024

@tisba thank you for troubleshooting this! ❤️

from mini_racer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.