Class: Fluent::Plugin::JsonSizeLimit::JsonString

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/json_size_limit/json_string.rb

Overview

Computes and truncates the bytes between a JSON string's quotes. Plain UTF-8 takes a zero-serialization path; escaped UTF-8 is generated once and decoded at a safe JSON token boundary.

Direct Known Subclasses

RecordReducer::StringLocation

Constant Summary collapse

ESCAPED_CHARACTERS =
"\"\\\\\x00-\x1f"
ESCAPE_REQUIRED =
/["\\\x00-\x1f]/
COUNT_SCAN_THRESHOLD =
1024
UTF_8 =
Encoding::UTF_8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, validate_encoding: true) ⇒ JsonString

Returns a new instance of JsonString.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 30

def initialize(value, validate_encoding: true)
  @value = value
  StringEncoding.validate!(value) if validate_encoding

  if raw_json_content?
    @strategy = :raw
    @content_bytes = value.bytesize
  else
    @generated = JSON.generate(value)
    @content_bytes = @generated.bytesize - 2
    @strategy = directly_decodable? ? :generated : :fallback
  end
end

Instance Attribute Details

#content_bytesObject (readonly)

Returns the value of attribute content_bytes.



18
19
20
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 18

def content_bytes
  @content_bytes
end

#truncated_content_bytesObject (readonly)

Returns the value of attribute truncated_content_bytes.



18
19
20
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 18

def truncated_content_bytes
  @truncated_content_bytes
end

#valueObject (readonly)

Returns the value of attribute value.



18
19
20
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 18

def value
  @value
end

Class Method Details

.content_bytes(value) ⇒ Object



20
21
22
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 20

def self.content_bytes(value)
  new(value).content_bytes
end

.truncate(value, allowed_content_bytes) ⇒ Object



24
25
26
27
28
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 24

def self.truncate(value, allowed_content_bytes)
  json_string = new(value)
  truncated_value = json_string.truncate(allowed_content_bytes)
  [truncated_value, json_string.truncated_content_bytes]
end

Instance Method Details

#truncate(allowed_content_bytes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/json_size_limit/json_string.rb', line 44

def truncate(allowed_content_bytes)
  if allowed_content_bytes <= 0
    @truncated_content_bytes = 0
    return empty_value
  end

  if allowed_content_bytes >= @content_bytes
    @truncated_content_bytes = @content_bytes
    return @value
  end

  case @strategy
  when :raw
    truncate_raw(allowed_content_bytes)
  when :generated
    truncate_generated(allowed_content_bytes)
  when :fallback
    truncate_with_binary_search(allowed_content_bytes)
  end
end