Class: JSON::TruffleRuby::Generator::State
- Inherits:
-
Object
- Object
- 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:.
-
#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.
-
#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
-
#[](name) ⇒ Object
Return the value returned by method
name. - #[]=(name, value) ⇒ Object
-
#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(opts) ⇒ 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. -
#generate_new(obj, anIO = nil) ⇒ Object
:nodoc:.
-
#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.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/json/truffle_ruby/generator.rb', line 148 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 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:
198 199 200 |
# File 'lib/json/truffle_ruby/generator.rb', line 198 def buffer_initial_length @buffer_initial_length end |
#depth ⇒ Object
This integer returns the current depth data structure nesting in the generated JSON.
209 210 211 |
# File 'lib/json/truffle_ruby/generator.rb', line 209 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.
187 188 189 |
# File 'lib/json/truffle_ruby/generator.rb', line 187 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 |
#script_safe ⇒ Object
If this attribute is set to true, forward slashes will be escaped in all json strings.
191 192 193 |
# File 'lib/json/truffle_ruby/generator.rb', line 191 def script_safe @script_safe 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
195 196 197 |
# File 'lib/json/truffle_ruby/generator.rb', line 195 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/json/truffle_ruby/generator.rb', line 117 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
109 110 111 |
# File 'lib/json/truffle_ruby/generator.rb', line 109 def self.generate(obj, opts = nil, io = nil) new(opts).generate(obj, io) end |
Instance Method Details
#[](name) ⇒ Object
Return the value returned by method name.
432 433 434 435 436 437 438 439 |
# File 'lib/json/truffle_ruby/generator.rb', line 432 def [](name) if respond_to?(name) __send__(name) else instance_variable_get("@#{name}") if instance_variables.include?("@#{name}".to_sym) # avoid warning end end |
#[]=(name, value) ⇒ Object
441 442 443 444 445 446 447 |
# File 'lib/json/truffle_ruby/generator.rb', line 441 def []=(name, value) if respond_to?(name_writer = "#{name}=") __send__ name_writer, value else instance_variable_set "@#{name}", value end end |
#allow_duplicate_key? ⇒ Boolean
:nodoc:
301 302 303 |
# File 'lib/json/truffle_ruby/generator.rb', line 301 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.
226 227 228 |
# File 'lib/json/truffle_ruby/generator.rb', line 226 def allow_nan? @allow_nan end |
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated. Otherwise returns false.
232 233 234 |
# File 'lib/json/truffle_ruby/generator.rb', line 232 def ascii_only? @ascii_only end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures are checked, otherwise returns false.
220 221 222 |
# File 'lib/json/truffle_ruby/generator.rb', line 220 def check_circular? !@max_nesting.zero? end |
#check_max_nesting ⇒ Object
:nodoc:
211 212 213 214 215 216 |
# File 'lib/json/truffle_ruby/generator.rb', line 211 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.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/json/truffle_ruby/generator.rb', line 250 def configure(opts) if opts.respond_to?(:to_hash) opts = opts.to_hash elsif opts.respond_to?(:to_h) opts = opts.to_h else raise TypeError, "can't convert #{opts.class} into Hash" end opts.each do |key, value| instance_variable_set "@#{key}", value end # NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json @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) @as_json = opts[:as_json].to_proc if opts[:as_json] @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only) @depth = opts[:depth] || 0 @buffer_initial_length ||= opts[:buffer_initial_length] @script_safe = if opts.key?(:script_safe) !!opts[:script_safe] elsif opts.key?(:escape_slash) !!opts[:escape_slash] else false end if opts.key?(:allow_duplicate_key) @allow_duplicate_key = !!opts[:allow_duplicate_key] else @allow_duplicate_key = nil # nil is deprecation end @strict = !!opts[:strict] if opts.key?(:strict) if !opts.key?(:max_nesting) # defaults to 100 @max_nesting = 100 elsif opts[:max_nesting] @max_nesting = opts[:max_nesting] else @max_nesting = 0 end self 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.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/json/truffle_ruby/generator.rb', line 327 def generate(obj, anIO = nil) 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) 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 end |
#generate_new(obj, anIO = nil) ⇒ Object
:nodoc:
346 347 348 |
# File 'lib/json/truffle_ruby/generator.rb', line 346 def generate_new(obj, anIO = nil) # :nodoc: dup.generate(obj, anIO) end |
#script_safe? ⇒ Boolean
Returns true, if forward slashes are escaped. Otherwise returns false.
237 238 239 |
# File 'lib/json/truffle_ruby/generator.rb', line 237 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.
244 245 246 |
# File 'lib/json/truffle_ruby/generator.rb', line 244 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.
307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/json/truffle_ruby/generator.rb', line 307 def to_h result = {} instance_variables.each do |iv| iv = iv.to_s[1..-1] result[iv.to_sym] = self[iv] end if result[:allow_duplicate_key].nil? result.delete(:allow_duplicate_key) end result end |