class Console::Format::Safe
A safe format for converting objects to strings.
Handles issues like circular references, encoding errors, excessive nesting depth, and excessive output size.
Constants
- TRUNCATED
-
The
JSONfragment used as the truncation marker when dropped fields cannot be named.
Attributes
@attribute [Integer] The maximum depth to recurse into objects.
@attribute [Integer | Nil] The maximum byte size of the serialized output.
Public Class Methods
Source
# File vendor/bundle/ruby/4.0.0/gems/console-1.36.0/lib/console/format/safe.rb, line 25 def initialize(format: ::JSON, depth_limit: 12, size_limit: 16 * 1024, encoding: ::Encoding::UTF_8, limit: nil) if limit warn "Console::Format::Safe `limit:` is deprecated, use `depth_limit:` instead.", uplevel: 1, category: :deprecated depth_limit = limit end @format = format @depth_limit = depth_limit @size_limit = size_limit @encoding = encoding end
Create a new safe format.
@parameter format [JSON] The format to use for serialization. @parameter depth_limit [Integer] The maximum depth to recurse into objects (the JSON max_nesting). @parameter size_limit [Integer | Nil] The maximum byte size of the serialized output, or nil to disable size limiting. Limits below {TRUNCATED} (the minimal marker) cannot be honoured. @parameter encoding [Encoding] The encoding to use for strings. @parameter limit [Integer | Nil] Deprecated alias for depth_limit.
Public Instance Methods
Source
# File vendor/bundle/ruby/4.0.0/gems/console-1.36.0/lib/console/format/safe.rb, line 52 def dump(object) buffer = @format.dump(object, @depth_limit) if @size_limit and buffer.bytesize > @size_limit return safe_dump(object) end return buffer rescue SystemStackError, StandardError return safe_dump(object) end
Dump the given object to a string.
The common case is a single fast serialization. If that fails (e.g. circular references, excessive nesting, or encoding errors) or its output exceeds {size_limit}, it falls back to {safe_dump}, which rebuilds the record field-by-field within the limit.
@parameter object [Object] The object to dump. @returns [String] The dumped object.