class JsDuck::Util::Json
Wrapper around the json gem for use in JsDuck
.
The main benefit of it is that we have a central place for controlling how the JSON is created (pretty-formatted or not).
Public Class Methods
new()
click to toggle source
# File lib/jsduck/util/json.rb, line 16 def initialize @pretty = false end
Public Instance Methods
configure(opts)
click to toggle source
Configures the pretty-formatting from command line options.
# File lib/jsduck/util/json.rb, line 21 def configure(opts) @pretty = true if opts.pretty_json end
generate(data)
click to toggle source
Generates JSON from object
# File lib/jsduck/util/json.rb, line 38 def generate(data) @pretty ? JSON.pretty_generate(data) : JSON.generate(data) end
parse(string, opts = {})
click to toggle source
Parses JSON string
# File lib/jsduck/util/json.rb, line 53 def parse(string, opts = {}) JSON.parse(string, opts) end
read(filename)
click to toggle source
Reads and parses JSON from file
# File lib/jsduck/util/json.rb, line 43 def read(filename) begin parse(Util::IO.read(filename)) rescue Logger.fatal("#{filename} is not a valid JSON file") exit(1) end end
write_json(filename, data)
click to toggle source
Turns object into JSON and writes inside a file
# File lib/jsduck/util/json.rb, line 33 def write_json(filename, data) File.open(filename, 'w') {|f| f.write(generate(data)) } end
write_jsonp(filename, callback_name, data)
click to toggle source
Turns object into JSON, places it inside JavaScript that calls the given callback name, and writes the result to file.
# File lib/jsduck/util/json.rb, line 27 def write_jsonp(filename, callback_name, data) jsonp = "Ext.data.JsonP." + callback_name + "(" + generate(data) + ");" File.open(filename, 'w') {|f| f.write(jsonp) } end