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).
-
#rfc8785 ⇒ Object
writeonly
Controls whether RFC8785 (canonicalization) mode is enabled.
-
#rfc8785_number_formatter_proc ⇒ Object
:nodoc:.
-
#rfc8785_sort_keys_proc ⇒ Object
:nodoc:.
-
#script_safe ⇒ Object
If this attribute is set to true, script tags and HTML comments 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.
-
#rfc8785? ⇒ Boolean
Returns true, if RFC8785 (canonicalization) mode is enabled.
-
#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.
-
#validate_rfc8785 ⇒ Object
:nodoc:.
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 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.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/json/truffle_ruby/generator.rb', line 145 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 @rfc8785 = 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.
180 181 182 |
# File 'lib/json/truffle_ruby/generator.rb', line 180 def array_nl @array_nl end |
#as_json ⇒ Object
This proc converts unsupported types into native JSON types.
183 184 185 |
# File 'lib/json/truffle_ruby/generator.rb', line 183 def as_json @as_json end |
#buffer_initial_length ⇒ Object
:stopdoc:
237 238 239 |
# File 'lib/json/truffle_ruby/generator.rb', line 237 def buffer_initial_length @buffer_initial_length end |
#default_sort_keys_proc ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/json/truffle_ruby/generator.rb', line 102 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.
248 249 250 |
# File 'lib/json/truffle_ruby/generator.rb', line 248 def depth @depth end |
#indent ⇒ Object
This string is used to indent levels in the JSON text.
166 167 168 |
# File 'lib/json/truffle_ruby/generator.rb', line 166 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).
177 178 179 |
# File 'lib/json/truffle_ruby/generator.rb', line 177 def object_nl @object_nl end |
#rfc8785=(value) ⇒ Object (writeonly)
Controls whether RFC8785 (canonicalization) mode is enabled. Otherwise returns false.
213 214 215 |
# File 'lib/json/truffle_ruby/generator.rb', line 213 def rfc8785=(value) @rfc8785 = value end |
#rfc8785_number_formatter_proc ⇒ Object
:nodoc:
103 104 105 |
# File 'lib/json/truffle_ruby/generator.rb', line 103 def rfc8785_number_formatter_proc @rfc8785_number_formatter_proc end |
#rfc8785_sort_keys_proc ⇒ Object
:nodoc:
104 105 106 |
# File 'lib/json/truffle_ruby/generator.rb', line 104 def rfc8785_sort_keys_proc @rfc8785_sort_keys_proc end |
#script_safe ⇒ Object
If this attribute is set to true, script tags and HTML comments 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.
170 171 172 |
# File 'lib/json/truffle_ruby/generator.rb', line 170 def space @space end |
#space_before ⇒ Object
This string is used to insert a space before the ':' in JSON objects.
173 174 175 |
# File 'lib/json/truffle_ruby/generator.rb', line 173 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.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/json/truffle_ruby/generator.rb', line 114 def self.from_state(opts) if opts case when self === opts return opts.validate_rfc8785 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
106 107 108 |
# File 'lib/json/truffle_ruby/generator.rb', line 106 def self.generate(obj, opts = nil, io = nil) new(opts).generate(obj, io) end |
Instance Method Details
#allow_duplicate_key? ⇒ Boolean
:nodoc:
369 370 371 |
# File 'lib/json/truffle_ruby/generator.rb', line 369 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.
272 273 274 |
# File 'lib/json/truffle_ruby/generator.rb', line 272 def allow_nan? @allow_nan end |
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated. Otherwise returns false.
278 279 280 |
# File 'lib/json/truffle_ruby/generator.rb', line 278 def ascii_only? @ascii_only end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
266 267 268 |
# File 'lib/json/truffle_ruby/generator.rb', line 266 def check_circular? !@max_nesting.zero? end |
#check_max_nesting ⇒ Object
:nodoc:
257 258 259 260 261 262 |
# File 'lib/json/truffle_ruby/generator.rb', line 257 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.
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/json/truffle_ruby/generator.rb', line 317 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.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/json/truffle_ruby/generator.rb', line 402 def generate(obj, anIO = nil) return dup.generate(obj, anIO) if frozen? depth = @depth validate_rfc8785 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 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 |
#rfc8785? ⇒ Boolean
Returns true, if RFC8785 (canonicalization) mode is enabled. Otherwise returns false.
295 296 297 |
# File 'lib/json/truffle_ruby/generator.rb', line 295 def rfc8785? @rfc8785 end |
#script_safe? ⇒ Boolean
Returns true, if forward slashes are escaped. Otherwise returns false.
283 284 285 |
# File 'lib/json/truffle_ruby/generator.rb', line 283 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.
290 291 292 |
# File 'lib/json/truffle_ruby/generator.rb', line 290 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.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/json/truffle_ruby/generator.rb', line 375 def to_h { indent: @indent, space: @space, space_before: @space_before, object_nl: @object_nl, array_nl: @array_nl, as_json: @as_json, allow_nan: @allow_nan, ascii_only: @ascii_only, max_nesting: @max_nesting, script_safe: @script_safe, strict: @strict, depth: @depth, buffer_initial_length: @buffer_initial_length, sort_keys: @sort_keys, allow_duplicate_key: @allow_duplicate_key, rfc8785: @rfc8785, } end |
#validate_rfc8785 ⇒ Object
:nodoc:
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/json/truffle_ruby/generator.rb', line 299 def validate_rfc8785 # :nodoc: if @rfc8785 option = if !@indent.empty? then :indent elsif !@space.empty? then :space elsif !@space_before.empty? then :space_before elsif !@object_nl.empty? then :object_nl elsif !@array_nl.empty? then :array_nl elsif @ascii_only then :ascii_only elsif @script_safe then :script_safe elsif @allow_nan then :allow_nan end raise ArgumentError, "#{option} cannot be used with rfc8785" if option end self end |