Class: JSON::Pure::Generator::State
- Inherits:
-
Object
- Object
- JSON::Pure::Generator::State
- Defined in:
- lib/gistto/enhancements/json.rb
Overview
This class is used to create State instances, that are use to hold data while generating a JSON text from a Ruby data structure.
Instance Attribute Summary collapse
-
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#buffer_initial_length ⇒ Object
:stopdoc:.
-
#depth ⇒ Object
This integer returns the current depth data structure nesting in the generated JSON.
-
#indent ⇒ Object
This string is used to indent levels in the JSON text.
-
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
-
#object_nl ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
-
#quirks_mode ⇒ Object
If this attribute is set to true, quirks mode is enabled, otherwise it’s disabled.
-
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
-
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return the value returned by method
name. -
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
-
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated.
-
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
-
#check_max_nesting ⇒ Object
:nodoc:.
-
#configure(opts) ⇒ Object
(also: #merge)
Configure this State instance with the Hash opts, and return itself.
-
#generate(obj) ⇒ Object
Generates a valid JSON document from object
objand returns the result. -
#initialize(opts = {}) ⇒ State
constructor
Instantiates a new State object, configured by opts.
-
#quirks_mode? ⇒ Boolean
Returns true, if quirks mode is enabled.
-
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
Constructor Details
#initialize(opts = {}) ⇒ State
Instantiates a new State object, configured by opts.
opts can have the following keys:
-
indent: a string used to indent levels (default: ”),
-
space: a string that is put after, a : or , delimiter (default: ”),
-
space_before: a string that is put before a : pair delimiter (default: ”),
-
object_nl: a string that is put at the end of a JSON object (default: ”),
-
array_nl: a string that is put at the end of a JSON array (default: ”),
-
check_circular: is deprecated now, use the :max_nesting option instead,
-
max_nesting: sets the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum should be checked.
-
allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.
-
quirks_mode: Enables quirks_mode for parser, that is for example generating single JSON values instead of documents is possible.
498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/gistto/enhancements/json.rb', line 498 def initialize(opts = {}) @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @allow_nan = false @ascii_only = false @quirks_mode = false @buffer_initial_length = 1024 configure opts end |
Instance Attribute Details
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
526 527 528 |
# File 'lib/gistto/enhancements/json.rb', line 526 def array_nl @array_nl end |
#buffer_initial_length ⇒ Object
:stopdoc:
537 538 539 |
# File 'lib/gistto/enhancements/json.rb', line 537 def buffer_initial_length @buffer_initial_length end |
#depth ⇒ Object
This integer returns the current depth data structure nesting in the generated JSON.
548 549 550 |
# File 'lib/gistto/enhancements/json.rb', line 548 def depth @depth end |
#indent ⇒ Object
This string is used to indent levels in the JSON text.
512 513 514 |
# File 'lib/gistto/enhancements/json.rb', line 512 def indent @indent end |
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
530 531 532 |
# File 'lib/gistto/enhancements/json.rb', line 530 def max_nesting @max_nesting end |
#object_nl ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
523 524 525 |
# File 'lib/gistto/enhancements/json.rb', line 523 def object_nl @object_nl end |
#quirks_mode ⇒ Object
If this attribute is set to true, quirks mode is enabled, otherwise it’s disabled.
534 535 536 |
# File 'lib/gistto/enhancements/json.rb', line 534 def quirks_mode @quirks_mode end |
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
516 517 518 |
# File 'lib/gistto/enhancements/json.rb', line 516 def space @space end |
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
519 520 521 |
# File 'lib/gistto/enhancements/json.rb', line 519 def space_before @space_before 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.
468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/gistto/enhancements/json.rb', line 468 def self.from_state(opts) case when self === opts opts when opts.respond_to?(:to_hash) new(opts.to_hash) when opts.respond_to?(:to_h) new(opts.to_h) else SAFE_STATE_PROTOTYPE.dup end end |
Instance Method Details
#[](name) ⇒ Object
Return the value returned by method name.
629 630 631 |
# File 'lib/gistto/enhancements/json.rb', line 629 def [](name) __send__ name end |
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
565 566 567 |
# File 'lib/gistto/enhancements/json.rb', line 565 def allow_nan? @allow_nan end |
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated. Otherwise returns false.
571 572 573 |
# File 'lib/gistto/enhancements/json.rb', line 571 def ascii_only? @ascii_only end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
559 560 561 |
# File 'lib/gistto/enhancements/json.rb', line 559 def check_circular? !@max_nesting.zero? end |
#check_max_nesting ⇒ Object
:nodoc:
550 551 552 553 554 555 |
# File 'lib/gistto/enhancements/json.rb', line 550 def check_max_nesting # :nodoc: return if @max_nesting.zero? current_nesting = depth + 1 current_nesting > @max_nesting and raise NestingError, "nesting of #{current_nesting} is too deep" end |
#configure(opts) ⇒ Object Also known as: merge
Configure this State instance with the Hash opts, and return itself.
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/gistto/enhancements/json.rb', line 582 def configure(opts) @indent = opts[:indent] if opts.key?(:indent) @space = opts[:space] if opts.key?(:space) @space_before = opts[:space_before] if opts.key?(:space_before) @object_nl = opts[:object_nl] if opts.key?(:object_nl) @array_nl = opts[:array_nl] if opts.key?(:array_nl) @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only) @depth = opts[:depth] || 0 @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode) if !opts.key?(:max_nesting) # defaults to 19 @max_nesting = 19 elsif opts[:max_nesting] @max_nesting = opts[:max_nesting] else @max_nesting = 0 end self end |
#generate(obj) ⇒ Object
Generates a valid JSON document from object obj and returns the result. If no valid JSON document can be created this method raises a GeneratorError exception.
616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/gistto/enhancements/json.rb', line 616 def generate(obj) result = obj.to_json(self) unless @quirks_mode unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ || result =~ /\A\s*\{/ && result =~ /\}\s*\Z/ then raise GeneratorError, "only generation of JSON objects or arrays allowed" end end result end |
#quirks_mode? ⇒ Boolean
Returns true, if quirks mode is enabled. Otherwise returns false.
576 577 578 |
# File 'lib/gistto/enhancements/json.rb', line 576 def quirks_mode? @quirks_mode end |
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
605 606 607 608 609 610 611 |
# File 'lib/gistto/enhancements/json.rb', line 605 def to_h result = {} for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth] result[iv.intern] = instance_variable_get("@#{iv}") end result end |