How do I use this to insert each object directly to a database?
Closed this issue · 3 comments
I was looking at your example which basically puts everything into an array, which defeats the purpose of a streamer, and uses just as much memory.
Could you perhaps make an example where you simply take each object and insert it into a database? I was playing around with it a little and couldn't seem to figure it out.
Thanks!
@Brobin You're right in that the example is contrived, but what you basically would want to do is to take the code in end_object()
and do whatever you want with it (such as sticking it in a database) instead of storing it in the existing array. I would suggest starting by changing $this->value($obj);
to something like var_export($obj, true);
so you can see the print out of each object as it comes through. This will give you an idea of what $obj
means in the documents and then you can decide what to do next.
Where it gets complicated is when a JSON document is heavily nested, which they often are. In those situations you have to keep track of how far down into the document you are to see whether the object you're ending is what you want to shove into a DB or simply a child of the object you want to shove into a DB.
Make sense?
Yeah, sounds good. I'll do some more messing around with it. The JSON I'm looking at isn''t too complicated, so I shouldn't have too much trouble with nested objects.
I've set a counter on start_array
and end_array
to detect when a new root array is detected. It allows you to test the value on end_object == 0
where you're sure that you have the full array :).
It's only a small proof of concept, here's a code extract (first try based on the example, not definitive ofc):
public function end_object() {
$obj = array_pop($this->_stack);
//object is full
if($this->array_level == 0) {
var_dump($obj); //do your stuff with the full array :)
$this->_stack = array(); //clear
sleep(1);
} else if (empty($this->_stack)) {
// doc is DONE!
$this->_json = $obj;
} else {
$this->value($obj);
}
}
public function start_array() {
$this->array_level++;
echo 'start array' . PHP_EOL;
$this->start_object();
}
public function end_array() {
echo 'end array' . PHP_EOL;
$this->array_level--;
$this->end_object();
}