Sha256: ed89ca94eeefcd34ade51295b42c90daedac1df804e9434c14539d566a4c29d2

Contents?: true

Size: 1.33 KB

Versions: 16

Compression:

Stored size: 1.33 KB

Contents

require 'jsduck/io'
require 'json'

module JsDuck

  # 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).
  class JsonDuck
    @@pretty = false

    # Set to true to turn on pretty-formatting of JSON
    def self.pretty=(pretty)
      @@pretty = pretty
    end

    # Turns object into JSON, places it inside JavaScript that calls the
    # given callback name, and writes the result to file.
    def self.write_jsonp(filename, callback_name, data)
      jsonp = "Ext.data.JsonP." + callback_name + "(" + self.generate(data) + ");"
      File.open(filename, 'w') {|f| f.write(jsonp) }
    end

    # Turns object into JSON and writes inside a file
    def self.write_json(filename, data)
      File.open(filename, 'w') {|f| f.write(self.generate(data)) }
    end

    # Generates JSON from object
    def self.generate(data)
      @@pretty ? JSON.pretty_generate(data) : JSON.generate(data)
    end

    # Reads and parses JSON from file
    def self.read(filename)
      begin
        self.parse(JsDuck::IO.read(filename))
      rescue
        puts "Oh noes!  #{filename} is not a valid JSON file."
        exit(1)
      end
    end

    # Parses JSON string
    def self.parse(string)
      JSON.parse(string)
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
jsduck-3.11.2 lib/jsduck/json_duck.rb
jsduck-4.0.beta2 lib/jsduck/json_duck.rb
jsduck-3.11.1 lib/jsduck/json_duck.rb
jsduck-4.0.beta lib/jsduck/json_duck.rb
jsduck-3.11.0 lib/jsduck/json_duck.rb
jsduck-3.10.5 lib/jsduck/json_duck.rb
jsduck-3.10.4 lib/jsduck/json_duck.rb
jsduck-3.10.3 lib/jsduck/json_duck.rb
jsduck-3.10.2 lib/jsduck/json_duck.rb
jsduck-3.10.1 lib/jsduck/json_duck.rb
jsduck-3.10.0 lib/jsduck/json_duck.rb
jsduck-3.9.1 lib/jsduck/json_duck.rb
jsduck-3.9.0 lib/jsduck/json_duck.rb
jsduck-3.9.0.pre lib/jsduck/json_duck.rb
jsduck-3.8.4 lib/jsduck/json_duck.rb
jsduck-3.8.3 lib/jsduck/json_duck.rb