NAME

JSON::JSONFold::Writer - streaming formatter for JSON::JSONFold

SYNOPSIS

use JSON::JSONFold;

my $formatter = create_formatter(
    \*STDOUT,
    100,
    'default'
);

$formatter->write("{\n");
$formatter->write(qq(  "name": "Alice"\n));
$formatter->write("}\n");

$formatter->finish;
$formatter->flush;

DESCRIPTION

JSON::JSONFold::Writer incrementally processes pretty-printed JSON text and writes a folded representation to an underlying filehandle.

This allows JSONFold to be used as a streaming post-processor without buffering the entire document in memory.

Normally users should prefer the higher-level interfaces in JSON::JSONFold, such as format_json, write_json, or the object interface.

CONSTRUCTOR

new

my $formatter = JSON::JSONFold::Writer->new(
    $fh,
    $config,
    $close_fp
);

Creates a streaming formatter around an existing filehandle.

Parameters:

  • $fh

    Destination filehandle.

  • $config

    JSONFold configuration object.

  • $close_fp

    If true, closing the formatter also closes the underlying filehandle.

METHODS

write

$formatter->write($text);

Process additional pretty-printed JSON text.

The input may be supplied incrementally. Output is written to the underlying filehandle as complete lines become available.

finish

$formatter->finish;

Complete processing and emit any buffered output.

This method should be called after the last input has been written.

flush

$formatter->flush;

Flush any pending output to the underlying filehandle.

close

$formatter->close;

Close the formatter.

If the formatter was created with $close_fp set to true, the underlying filehandle is also closed.

stats

my $stats = $formatter->stats;

Return a JSON::JSONFold::Stats object containing formatting statistics.

reset

$formatter->reset;

Reset the formatter state and statistics.

EXAMPLE

use JSON::JSONFold;

my $formatter = create_formatter(
    \*STDOUT,
    100,
    'default'
);

my $json = JSON->new->pretty;

$formatter->write(
    $json->encode($data)
);

$formatter->finish;
$formatter->close;

SEE ALSO

JSON::JSONFold, JSON::JSONFold::Config, JSON::JSONFold::Stats

AUTHOR

Yair Lenga

COPYRIGHT AND LICENSE

See the distribution license.