Class: Daru::IO::Exporters::JSON
- Defined in:
- lib/daru/io/exporters/json.rb
Overview
JSON Exporter Class, that extends to_json
, to_json_string
and write_json
methods
to Daru::DataFrame
instance variables
Constant Summary collapse
- ORIENT_TYPES =
%i[index records split values].freeze
Instance Method Summary collapse
-
#initialize(dataframe, orient: :records, pretty: false, **jsonpaths, &block) ⇒ JSON
constructor
Initializes a JSON Exporter instance.
-
#to ⇒ Array or Hash
Exports a JSON Exporter instance to a Ruby structure comprising of Arrays & Hashes.
-
#to_s ⇒ String
Exports a JSON Exporter instance to a file-writable String.
-
#write(path) ⇒ Object
Exports a JSON Exporter instance to a json file.
Methods inherited from Base
Constructor Details
#initialize(dataframe, orient: :records, pretty: false, **jsonpaths, &block) ⇒ JSON
Initializes a JSON Exporter instance.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/daru/io/exporters/json.rb', line 67 def initialize(dataframe, orient: :records, pretty: false, **jsonpaths, &block) require 'json' optional_gem 'jsonpath' super(dataframe) @block = block @orient = orient @pretty = pretty @jsonpath_hash = jsonpaths.empty? ? nil : jsonpaths validate_params end |
Instance Method Details
#to ⇒ Array or Hash
Exports a JSON Exporter instance to a Ruby structure comprising of Arrays & Hashes.
464 465 466 467 468 469 470 471 472 |
# File 'lib/daru/io/exporters/json.rb', line 464 def to @jsonpath_hash ||= @dataframe.vectors.to_a.map { |v| {v => "$.#{v}"} }.reduce(:merge) @vectors = @jsonpath_hash.keys @jsonpaths = process_jsonpath @json_content = process_json_content @json_content = @block.call(@json_content) if @block @json_content end |
#to_s ⇒ String
Exports a JSON Exporter instance to a file-writable String.
270 271 272 |
# File 'lib/daru/io/exporters/json.rb', line 270 def to_s super end |
#write(path) ⇒ Object
Exports a JSON Exporter instance to a json file.
486 487 488 489 490 |
# File 'lib/daru/io/exporters/json.rb', line 486 def write(path) File.open(path, 'w') do |file| file.write(::JSON.send(@pretty ? :pretty_generate : :generate, to)) end end |