Class: Mysql2xxxx::JSON

Inherits:
Writer
  • Object
show all
Defined in:
lib/mysql2xxxx/writer/json.rb

Instance Attribute Summary

Attributes inherited from Writer

#properties

Instance Method Summary collapse

Methods inherited from Writer

#close, #dbh, #initialize, #keys, #last_statement, #result, #stream_arrays, #stream_hashes, #to_path, #to_s, #to_stdout

Constructor Details

This class inherits a constructor from Mysql2xxxx::Writer

Instance Method Details

#to_file(f) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mysql2xxxx/writer/json.rb', line 9

def to_file(f)
  first = true
  f.write '['
  stream_hashes do |hsh|
    if first
      first = false
    else
      f.write ','
    end
    if RUBY_VERSION >= '1.9'
      # the mysql gem isn't encoding aware, so even if mysql returns proper utf-8, ruby 1.9 treats it as ASCII-8BIT
      # here we force ruby to treat these strings as utf-8 without any further conversion
      hsh.each { |k, v| hsh[k] = v.nil? ? nil : v.force_encoding(properties.encoding) }
    end
    f.write hsh.to_json
  end
  f.write ']'
  nil
end