Skip to content

Examples

Paul edited this page Jan 23, 2020 · 5 revisions

Example 1

Parse a JSON string. Look for all numbers and output their value and associated property name.

use pcrov\JsonReader\JsonReader;

$json = <<<'JSON'
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": 5002, "type": "Glazed" },
        { "id": 5006, "type": "Chocolate with Sprinkles" },
        { "id": 5004, "type": "Maple" }
    ]
}
JSON;

$reader = new JsonReader();
$reader->json($json);

while ($reader->read()) {
    if ($reader->type() === JsonReader::NUMBER) {
        printf("%s: %d\n", $reader->name(), $reader->value());
    }
}
$reader->close();

Output:

id: 5002
id: 5006
id: 5004

Example 2

(With the JSON from example 1 in a file named "data.json".)

Parse a file. Read every property named "type" and output its value.

use pcrov\JsonReader\JsonReader;

$reader = new JsonReader();
$reader->open("data.json");

while ($reader->read("type")) {
    echo $reader->value(), "\n";
}
$reader->close();

Output:

donut
Glazed
Chocolate with Sprinkles
Maple

Example 3

Read until the first (and only, in this case) "toppings" property and output its array entirely.

use pcrov\JsonReader\JsonReader;

$reader = new JsonReader();
$reader->open("data.json");

$reader->read("toppings");
print_r($reader->value());

$reader->close();

Output:

Array
(
    [0] => Array
        (
            [id] => 5002
            [type] => Glazed
        )

    [1] => Array
        (
            [id] => 5006
            [type] => Chocolate with Sprinkles
        )

    [2] => Array
        (
            [id] => 5004
            [type] => Maple
        )

)

Example 4

Read until the first "toppings" property and output each element of its array individually.

use pcrov\JsonReader\JsonReader;

$reader = new JsonReader();
$reader->open("data.json");

$reader->read("toppings");
$depth = $reader->depth(); // Check in a moment to break when the array is done.

$reader->read(); // Step to the first element.
do {
    print_r($reader->value());
} while ($reader->next() && $reader->depth() > $depth); // Read each sibling.

$reader->close();

Output:

Array
(
    [id] => 5002
    [type] => Glazed
)
Array
(
    [id] => 5006
    [type] => Chocolate with Sprinkles
)
Array
(
    [id] => 5004
    [type] => Maple
)

Example 5

Handle a common structure - an array of objects which are individually small enough to hold in memory.

use pcrov\JsonReader\JsonReader;

$json = <<<'JSON'
[
    { "id": 5002, "type": "Glazed" },
    { "id": 5006, "type": "Chocolate with Sprinkles" },
    { "id": 5004, "type": "Maple" }
]
JSON;

$reader = new JsonReader();
$reader->json($json);

$reader->read(); // Begin array
$reader->read(); // First element, or end of array
while($reader->type() === JsonReader::OBJECT) {
    $data = $reader->value();
    printf("%d: %s\n", $data["id"], $data["type"]);

    $reader->next();
}

$reader->close();

Output:

5002: Glazed
5006: Chocolate with Sprinkles
5004: Maple