Created
December 22, 2017 01:54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
class MyJsonHierarchyReader | |
# @param [#each] input_stream Probably a file, but any iterator will do | |
# so long as it returns a valid JSON object from #each | |
def initialize(input_stream, settings) | |
# ... whatever you need to do. Let's pretend it's | |
# a newline-delimited JSON file, since you didn't | |
# specify anything | |
@json_lines = input_stream | |
end | |
def each(&blk) | |
@json_lines.each do |jline| | |
h = JSON.parse jline | |
recursive_yield(h, blk) | |
end | |
end | |
def recursive_yield(h, blk) | |
doc = build_a_doc(h) | |
blk.call doc | |
Array(h['children']).each do |child| | |
recursive_yield(child, blk) | |
end | |
end | |
def build_a_doc(h) | |
{"my_id" => h['id'] } | |
end | |
end | |
sample_record1 = { | |
id: 1, | |
children: [ | |
{ id: 2, | |
children: [ | |
{ id: 3 }, | |
{ id: 4 } | |
] | |
}, | |
{id: 5, | |
children: [ | |
{id: 6}, | |
{id: 7} | |
] | |
} | |
] | |
} | |
sample_record2 = { | |
id: 10, | |
children: [ | |
{ id: 20, | |
children: [ | |
{ id: 30 }, | |
{ id: 40 } | |
] | |
}, | |
{id: 50, | |
children: [ | |
{id: 60}, | |
{id: 70} | |
] | |
} | |
] | |
} | |
records = [sample_record1.to_json, sample_record2.to_json] | |
require 'pp' | |
reader = MyJsonHierarchyReader.new(records, {}) | |
reader.each {|doc| pp doc } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output is what you'd expect:
{"my_id"=>1}
{"my_id"=>2}
{"my_id"=>3}
{"my_id"=>4}
{"my_id"=>5}
{"my_id"=>6}
{"my_id"=>7}
{"my_id"=>10}
{"my_id"=>20}
{"my_id"=>30}
{"my_id"=>40}
{"my_id"=>50}
{"my_id"=>60}
{"my_id"=>70}