public function write($path, Iterator $data, $threshold = null, $flags = 0)
    {
        $asObject = $this->isAsObject($data, $flags);
        $pretty = $flags & self::FLAG_PRETTY_PRINT;
        $jsonFlags = $pretty ? JSON_PRETTY_PRINT : 0;

        // When a threshold is given, enumerate files
        $ord = isset($threshold) ? 1 : null;

        $file = $this->open($path, $asObject, $pretty, $ord);

        while($data->valid()) {
            if ($pretty) {
                $file->fwrite("\t");
            }

            if ($asObject) {
                $file->fwrite('"' . $data->key() . '": ');
            }

            $file->fwrite(Json::encode($data->current(), $jsonFlags));

            // Move to next item to see if next exists
            $data->next();
            $hasNext = $data->valid();

            // Check whether a rollover is necessary
            $shouldRollover = $this->shouldRollover($file, $threshold);

            // Write a comma separator if it's not the last item for this file
            if (!$shouldRollover && $hasNext) {
                $file->fwrite(",");
            }

            if ($pretty) {
                $file->fwrite("\n");
            }

            if ($shouldRollover && $hasNext) {
                $ord += 1;
                $file = $this->rollover($file, $asObject, $path, $pretty, $ord);
            }
        }

        $this->close($file, $asObject, $pretty);
    }