Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane authored Mar 3, 2018
1 parent 9cc1a5d commit 6591bac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/basic-usage.php
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
<?php
//show any errors
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);


//load JsonX
require_once '../JsonX.php';

//initialize an object of the class JsonX
$j = new JsonX();

//add a number attribute
$j->addNumber('my-number', 34);

//add a boolean with 'false' as its value.
$j->addBoolean('my-boolean', FALSE);

//add a string
$j->addString('a-string', 'Hello, I\'m JsonX! I like "json". ');

header('content-type:application/json');
//display json object in the browser.
echo $j;

52 changes: 52 additions & 0 deletions examples/use-with-classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
//show errors
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

//load required files
require_once '../JsonX.php';
require_once '../JsonI.php';


//defining a class user
//in order for the class to be added in JsonX object,
//it must implement the interface JsonI
class User implements JsonI{
private $username;
private $email;

public function getUserName(){
return $this->username;
}
public function setUsername($username){
$this->username = $username;
}

public function setEmail($email){
$this->email = $email;
}

//this function is from the interface JsonI
// it must return JsonX object
public function toJSON(){
$retVal = new JsonX();
$retVal->addString('username', $this->username);
$retVal->addString('email', $this->email);
return $retVal;
}
}
$user = new User();
$user->setEmail('[email protected]');
$user->setUsername('Warrior Vx');

$json = new JsonX();
$json->addBoolean('my-boolean');
$json->addObject('user', $user);

//adding arrays
$json->addArray('my-arr', array(1,2,"hello",array("nested array")));

header('content-type:application/json');
//display json object in the browser.
echo $json;

0 comments on commit 6591bac

Please sign in to comment.