Class: StreamingJsonReader

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/streaming_import.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (StreamingJsonReader) initialize(filename)

Returns a new instance of StreamingJsonReader



9
10
11
12
# File 'backend/app/lib/streaming_import.rb', line 9

def initialize(filename)
  @filename = filename
  @count = nil
end

Instance Attribute Details

- (Object) count (readonly)

Returns the value of attribute count



7
8
9
# File 'backend/app/lib/streaming_import.rb', line 7

def count
  @count
end

Instance Method Details

- (Object) each(determine_count = false)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'backend/app/lib/streaming_import.rb', line 20

def each(determine_count = false)
  return if empty?

  stream = java.io.FileReader.new(@filename)
  @count = 0 if determine_count

  begin
    mapper = org.codehaus.jackson.map.ObjectMapper.new

    # Skip the opening [
    stream.read

    parser = mapper.getJsonFactory.createJsonParser(stream)

    while parser.nextToken
      result = parser.readValueAs(java.util.Map.java_class)
      @count += 1 if determine_count
      yield(result)

      begin
        puts parser.nextToken
      rescue org.codehaus.jackson.JsonParseException
        # Skip over the pesky commas
      end
    end
  # rescue
  #   raise JSON::ParserError.new($!)

  ensure
    stream.close
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


15
16
17
# File 'backend/app/lib/streaming_import.rb', line 15

def empty?
  File.size(@filename) <= 2
end