-
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.
new Setting: Skip Inherited Properties
- Loading branch information
Showing
7 changed files
with
105 additions
and
18 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
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,9 @@ | ||
<?php | ||
namespace Demo\Models; | ||
|
||
class SpecialOrder extends Order { | ||
|
||
public string $special = "Special"; | ||
|
||
public \DateTime $dateTime; | ||
} |
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,51 @@ | ||
<?php | ||
namespace Demo; | ||
|
||
use Demo\Models\SpecialOrder; | ||
use Pyther\Json\Exceptions\JsonException; | ||
use Pyther\Json\Json; | ||
use Pyther\Json\JsonSettings; | ||
use Pyther\Json\NamingPolicies\CamelToPascalNamingPolicy; | ||
use Pyther\Json\Types\EnumFormat; | ||
|
||
ini_set('display_errors', 1); | ||
ini_set('display_startup_errors', 1); | ||
error_reporting(E_ALL); | ||
header("Content-Type: text/plain"); | ||
|
||
$autoloader = require_once __DIR__."/../vendor/autoload.php"; | ||
$autoloader->addPsr4('Demo\\', __DIR__); | ||
|
||
try { | ||
$order = new SpecialOrder(); | ||
$order->dateTime = new \DateTime(); | ||
|
||
$order = [ $order, $order]; | ||
// serialize with default settings | ||
$json = Json::serialize($order); | ||
echo "\n\nJSON without settings:\n"; | ||
echo $json; | ||
|
||
// serialize with custom settings | ||
$settings = new JsonSettings(); | ||
$settings->setNamingPolicy(new CamelToPascalNamingPolicy()); | ||
$settings->setDateTimeAsString(false); | ||
$settings->setSkipNull(true); | ||
$settings->setSkipEmptyArray(true); | ||
$json = Json::serialize($order, $settings); | ||
|
||
echo "\n\nJSON with settings 1:\n"; | ||
echo $json; | ||
|
||
// serialize with custom settings | ||
$settings->setSkipInheritedProperties(true); | ||
$json = Json::serialize($order, $settings); | ||
|
||
echo "\n\nJSON with settings 2:\n"; | ||
echo $json; | ||
|
||
} catch (JsonException $ex) { | ||
echo $ex->getMessage(); | ||
} catch (\Exception $ex) { | ||
echo "Internal Error: ".$ex->getMessage(); | ||
} |
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
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
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
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