| Class | Array |
| In: |
lib/json.rb
|
| Parent: | Object |
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.rb, line 583
583: def to_json(state = nil, depth = 0)
584: state = JSON::State.from_state(state)
585: json_check_circular(state) { json_transform(state, depth) }
586: end
# File lib/json.rb, line 590
590: def json_check_circular(state)
591: if state
592: state.seen?(self) and raise JSON::CircularDatastructure,
593: "circular data structures not supported!"
594: state.remember self
595: end
596: yield
597: ensure
598: state and state.forget self
599: end
# File lib/json.rb, line 601
601: def json_shift(state, depth)
602: state and not state.array_nl.empty? or return ''
603: state.indent * depth
604: end
# File lib/json.rb, line 606
606: def json_transform(state, depth)
607: delim = ','
608: delim << state.array_nl if state
609: result = '['
610: result << state.array_nl if state
611: result << map { |value|
612: json_shift(state, depth + 1) << value.to_json(state, depth + 1)
613: }.join(delim)
614: result << state.array_nl if state
615: result << json_shift(state, depth)
616: result << ']'
617: result
618: end