Class: MiGA::Json

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/json.rb

Overview

Taxonomic classifications in MiGA.

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Class Method Summary collapse

Methods inherited from MiGA

CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?, #say

Methods included from Common::Path

#root_path, #script_path

Methods included from Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Methods included from Common::Net

#download_file_ftp, #known_hosts, #remote_connection

Methods included from Common::SystemCall

#run_cmd, #run_cmd_opts

Class Method Details

.default_opts(opts = {}) ⇒ Object

Default parsing options. Supported opts keys:

  • :contents: If true, the input is assumed to be the contents to parse, not the path to a JSON file.

  • :default: A base to attach the parsed hash. A Hash or a String (path).

  • :additions: If addition classes should be parsed. By default is false.

  • :symbolize: If names should be symbolized. By default it’s true if additions is false, or false otherwise. They can both be false, but an exception will be raised if both are true



18
19
20
21
22
23
24
25
26
27
# File 'lib/miga/json.rb', line 18

def default_opts(opts = {})
  opts[:contents] ||= false
  opts[:additions] ||= false
  opts[:symbolize] = !opts[:additions] if opts[:symbolize].nil?
  if opts[:additions] and opts[:symbolize]
    raise 'JSON additions are not supported with symbolized names'
  end

  opts
end

.generate(obj, path = nil) ⇒ Object

Generates and returns prettyfied JSON to represent obj. If path is passed, it saves the JSON in that file.



67
68
69
70
71
# File 'lib/miga/json.rb', line 67

def generate(obj, path = nil)
  y = JSON.pretty_generate(obj)
  File.open(path, 'w') { |fh| fh.print y } unless path.nil?
  y
end

.parse(path, opts = {}) ⇒ Object

Parse a JSON file in path and return a hash. Optionally, use default as the base to attach the parsed hash. default can be a Hash or a String (path). See default_opts for supported opts.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/miga/json.rb', line 34

def parse(path, opts = {})
  opts = default_opts(opts)

  # Read JSON
  cont = path
  12.times do
    cont = File.read(path)
    break unless cont.empty?
    sleep 1 # Wait up to 12 seconds for racing processes (iff empty file)
  end unless opts[:contents]
  raise "Empty descriptor: #{opts[:contents] ? "''" : path}" if cont.empty?

  # Parse JSON
  params = {
    symbolize_names: opts[:symbolize],
    create_additions: opts[:additions]
  }
  y = JSON.parse(cont, params)

  # Add defaults
  unless opts[:default].nil?
    opts[:default] = parse(opts[:default]) if opts[:default].is_a? String
    y.each { |k, v| opts[:default][k] = v }
    y = opts[:default]
  end

  # Return
  y
end