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 266 266: def to_json(state = nil, depth = 0, *) 267: if state 268: state = JSON.state.from_state(state) 269: state.check_max_nesting(depth) 270: json_check_circular(state) { json_transform(state, depth) } 271: else 272: json_transform(state, depth) 273: end 274: end
# File lib/json/pure/generator.rb, line 278 278: def json_check_circular(state) 279: if state and state.check_circular? 280: state.seen?(self) and raise JSON::CircularDatastructure, 281: "circular data structures not supported!" 282: state.remember self 283: end 284: yield 285: ensure 286: state and state.forget self 287: end
# File lib/json/pure/generator.rb, line 289 289: def json_shift(state, depth) 290: state and not state.array_nl.empty? or return '' 291: state.indent * depth 292: end
# File lib/json/pure/generator.rb, line 294 294: def json_transform(state, depth) 295: delim = ',' 296: delim << state.array_nl if state 297: result = '[' 298: result << state.array_nl if state 299: result << map { |value| 300: json_shift(state, depth + 1) << value.to_json(state, depth + 1) 301: }.join(delim) 302: result << state.array_nl if state 303: result << json_shift(state, depth) 304: result << ']' 305: result 306: end