Class: JSON::Pure::Generator::State
- Defined in:
- lib/json/pure/generator.rb
Overview
This class is used to create State instances, that are use to hold data while generating a JSON text from a 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.
-
#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).
-
#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
-
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
-
#check_max_nesting ⇒ Object
:nodoc:.
-
#configure(opts) ⇒ Object
Configure this State instance with the Hash opts, and return itself.
-
#generate(obj) ⇒ Object
Generates a valid JSON document from object
obj
and returns the result. -
#initialize(opts = {}) ⇒ State
constructor
Instantiates a new State object, configured by opts.
-
#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.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/json/pure/generator.rb', line 134 def initialize(opts = {}) @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @allow_nan = false @ascii_only = false configure opts end |
Instance Attribute Details
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
160 161 162 |
# File 'lib/json/pure/generator.rb', line 160 def array_nl @array_nl end |
#depth ⇒ Object
This integer returns the current depth data structure nesting in the generated JSON.
168 169 170 |
# File 'lib/json/pure/generator.rb', line 168 def depth @depth end |
#indent ⇒ Object
This string is used to indent levels in the JSON text.
146 147 148 |
# File 'lib/json/pure/generator.rb', line 146 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.
164 165 166 |
# File 'lib/json/pure/generator.rb', line 164 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).
157 158 159 |
# File 'lib/json/pure/generator.rb', line 157 def object_nl @object_nl end |
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
150 151 152 |
# File 'lib/json/pure/generator.rb', line 150 def space @space end |
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
153 154 155 |
# File 'lib/json/pure/generator.rb', line 153 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.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/json/pure/generator.rb', line 108 def self.from_state(opts) case opts when self opts when Hash new(opts) else SAFE_STATE_PROTOTYPE.dup end end |
Instance Method Details
#[](name) ⇒ Object
Return the value returned by method name
.
236 237 238 |
# File 'lib/json/pure/generator.rb', line 236 def [](name) __send__ name end |
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
185 186 187 |
# File 'lib/json/pure/generator.rb', line 185 def allow_nan? @allow_nan end |
#ascii_only? ⇒ Boolean
189 190 191 |
# File 'lib/json/pure/generator.rb', line 189 def ascii_only? @ascii_only end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
179 180 181 |
# File 'lib/json/pure/generator.rb', line 179 def check_circular? !@max_nesting.zero? end |
#check_max_nesting ⇒ Object
:nodoc:
170 171 172 173 174 175 |
# File 'lib/json/pure/generator.rb', line 170 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
Configure this State instance with the Hash opts, and return itself.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/json/pure/generator.rb', line 195 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 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.
227 228 229 230 231 232 233 |
# File 'lib/json/pure/generator.rb', line 227 def generate(obj) result = obj.to_json(self) if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m raise GeneratorError, "only generation of JSON objects or arrays allowed" end result end |
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
216 217 218 219 220 221 222 |
# File 'lib/json/pure/generator.rb', line 216 def to_h result = {} for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth] result[iv.intern] = instance_variable_get("@#{iv}") end result end |