Module: DbAgile::IO::JSON

Extended by:
TypeSafe
Defined in:
lib/dbagile/io/json.rb

Class Method Summary collapse

Methods included from TypeSafe

from_typesafe_relation, from_typesafe_tuple, from_typesafe_xxx, to_typesafe_relation, to_typesafe_tuple, with_type_safe_relation

Class Method Details

.from_json(input, options = {}, &block) ⇒ Object

Loads some data from a json input. If a block is given, yields it with each tuple in turn and returns nil. Otherwise returns an array of tuples.



33
34
35
36
# File 'lib/dbagile/io/json.rb', line 33

def from_json(input, options = {}, &block)
  require "json"
  from_typesafe_xxx(::JSON::load(input), options, &block)
end

.to_json(data, columns = nil, buffer = "", options = {}) ⇒ ...

Outputs some data as a JSON string

Returns:

  • (...)

    the buffer itself



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dbagile/io/json.rb', line 11

def to_json(data, columns = nil, buffer = "", options = {})
  require "json"
  pretty, first = options[:pretty], true
  buffer << (pretty ? "[\n" : "[")
  with_type_safe_relation(data, options) do |tuple|
    buffer << (pretty ? ",\n" : ",") unless first
    if pretty
      buffer << ::JSON::pretty_generate(tuple)
    else
      buffer << ::JSON::fast_generate(tuple)
    end
    first = false
  end
  buffer << (pretty ? "\n]" : "]")
  buffer
end