Skip to content

Commit bc4d7b2

Browse files
committed
docs: Updated Documentation of Library
1 parent 8798259 commit bc4d7b2

14 files changed

Lines changed: 302 additions & 515 deletions

README.md

Lines changed: 156 additions & 349 deletions
Large diffs are not rendered by default.

examples/01-basic-usage.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
6+
// Quick init via constructor
7+
$json = new Json([
8+
'name' => 'Ibrahim',
9+
'age' => 30,
10+
'married' => false,
11+
'score' => 9.5,
12+
'notes' => null,
13+
]);
14+
15+
echo $json . "\n";
16+
// {"name":"Ibrahim","age":30,"married":false,"score":9.5,"notes":null}

examples/02-arrays.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
6+
$json = new Json();
7+
$json->addArray('tags', ['php', 'json', 'api']);
8+
$json->addArray('address', ['city' => 'Riyadh', 'country' => 'SA'], true); // true = as object
9+
10+
echo $json . "\n";
11+
// {"tags":["php","json","api"],"address":{"city":"Riyadh","country":"SA"}}

examples/03-object-with-jsoni.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
use WebFiori\Json\JsonI;
6+
7+
class User implements JsonI {
8+
public function __construct(
9+
private string $username,
10+
private string $email
11+
) {}
12+
13+
public function toJSON(): Json {
14+
return new Json(['username' => $this->username, 'email' => $this->email]);
15+
}
16+
}
17+
18+
$json = new Json();
19+
$user = new User('ibrahim', 'ibrahim@example.com');
20+
$json->addObject('user', $user);
21+
22+
echo $json . "\n";
23+
// {"user":{"username":"ibrahim","email":"ibrahim@example.com"}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
6+
class Product {
7+
public string $sku = 'ABC-001';
8+
private string $name;
9+
private float $price;
10+
11+
public function __construct(string $name, float $price) {
12+
$this->name = $name;
13+
$this->price = $price;
14+
}
15+
16+
public function getName(): string { return $this->name; }
17+
public function getPrice(): float { return $this->price; }
18+
}
19+
20+
$json = new Json();
21+
$product = new Product('Keyboard', 49.99);
22+
$json->addObject('product', $product);
23+
24+
echo $json . "\n";
25+
// {"product":{"Name":"Keyboard","Price":49.99,"sku":"ABC-001"}}
26+
// ^ getters produce "Name"/"Price", public property adds "sku"

examples/05-naming-styles.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
6+
$data = ['first-name' => 'Ibrahim', 'last-name' => 'Al-Shikh'];
7+
8+
echo new Json($data, 'none') . "\n"; // {"first-name":"Ibrahim","last-name":"Al-Shikh"}
9+
echo new Json($data, 'camel') . "\n"; // {"firstName":"Ibrahim","lastName":"Al-Shikh"}
10+
echo new Json($data, 'snake') . "\n"; // {"first_name":"Ibrahim","last_name":"Al-Shikh"}
11+
echo new Json($data, 'kebab') . "\n"; // {"first-name":"Ibrahim","last-name":"Al-Shikh"}
12+
13+
// Change style after construction + apply upper case
14+
$json = new Json($data);
15+
$json->setPropsStyle('snake', 'upper');
16+
echo $json . "\n"; // {"FIRST_NAME":"Ibrahim","LAST_NAME":"Al-Shikh"}

examples/06-decode-and-read.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
use WebFiori\Json\JsonException;
6+
7+
// Decode a JSON string
8+
$json = Json::decode('{"name":"Ibrahim","age":30}');
9+
echo $json->get('name') . "\n"; // Ibrahim
10+
echo $json->get('age') . "\n"; // 30
11+
12+
// Read from file
13+
try {
14+
$json = Json::fromJsonFile(__DIR__ . '/sample.json');
15+
echo $json->get('product') . "\n"; // Keyboard
16+
echo $json->get('price') . "\n"; // 49.99
17+
echo ($json->get('inStock') ? 'true' : 'false') . "\n"; // true
18+
} catch (JsonException $e) {
19+
echo 'Error: ' . $e->getMessage() . "\n";
20+
}

examples/07-jsonx.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use WebFiori\Json\Json;
5+
6+
$json = new Json([
7+
'name' => 'Ibrahim',
8+
'age' => 30,
9+
'isEmployed' => true,
10+
]);
11+
12+
echo $json->toJSONxString() . "\n";

examples/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Examples
2+
3+
| File | What it shows |
4+
|------|---------------|
5+
| [01-basic-usage.php](01-basic-usage.php) | Constructor init, scalar types, `echo` output |
6+
| [02-arrays.php](02-arrays.php) | Indexed arrays, associative arrays as objects |
7+
| [03-object-with-jsoni.php](03-object-with-jsoni.php) | Custom serialization via the `JsonI` interface |
8+
| [04-object-auto-mapping.php](04-object-auto-mapping.php) | Auto-mapping via public getters and public properties |
9+
| [05-naming-styles.php](05-naming-styles.php) | `camel` / `snake` / `kebab` / `none` styles + letter case |
10+
| [06-decode-and-read.php](06-decode-and-read.php) | Decoding JSON strings and reading from files |
11+
| [07-jsonx.php](07-jsonx.php) | Converting JSON to JSONx (XML) format |
12+
13+
Run any example from the project root:
14+
15+
```bash
16+
php examples/01-basic-usage.php
17+
```

examples/basic-usage.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)