Module JSON::Pure::Generator::GeneratorMethods::Hash
In: lib/json/pure/generator.rb
JSONError GeneratorError ParserError MissingUnicodeSupport CircularDatastructure NestingError StandardError StringScanner Parser Gtk State lib/json/common.rb lib/json/pure/parser.rb lib/json/pure/generator.rb TrueClass Integer Float Array Extend String Object FalseClass NilClass Hash GeneratorMethods Generator Pure Editor Ext JSON dot/m_9_1.png

Methods

Public Instance methods

Returns a JSON string containing a JSON object, that is unparsed from this Hash 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.

[Source]

     # File lib/json/pure/generator.rb, line 236
236:           def to_json(state = nil, depth = 0, *)
237:             if state
238:               state = JSON.state.from_state(state)
239:               state.check_max_nesting(depth)
240:               json_check_circular(state) { json_transform(state, depth) }
241:             else
242:               json_transform(state, depth)
243:             end
244:           end

Private Instance methods

[Source]

     # File lib/json/pure/generator.rb, line 248
248:           def json_check_circular(state)
249:             if state and state.check_circular?
250:               state.seen?(self) and raise JSON::CircularDatastructure,
251:                   "circular data structures not supported!"
252:               state.remember self
253:             end
254:             yield
255:           ensure
256:             state and state.forget self
257:           end

[Source]

     # File lib/json/pure/generator.rb, line 259
259:           def json_shift(state, depth)
260:             state and not state.object_nl.empty? or return ''
261:             state.indent * depth
262:           end

[Source]

     # File lib/json/pure/generator.rb, line 264
264:           def json_transform(state, depth)
265:             delim = ','
266:             if state
267:               delim << state.object_nl
268:               result = '{'
269:               result << state.object_nl
270:               result << map { |key,value|
271:                 s = json_shift(state, depth + 1)
272:                 s << key.to_s.to_json(state, depth + 1)
273:                 s << state.space_before
274:                 s << ':'
275:                 s << state.space
276:                 s << value.to_json(state, depth + 1)
277:               }.join(delim)
278:               result << state.object_nl
279:               result << json_shift(state, depth)
280:               result << '}'
281:             else
282:               result = '{'
283:               result << map { |key,value|
284:                 key.to_s.to_json << ':' << value.to_json
285:               }.join(delim)
286:               result << '}'
287:             end
288:             result
289:           end

[Validate]