1. Go to this page and download the library: Download xp-forge/json library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xp-forge / json example snippets
use text\json\Json;
use io\File;
use peer\SocketInputStream;
// Strings
$value= Json::read('"Test"');
// Input
$in= '{"Hello": "World"}');
$in= new File('input.json');
$in= new SocketInputStream(/* ... */);
$value= Json::read($in);
use text\json\Json;
use io\File;
use peer\SocketOutputStream;
// Strings
$json= Json::of('Test');
// Output
$out= new File('output.json');
$out= new SocketOuputStream(/* ... */);
Json::write($value, $out);
use text\json\{FileOutput, Format};
$out= new FileOutput('glue.json', Format::wrapped());
$out->write([
'name' => 'example/package',
'version' => '1.0.0',
'
use peer\http\HttpConnection;
use text\json\StreamInput;
$conn= new HttpConnection(...);
$in= new StreamInput($conn->get('/search?q=example&limit=1000')->in());
foreach ($in->elements() as $element) {
// Process
}
use peer\http\HttpConnection;
use text\json\StreamInput;
$conn= new HttpConnection(...);
$in= new StreamInput($conn->get('/resources/4711?expand=*')->in());
foreach ($in->pairs() as $key => $value) {
// Process
}
use peer\http\HttpConnection;
use text\json\StreamInput;
$conn= new HttpConnection(...);
$in= new StreamInput($conn->get($resource)->in());
$type= $in->type();
if ($type->isArray()) {
// Handle arrays
} else if ($type->isObject()) {
// Handle objects
} else {
// Handle primitives
}
use text\json\{StreamOutput, Types};
$query= $conn->query('select * from person');
$stream= (new StreamOutput(...))->begin(Types::$ARRAY);
while ($record= $query->next()) {
$stream->element($record);
}
$stream->close();
use text\json\{StreamOutput, Types};
$query= $conn->query('select * from person');
with ((new StreamOutput(...))->begin(Types::$ARRAY), function($stream) use($query) {
while ($record= $query->next()) {
$stream->element($record);
}
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.