Class: Embulk::Guess::JsonGuessPlugin

Inherits:
Embulk::GuessPlugin show all
Defined in:
lib/embulk/guess/json.rb

Instance Method Summary collapse

Methods inherited from Embulk::GuessPlugin

from_java, new_java

Instance Method Details

#guess(config, sample_buffer) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/embulk/guess/json.rb', line 14

def guess(config, sample_buffer)
  return {} unless config.fetch("parser", {}).fetch("type", "json") == "json"

  # Use org.embulk.spi.json.JsonParser to respond to multi-line Json
  json_parser = new_json_parser(sample_buffer)
  one_json_parsed = false
  begin
    while (v = json_parser.next)
      # "v" needs to be JSON object type (isMapValue) because:
      # 1) Single-column CSV can be mis-guessed as JSON if JSON non-objects are accepted.
      # 2) JsonParserPlugin accepts only the JSON object type.
      raise JsonParseException.new("v must be JSON object type") unless v.isMapValue
      one_json_parsed = true
    end
  rescue JsonParseException
    # the exception is ignored
  end

  if one_json_parsed
    return {"parser" => {"type" => "json"}} # if JsonParser can parse even one JSON data
  else
    return {}
  end
end