Class: Serialbench::Serializers::Json::YajlSerializer

Inherits:
BaseJsonSerializer show all
Defined in:
lib/serialbench/serializers/json/yajl_serializer.rb

Instance Method Summary collapse

Methods inherited from BaseJsonSerializer

#available?, #features, format, #generate_json, #parse_object, #supports_custom_types?, #supports_generation?, #supports_pretty_print?, #supports_symbol_keys?

Methods inherited from BaseSerializer

#get_version, #initialize, #require_library, #stream_parse

Constructor Details

This class inherits a constructor from Serialbench::Serializers::BaseSerializer

Instance Method Details

#generate(data, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 18

def generate(data, options = {})
  require 'yajl'
  if options[:pretty]
    Yajl::Encoder.encode(data, pretty: true, indent: '  ')
  else
    Yajl::Encoder.encode(data)
  end
end

#library_require_nameObject



63
64
65
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 63

def library_require_name
  'yajl'
end

#nameObject



9
10
11
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 9

def name
  'yajl'
end

#parse(json_string) ⇒ Object



13
14
15
16
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 13

def parse(json_string)
  require 'yajl'
  Yajl::Parser.parse(json_string)
end

#parse_streaming(json_string, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 27

def parse_streaming(json_string, &block)
  require 'yajl'

  parser = Yajl::Parser.new
  parser.on_parse_complete = block if block

  # Parse the JSON string
  result = parser.parse(json_string)

  # Return number of top-level objects processed
  case result
  when Array
    result.length
  when Hash
    1
  else
    1
  end
end

#supports_streaming?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 47

def supports_streaming?
  true
end

#versionObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/serialbench/serializers/json/yajl_serializer.rb', line 51

def version
  return 'unknown' unless available?

  require 'yajl'
  # YAJL doesn't have a VERSION constant, try to get gem version
  begin
    Gem.loaded_specs['yajl-ruby']&.version&.to_s || 'unknown'
  rescue StandardError
    'unknown'
  end
end