-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |