Class: Synthra::Output::NdjsonFormatter
- Inherits:
-
Object
- Object
- Synthra::Output::NdjsonFormatter
- Defined in:
- lib/synthra/output/ndjson_formatter.rb
Overview
NDJSON (newline-delimited JSON) formatter
Instance Method Summary collapse
-
#format_many(records) ⇒ String
Format records to NDJSON string.
-
#format_stream(stream, io: $stdout) ⇒ void
Format a stream to NDJSON.
-
#write_to_file(stream, path) ⇒ void
Write to file.
Instance Method Details
#format_many(records) ⇒ String
Format records to NDJSON string
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/synthra/output/ndjson_formatter.rb', line 28 def format_many(records) # Append into one buffer instead of allocating an N-element intermediate array of strings. # Identical output to map.join("\n") (no trailing newline). For true O(1) memory, stream # to an IO with #format_stream instead of materialising the whole string. buffer = +"" records.each_with_index do |record, i| buffer << "\n" if i.positive? buffer << JSON.generate(record) end buffer end |
#format_stream(stream, io: $stdout) ⇒ void
This method returns an undefined value.
Format a stream to NDJSON
15 16 17 18 19 20 |
# File 'lib/synthra/output/ndjson_formatter.rb', line 15 def format_stream(stream, io: $stdout) stream.each do |record| io.puts JSON.generate(record) io.flush if io.respond_to?(:flush) end end |
#write_to_file(stream, path) ⇒ void
This method returns an undefined value.
Write to file
47 48 49 50 51 |
# File 'lib/synthra/output/ndjson_formatter.rb', line 47 def write_to_file(stream, path) File.open(path, "w") do |file| format_stream(stream, io: file) end end |