Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json Encoder #533

Closed
nyamsprod opened this issue Sep 13, 2024 · 0 comments
Closed

Json Encoder #533

nyamsprod opened this issue Sep 13, 2024 · 0 comments

Comments

@nyamsprod
Copy link
Member

nyamsprod commented Sep 13, 2024

Feature Request

Q A
New Feature yes
BC Break yes
Version taget 10.0

Proposal

Adding a new converter to convert a tabular data into a JSON stream.

Proposed API

use League\Csv\JsonConverter;

$converter = JsonConverter::create(); ///returns a new converter with default options that match those of json_encode
$converter->convert(iterable $tabularData): Iterator; //returns an Iterator<string> object 

foreach ($converter->convert($tabularData) as $line) {
    echo $line;
}

//lazyly generates the JSON data


$converter->save(iterable $tabularData, mixed $destination): void;
// This methods uses the `convert` method internally to store the generated data to the filepath located at detination.

$converter->save(Reader::createFromPath('document.csv'), 'document.json');
//will convert the CSV into a JSON string and store the result in document.json

The class can be configured with the following methods:

JsonConverter::addFlags(int ...$flags);
JsonConverter::removeFlags(int ...$flags);
JsonConverter::flags(int $flags);
JsonConverter::depth(int $depth);
JsonConverter::indentSize(int $indentSize);

They all more or less map to json_encode parameters except for the indentSize method that enable creating JSON string in pretty print mode with different indentation size. By default the method uses and indentSize of 4 characters.

``

@nyamsprod nyamsprod self-assigned this Sep 13, 2024
nyamsprod added a commit that referenced this issue Sep 18, 2024
nyamsprod added a commit that referenced this issue Sep 18, 2024
nyamsprod added a commit that referenced this issue Sep 18, 2024
* Implementing JsonConverter #533

* Implementing JsonConverter #533
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant