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 298 298: def to_json(state = nil, depth = 0, *) 299: if state 300: state = JSON.state.from_state(state) 301: state.check_max_nesting(depth) 302: json_check_circular(state) { json_transform(state, depth) } 303: else 304: json_transform(state, depth) 305: end 306: end
# File lib/json/pure/generator.rb, line 310 310: def json_check_circular(state) 311: if state and state.check_circular? 312: state.seen?(self) and raise JSON::CircularDatastructure, 313: "circular data structures not supported!" 314: state.remember self 315: end 316: yield 317: ensure 318: state and state.forget self 319: end
# File lib/json/pure/generator.rb, line 321 321: def json_shift(state, depth) 322: state and not state.array_nl.empty? or return '' 323: state.indent * depth 324: end
# File lib/json/pure/generator.rb, line 326 326: def json_transform(state, depth) 327: delim = ',' 328: if state 329: delim << state.array_nl 330: result = '[' 331: result << state.array_nl 332: result << map { |value| 333: json_shift(state, depth + 1) << value.to_json(state, depth + 1) 334: }.join(delim) 335: result << state.array_nl 336: result << json_shift(state, depth) 337: result << ']' 338: else 339: '[' << map { |value| value.to_json }.join(delim) << ']' 340: end 341: end