| Module | JSON::Pure::Generator::GeneratorMethods::Array |
| In: |
lib/json/pure/generator.rb
|
Returns a JSON string containing a JSON array, that is unparsed from this Array instance. state is a JSON::State object, that can also be used to configure the produced JSON string output further. depth is used to find out nesting depth, to indent accordingly.
# File lib/json/pure/generator.rb, line 299
299: def to_json(state = nil, depth = 0, *)
300: if state
301: state = JSON.state.from_state(state)
302: state.check_max_nesting(depth)
303: json_check_circular(state) { json_transform(state, depth) }
304: else
305: json_transform(state, depth)
306: end
307: end
# File lib/json/pure/generator.rb, line 311
311: def json_check_circular(state)
312: if state and state.check_circular?
313: state.seen?(self) and raise JSON::CircularDatastructure,
314: "circular data structures not supported!"
315: state.remember self
316: end
317: yield
318: ensure
319: state and state.forget self
320: end
# File lib/json/pure/generator.rb, line 322
322: def json_shift(state, depth)
323: state and not state.array_nl.empty? or return ''
324: state.indent * depth
325: end
# File lib/json/pure/generator.rb, line 327
327: def json_transform(state, depth)
328: delim = ','
329: if state
330: delim << state.array_nl
331: result = '['
332: result << state.array_nl
333: result << map { |value|
334: json_shift(state, depth + 1) << value.to_json(state, depth + 1)
335: }.join(delim)
336: result << state.array_nl
337: result << json_shift(state, depth)
338: result << ']'
339: else
340: '[' << map { |value| value.to_json }.join(delim) << ']'
341: end
342: end