Class: K8s::JSONParser::Yajl

Inherits:
Object
  • Object
show all
Defined in:
lib/k8s/json_parser/yajl.rb

Overview

Yajl JSON-parsing backend, with the native extension it’s faster than the pure-ruby standard library JSON-parser.

To use install, add ‘gem ’yajl-ruby’‘ to your gemfile and require this class: `require ’k8s/json_parser/yajl’‘

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Yajl

Instantiate a streaming parser.

Examples:

result = []
parser = K8s::JSONParser::Yajl.new do |obj|
  result << obj
end
parser << '{"hello":"world"\n}'
parser << '{"hello":"world"\n}'
puts result.inspect
# outputs:
# [{ 'hello' => 'world' }, { 'hello' => 'world' }]


29
30
31
32
33
# File 'lib/k8s/json_parser/yajl.rb', line 29

def initialize(&block)
  @parser = ::Yajl::Parser.new.tap do |parser|
    parser.on_parse_complete = block
  end
end

Class Method Details

.parse(input) ⇒ Hash, ...

Returns the parsed result.

Parameters:

  • input (String)

    JSON content

Returns:

  • (Hash, Array, String, Integer, Float, DateTime)

    the parsed result



13
14
15
# File 'lib/k8s/json_parser/yajl.rb', line 13

def self.parse(input)
  ::Yajl::Parser.parse(input)
end

Instance Method Details

#<<(data) ⇒ Object

The block passed to #new will be executed for each complete document

Parameters:

  • data (String)

    partial JSON content



37
38
39
# File 'lib/k8s/json_parser/yajl.rb', line 37

def <<(data)
  @parser << data
end