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 527 527: def to_json(state = nil, depth = 0) 528: state = JSON::State.from_state(state) 529: json_check_circular(state) { json_transform(state, depth) } 530: end
# File lib/json.rb, line 534 534: def json_check_circular(state) 535: if state 536: state.seen?(self) and raise JSON::CircularDatastructure, 537: "circular data structures not supported!" 538: state.remember self 539: end 540: yield 541: ensure 542: state and state.forget self 543: end
# File lib/json.rb, line 545 545: def json_shift(state, depth) 546: state and not state.array_nl.empty? or return '' 547: state.indent * depth 548: end
# File lib/json.rb, line 550 550: def json_transform(state, depth) 551: delim = ',' 552: delim << state.array_nl if state 553: result = '[' 554: result << state.array_nl if state 555: result << map { |value| 556: json_shift(state, depth + 1) << value.to_json(state, depth + 1) 557: }.join(delim) 558: result << state.array_nl if state 559: result << json_shift(state, depth) 560: result << ']' 561: result 562: end