Module: RubyRunJs::JsJsonMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_json.rb

Class Method Summary collapse

Methods included from Helper

check_object, get_member, get_member_dot, is_accessor_descriptor, is_callable, is_data_descriptor, is_generic_descriptor, is_primitive, make_error, strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Class Method Details

._ja(value, stack, gap, property_list, replacer_function, space) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 190

def _ja(value, stack, gap, property_list, replacer_function, space)
  if stack.include?(value)
    raise make_error('TypeError', 'Converting circular structure to JSON')
  end
  stack << value
  stepback = @indent
  @indent += gap
  partial = []
  len = value.get('length')
  index = 0
  while index < len
    str_k = _str(index.to_s, value, replacer_function, stack, gap, property_list, space)
    if str_k == undefined
      partial << 'null'
    else
      partial << str_k
    end
    index += 1
  end
  if partial.length == 0
    final = '[]'
  else
    if gap == ''
      final = '[' + partial.join(',') + ']'
    else
      separator = ',\n' + @indent
      properties = partial.join(separator)
      final = '[\n' + @indent + properties + '\n' + stepback + ']'
    end
  end
  stack.pop()
  @indent = stepback
  final
end

._jo(value, stack, gap, property_list, replacer_function, space) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 152

def _jo(value, stack, gap, property_list, replacer_function, space)
  if stack.include?(value)
    raise make_error('TypeError', 'Converting circular structure to JSON')
  end
  stack << value
  stepback = @indent
  @indent += gap
  keys = property_list != undefined ? property_list : value.own.keys.filter { |k| value.own[k]['enumerable'] }
  partial = []
  keys.each do |k|
    str_k = _str(k, value, replacer_function, stack, gap, property_list, space)
    if str_k != undefined
      member = _quote(k)
      member += ':'
      if gap != ''
        member += gap
      end
      member += str_k
      partial << member
    end
  end

  if partial.length == 0
    final = '{}'
  else
    if gap == ''
      final = '{' + partial.join(',') + '}'
    else
      separator = ',\n' + @indent
      properties = partial.join(separator)
      final = '{\n' + @indent + properties + '\n' + stepback + '}'
    end
  end
  stack.pop()
  @indent = stepback
  final
end

._quote(value) ⇒ Object

Parameters:

  • (String)


148
149
150
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 148

def _quote(value)
  JSON.dump(value)
end

._str(key, holder, replacer_function, stack, gap, property_list, space) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 107

def _str(key, holder, replacer_function, stack, gap, property_list, space)
  value = holder.get(key)
  if value.js_type == :Object
    toJSON = value.get('toJSON')
    if is_callable(toJSON)
      value = toJSON.call(value, [key])
    end
  end
  if replacer_function != undefined
    value = replacer_function.call(holder, [key, value])
  end

  if value.js_type == :Object
    if value.js_class == 'Number'
      value = to_number(value)
    elsif value.js_class == 'String'
      value = to_string(value)
    elsif value.js_class == 'Boolean'
      value = value.value
    end
  end
  return 'null' if value == null
  return 'true' if value == true
  return 'false' if value == false
  if value.js_type == :String
    return _quote(value)
  end
  if value.js_type == :Number
    return value.finite? ? to_string(value) : 'null'
  end
  if value.js_type == :Object && !is_callable(value)
    if value.js_class == 'Array'
      return _ja(value, stack, gap, property_list, replacer_function, space)
    else
      return _jo(value, stack, gap, property_list, replacer_function, space)
    end
  end
  undefined
end

._walk(holder, name, reviver) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 77

def _walk(holder, name, reviver)
  val = holder.get(name)
  if val.js_type == :Object
    if val.js_class == 'Array'
      i = 0
      len = val.get('length')
      while i < len
        new_element = _walk(val, i.to_s, reviver)
        if new_element == undefined
          val.delete(i.to_s, false)
        else
          val.put(i.to_s, new_element)
        end
        i += 1
      end
    else
      keys = val.own.keys.filter { |k| val.own[k]['enumerable'] }
      keys.each do |key|
        new_element = _walk(val, key, reviver)
        if new_element == undefined
          val.delete(key, false)
        else
          val.put(key, new_element)
        end
      end
    end
  end
  return reviver.call(holder, [name, val])
end

.constructor_parse(builtin, this, str, reviver) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 11

def constructor_parse(builtin, this, str, reviver)
  begin
    obj = JSON.parse(str)
  rescue
    raise make_error('SyntaxError', 'JSON.parse could not parse JSON string - Invalid syntax')
  end
  unfiltered = convert_to_js_type(builtin, obj)

  if is_callable(reviver)
    result = builtin.new_object()
    result.put('': unfiltered)
    return _walk(result, '', reviver)
  else
    return unfiltered
  end
end

.constructor_stringify(builtin, this, value, replacer, space) ⇒ Object



28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_run_js/object_methods/js_json.rb', line 28

def constructor_stringify(builtin, this, value, replacer, space)
  stack = []
  @indent = ''
  property_list = undefined
  replacer_function = undefined
  if replacer.js_type == :Object
    if is_callable(replacer)
      replacer_function = replacer
    elsif replacer.js_class == 'Array'
      property_list = []
      replacer.get_items.each do |v|
        item = undefined
        if v.js_type == :String
          item = v
        elsif v.js_type == :Number
          item = to_string(v)
        elsif v.js_type == :Object
          if v.js_class == 'String' || v.js_class == 'Number'
            item = to_string(v)
          end
        end
        if item != undefined && !property_list.include?(item)
          property_list << item
        end
      end
    end
  end
  if space.js_type == :Object
    if space.js_class == 'Number'
      space = to_number(space)
    elsif space.js_class == 'String'
      space = to_string(space)
    end
  end

  if space.js_type == :Number
    space = [10, to_integer(space)].min
    gap = ' ' * [0, space].max
  elsif space.js_type == :String
    gap = space.length <= 10 ? space : space[0...10]
  else
    gap = ''
  end

  wrapper = builtin.new_object()
  wrapper.put('', value)
  _str('', wrapper, replacer_function, stack, gap, property_list, space)
end