js2json

It is a library to convert from JavaScript to JSON.

Installation

Add this line to your application's Gemfile:

gem 'js2json'

And then execute:

$ bundle

Or install it yourself as:

$ gem install js2json

Usage

require 'js2json'

json = Js2json.js2json("function plus(a, b) {\n  return a + b;\n};\n\n// Comment\n({\n  foo: \"bar\",\n  \"zoo\": plus(1, 2),\n  'BAZ': 'A' + 'B',\n  plus: plus,\n});\n")

puts json # => {
          #      "foo": "bar",
          #      "zoo": 3,
          #      "BAZ": "AB",
          #      "plus": "function plus(a, b) {\n  return a + b;\n}"
          #    }

Use Ruby in JavaScript

require 'js2json'

json = Js2json.js2json("Ruby.Kernel.require('net/http');\n\nvar page = Ruby.Net.HTTP.start('example.com', 80, function(http) {\n  return http.get('/').body();\n});\n\n({\n  site: 'example.com',\n  page: page,\n});\n")

Auto bracket script

require 'js2json'

json = Js2json.js2json("{\n  foo: \"bar\",\n  zoo: \"baz\",\n}\n", :bracket_script => true)

puts json # => {
          #      "foo": "bar",
          #      "zoo": "baz"
          #    }

Command line tool

$ echo '{foo:"bar", zoo:"baz"}' | js2json

Contributing

  1. Fork it ( http://github.com/winebarrel/js2json/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request