Class: JSON::TruffleRuby::Generator::State
- Defined in:
- lib/json/truffle_ruby/generator.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.
-
#as_json ⇒ Object
This proc converts unsupported types into native JSON types.
-
#buffer_initial_length ⇒ Object
:stopdoc:.
-
#default_sort_keys_proc ⇒ Object
:nodoc:.
-
#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).
-
#script_safe ⇒ Object
If this attribute is set to true, forward slashes will be escaped in all json strings.
-
#sort_keys ⇒ Object
Controls key sorting in the generated JSON.
-
#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.
-
#strict ⇒ Object
If this attribute is set to true, attempting to serialize types not supported by the JSON spec will raise a JSON::GeneratorError.
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.
- .generate(obj, opts = nil, io = nil) ⇒ Object
Instance Method Summary collapse
-
#allow_duplicate_key? ⇒ Boolean
:nodoc:.
-
#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(options) ⇒ Object
(also: #merge)
Configure this State instance with the Hash opts, and return itself.
-
#generate(obj, anIO = nil) ⇒ Object
Generates a valid JSON document from object
objand returns the result. -
#initialize(opts = nil) ⇒ State
constructor
Instantiates a new State object, configured by opts.
-
#script_safe? ⇒ Boolean
Returns true, if forward slashes are escaped.
-
#strict? ⇒ Boolean
Returns true, if strict mode is enabled.
-
#to_h ⇒ Object
(also: #to_hash)
Returns the configuration instance variables as a hash, that can be passed to the configure method.
Constructor Details
#initialize(opts = nil) ⇒ 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: ''),
- script_safe: true if U+2028, U+2029 and forward slash (/) should be escaped as to make the JSON object safe to interpolate in a script tag (default: false).
- 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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/json/truffle_ruby/generator.rb', line 155 def initialize(opts = nil) @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @allow_nan = false @ascii_only = false @as_json = false @depth = 0 @buffer_initial_length = 1024 @script_safe = false @strict = false @max_nesting = 100 @sort_keys = false @allow_duplicate_key = false _configure(**opts) if opts end |
Instance Attribute Details
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
189 190 191 |
# File 'lib/json/truffle_ruby/generator.rb', line 189 def array_nl @array_nl end |
#as_json ⇒ Object
This proc converts unsupported types into native JSON types.
192 193 194 |
# File 'lib/json/truffle_ruby/generator.rb', line 192 def as_json @as_json end |
#buffer_initial_length ⇒ Object
:stopdoc:
234 235 236 |
# File 'lib/json/truffle_ruby/generator.rb', line 234 def buffer_initial_length @buffer_initial_length end |
#default_sort_keys_proc ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/json/truffle_ruby/generator.rb', line 114 def default_sort_keys_proc @default_sort_keys_proc end |
#depth ⇒ Object
This integer returns the current depth data structure nesting in the generated JSON.
245 246 247 |
# File 'lib/json/truffle_ruby/generator.rb', line 245 def depth @depth end |
#indent ⇒ Object
This string is used to indent levels in the JSON text.
175 176 177 |
# File 'lib/json/truffle_ruby/generator.rb', line 175 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.
196 197 198 |
# File 'lib/json/truffle_ruby/generator.rb', line 196 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).
186 187 188 |
# File 'lib/json/truffle_ruby/generator.rb', line 186 def object_nl @object_nl end |
#script_safe ⇒ Object
If this attribute is set to true, forward slashes will be escaped in all json strings.
200 201 202 |
# File 'lib/json/truffle_ruby/generator.rb', line 200 def script_safe @script_safe end |
#sort_keys ⇒ Object
Controls key sorting in the generated JSON. If set to true, object
keys are sorted by key lexicographically. If set to a Proc, it
receives the entire Hash and must return a Hash with its pairs in the
desired order.
210 211 212 |
# File 'lib/json/truffle_ruby/generator.rb', line 210 def sort_keys @sort_keys end |
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
179 180 181 |
# File 'lib/json/truffle_ruby/generator.rb', line 179 def space @space end |
#space_before ⇒ Object
This string is used to insert a space before the ':' in JSON objects.
182 183 184 |
# File 'lib/json/truffle_ruby/generator.rb', line 182 def space_before @space_before end |
#strict ⇒ Object
If this attribute is set to true, attempting to serialize types not supported by the JSON spec will raise a JSON::GeneratorError
204 205 206 |
# File 'lib/json/truffle_ruby/generator.rb', line 204 def strict @strict 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/json/truffle_ruby/generator.rb', line 124 def self.from_state(opts) if opts case when self === opts return opts when opts.respond_to?(:to_hash) return new(opts.to_hash) when opts.respond_to?(:to_h) return new(opts.to_h) end end new end |
.generate(obj, opts = nil, io = nil) ⇒ Object
116 117 118 |
# File 'lib/json/truffle_ruby/generator.rb', line 116 def self.generate(obj, opts = nil, io = nil) new(opts).generate(obj, io) end |
Instance Method Details
#allow_duplicate_key? ⇒ Boolean
:nodoc:
336 337 338 |
# File 'lib/json/truffle_ruby/generator.rb', line 336 def allow_duplicate_key? # :nodoc: @allow_duplicate_key end |
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
269 270 271 |
# File 'lib/json/truffle_ruby/generator.rb', line 269 def allow_nan? @allow_nan end |
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated. Otherwise returns false.
275 276 277 |
# File 'lib/json/truffle_ruby/generator.rb', line 275 def ascii_only? @ascii_only end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
263 264 265 |
# File 'lib/json/truffle_ruby/generator.rb', line 263 def check_circular? !@max_nesting.zero? end |
#check_max_nesting ⇒ Object
:nodoc:
254 255 256 257 258 259 |
# File 'lib/json/truffle_ruby/generator.rb', line 254 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. Did you try to serialize objects with circular references?" end |
#configure(options) ⇒ Object Also known as: merge
Configure this State instance with the Hash opts, and return itself.
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/json/truffle_ruby/generator.rb', line 293 def configure() unless Hash == if .respond_to?(:to_hash) = .to_hash elsif .respond_to?(:to_h) = .to_h end end _configure(**) end |
#generate(obj, anIO = nil) ⇒ 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.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/json/truffle_ruby/generator.rb', line 358 def generate(obj, anIO = nil) return dup.generate(obj, anIO) if frozen? depth = @depth if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj) and !@sort_keys result = generate_json(obj, ''.dup) else if @sort_keys obj = @sort_keys.call(obj) end result = obj.to_json(self) end JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError.new( "source sequence #{result.inspect} is illegal/malformed utf-8", obj ) if anIO anIO.write(result) anIO else result end ensure @depth = depth unless frozen? end |
#script_safe? ⇒ Boolean
Returns true, if forward slashes are escaped. Otherwise returns false.
280 281 282 |
# File 'lib/json/truffle_ruby/generator.rb', line 280 def script_safe? @script_safe end |
#strict? ⇒ Boolean
Returns true, if strict mode is enabled. Otherwise returns false. Strict mode only allow serializing JSON native types: Hash, Array, String, Integer, Float, true, false and nil.
287 288 289 |
# File 'lib/json/truffle_ruby/generator.rb', line 287 def strict? @strict end |
#to_h ⇒ Object Also known as: to_hash
Returns the configuration instance variables as a hash, that can be passed to the configure method.
342 343 344 345 346 347 348 349 350 |
# File 'lib/json/truffle_ruby/generator.rb', line 342 def to_h result = {} instance_variables.each do |iv| key = iv.to_s[1..-1] result[key.to_sym] = instance_variable_get(iv) end result end |