Class: JSON::State

Inherits:
Object show all
Defined in:
lib/facets/json.rb

Overview

This class is used to create State instances, that are use to hold data while unparsing a Ruby data structure into a JSON string.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ State

Instantiates a new State object, configured by opts.



361
362
363
364
365
366
367
# File 'lib/facets/json.rb', line 361

def initialize(opts = {})
  @indent     = opts[:indent]     || ''
  @space      = opts[:space]      || ''
  @object_nl  = opts[:object_nl]  || ''
  @array_nl   = opts[:array_nl]   || ''
  @seen       = {}
end

Instance Attribute Details

#array_nlObject

This string is put at the end of a line that holds a JSON array.



381
382
383
# File 'lib/facets/json.rb', line 381

def array_nl
  @array_nl
end

#indentObject

This string is used to indent levels in the JSON string.



370
371
372
# File 'lib/facets/json.rb', line 370

def indent
  @indent
end

#object_nlObject

This string is put at the end of a line that holds a JSON object (or Hash).



378
379
380
# File 'lib/facets/json.rb', line 378

def object_nl
  @object_nl
end

#spaceObject

This string is used to include a space between the tokens in a JSON string.



374
375
376
# File 'lib/facets/json.rb', line 374

def space
  @space
end

Class Method Details

.from_state(opts) ⇒ Object

Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance. If opts is a State object, it is just returned.



349
350
351
352
353
354
355
356
357
358
# File 'lib/facets/json.rb', line 349

def self.from_state(opts)
  case opts
  when self
    opts
  when Hash
    new(opts)
  else
    new
  end
end

Instance Method Details

#forget(object) ⇒ Object

Forget object for this Unparsing run.



395
396
397
# File 'lib/facets/json.rb', line 395

def forget(object)
  @seen.delete object.__id__
end

#remember(object) ⇒ Object

Remember object, to find out if it was already encountered (to find out if a cyclic data structure is unparsed).



390
391
392
# File 'lib/facets/json.rb', line 390

def remember(object)
  @seen[object.__id__] = true
end

#seen?(object) ⇒ Boolean

Returns true, if object was already seen during this Unparsing run.

Returns:

  • (Boolean)


384
385
386
# File 'lib/facets/json.rb', line 384

def seen?(object)
  @seen.key?(object.__id__)
end