Class: JSON::Pure::Generator::State

Inherits:
Object
  • Object
show all
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 Ruby data structure.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/json/pure/generator.rb', line 159

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_nlObject

This string is put at the end of a line that holds a JSON array.



187
188
189
# File 'lib/json/pure/generator.rb', line 187

def array_nl
  @array_nl
end

#buffer_initial_lengthObject

:stopdoc:



198
199
200
# File 'lib/json/pure/generator.rb', line 198

def buffer_initial_length
  @buffer_initial_length
end

#depthObject

This integer returns the current depth data structure nesting in the generated JSON.



209
210
211
# File 'lib/json/pure/generator.rb', line 209

def depth
  @depth
end

#indentObject

This string is used to indent levels in the JSON text.



173
174
175
# File 'lib/json/pure/generator.rb', line 173

def indent
  @indent
end

#max_nestingObject

This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.



191
192
193
# File 'lib/json/pure/generator.rb', line 191

def max_nesting
  @max_nesting
end

#object_nlObject

This string is put at the end of a line that holds a JSON object (or Hash).



184
185
186
# File 'lib/json/pure/generator.rb', line 184

def object_nl
  @object_nl
end

#quirks_modeObject

If this attribute is set to true, quirks mode is enabled, otherwise it’s disabled.



195
196
197
# File 'lib/json/pure/generator.rb', line 195

def quirks_mode
  @quirks_mode
end

#spaceObject

This string is used to insert a space between the tokens in a JSON string.



177
178
179
# File 'lib/json/pure/generator.rb', line 177

def space
  @space
end

#space_beforeObject

This string is used to insert a space before the ‘:’ in JSON objects.



180
181
182
# File 'lib/json/pure/generator.rb', line 180

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.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/json/pure/generator.rb', line 129

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.



307
308
309
310
311
312
313
# File 'lib/json/pure/generator.rb', line 307

def [](name)
  if respond_to?(name)
    __send__(name)
  else
    instance_variable_get("@#{name}")
  end
end

#[]=(name, value) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/json/pure/generator.rb', line 315

def []=(name, value)
  if respond_to?(name_writer = "#{name}=")
    __send__ name_writer, value
  else
    instance_variable_set "@#{name}", value
  end
end

#allow_nan?Boolean

Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.

Returns:

  • (Boolean)


226
227
228
# File 'lib/json/pure/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.

Returns:

  • (Boolean)


232
233
234
# File 'lib/json/pure/generator.rb', line 232

def ascii_only?
  @ascii_only
end

#check_circular?Boolean

Returns true, if circular data structures are checked, otherwise returns false.

Returns:

  • (Boolean)


220
221
222
# File 'lib/json/pure/generator.rb', line 220

def check_circular?
  !@max_nesting.zero?
end

#check_max_nestingObject

:nodoc:



211
212
213
214
215
216
# File 'lib/json/pure/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.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/json/pure/generator.rb', line 243

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
  for key, value in opts
    instance_variable_set "@#{key}", value
  end
  @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)
  @buffer_initial_length ||= opts[:buffer_initial_length]

  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) ⇒ 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.



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/json/pure/generator.rb', line 292

def generate(obj)
  result = obj.to_json(self)
  JSON.valid_utf8?(result) or raise GeneratorError,
    "source sequence #{result.inspect} is illegal/malformed utf-8"
  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.

Returns:

  • (Boolean)


237
238
239
# File 'lib/json/pure/generator.rb', line 237

def quirks_mode?
  @quirks_mode
end

#to_hObject Also known as: to_hash

Returns the configuration instance variables as a hash, that can be passed to the configure method.



278
279
280
281
282
283
284
285
# File 'lib/json/pure/generator.rb', line 278

def to_h
  result = {}
  for iv in instance_variables
    iv = iv.to_s[1..-1]
    result[iv.to_sym] = self[iv]
  end
  result
end