Module: Sqm2Json::Reverse

Defined in:
lib/sqm2json/json2sqm.rb

Overview

Provide JSON => SQM conversion

Instance Method Summary collapse

Instance Method Details

#get_array(key, values, level) ⇒ String

Convert a JSON value array in SQM equivalent

Parameters:

  • key (Object)

    of JSON element

  • values (Array)

    of JSON element

  • level (Integer)

    of the element in the whole JSON tree. (root is level 0)

Returns:

  • (String)

    SQM formatted array



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sqm2json/json2sqm.rb', line 51

def get_array(key, values, level)
  content = ''
  content << "#{key}[]="
  if values[0].is_a?(::Numeric)
    content << '{'
    values.each { |v|
      content << "#{get_numeric(v)},"
    }
    content.chomp!(',')
  else
    content << "\n"
    level.times { content << "\t" }
    content << "{\n"
    values.each do |v|
      (level + 1).times {content << "\t" }
      content << "\"#{v.to_s}\",\n"
    end
    content.chomp!(",\n")
    content << "\n"
    level.times { content << "\t" }
  end
  content << "};\n"
  content
end

#get_element(key, value, level) ⇒ String

Convert any JSON element to SQM equivalent

Parameters:

  • key (Object)

    of JSON element

  • value (Object)

    of JSON element

  • level (Integer)

    of the element in the whole JSON tree. (root is level 0)

Returns:

  • (String)

    SQM formatted element



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sqm2json/json2sqm.rb', line 20

def get_element(key, value, level)
  content = ''
  level.times { content << "\t" }

  case value
  when ::Numeric
    content << "#{key}=#{get_numeric(value)};\n"
  when ::String
    content << "#{key}=\"#{value}\";\n"
  when ::Array
    content << get_array(key, value, level)
  when ::Hash
    content << "#{key.to_s.gsub(/class_(\w+)/, 'class \1')}\n"
    level.times { content << "\t" }
    content << "{\n"
    value.each do |k, v|
      content << get_element(k, v, level + 1)
    end
    level.times { content << "\t" }
    content << "};\n"
  else
    raise "Invalid JSON element type: #{value.class}"
  end
  content
end

#get_numeric(value) ⇒ String

Convert a JSON numeric value in SQM equivalent

Parameters:

  • value (Object)

    of JSON element

Returns:

  • (String)

    SQM formatted numeric value



79
80
81
82
83
84
85
86
87
88
# File 'lib/sqm2json/json2sqm.rb', line 79

def get_numeric(value)
  value.to_s.gsub(/(?<val>[0-9\.]+e[-\+]?)(?<exp>[0-9]+)/) do |m|
    arr = m.split(/e/)
    if arr[1][0] =~ /[0-9]/
      "#{arr[0]}e#{arr[1].rjust(3,'0')}"
    else
      "#{arr[0]}e#{arr[1][0]}#{arr[1][1..].rjust(3,'0')}"
    end
  end.to_s
end

#to_sqm(json) ⇒ String

Generate a SQM formatted output from given JSON document

Parameters:

  • json (Hash)

    valid JSON document

Returns:

  • (String)

    SQM document as string



9
10
11
12
13
# File 'lib/sqm2json/json2sqm.rb', line 9

def to_sqm(json)
  content = ''
  json.each { |k, v| content += get_element(k, v, 0) }
  content
end