1. Go to this page and download the library: Download dschledermann/json-coder 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/ */
dschledermann / json-coder example snippets
class SomeClass
{
public function __construct(
public string $name,
public int $age,
) {}
}
$someObj = new SomeClass(
"John Doe",
45,
);
$encoder = Encoder::create(SomeClass::class);
echo $encoder->encode($someObj);
class SomeClass
{
public function __construct(
public string $name,
public int $age,
) {}
}
$decoder = Decoder::create(SomeClass::class);
$json = '{"name":"John Doe","age":45}';
print_r($decoder->decode($json));
use Dschledermann\JsonCoder\KeyConverter\ToLower;
#[ToLower]
final class SomeObj
{
public function __construct(
public string $myString,
public int $myFancyInt,
) {}
}
$obj = new SomeObj("Walter White", 52);
$encoder = Encoder::create(SomeObj::class, JSON_PRETTY_PRINT);
echo $encoder->encode($obj);
use Dschledermann\JsonCoder\KeyConverter\ToSnakeCase;
#[ToSnakeCase]
class SomeObj
{
public function __construct(
public string $myString,
public int $myFancyInt,
) {}
}
$encoder = Encoder::create(SomeObj::class);
$obj = new SomeObj("Walter White", 52);
echo $encoder->encode($obj);
final class Person
{
public function __construct(
public string $name,
) {}
}
final class Car
{
public function __construct(
public string $brand,
public float $horsePowers,
) {}
}
use Dschledermann\JsonCoder\VariantChoiceTrait;
use Dschledermann\JsonCoder\Filter\Encode\SkipEncodeIfNull;
#[SkipEncodeIfNull]
final class Payload
{
use VariantChoiceTrait;
public ?Person $person = null;
public ?Car $car = null;
}
$person = new Person("Mr. Bean");
$payload = Payload::createFromVariant($person);
print_r($payload);
final class SomeType
{
/** @var int[] */
public array $listOfInts;
}
final class SomeType
{
public string $someField;
/** @var SubType[] */
public array $subTypeList;
}
final class SubType
{
public int $someValue;
}
namespace Path\To\SomeSpace;
final class SubType
{
public string $value;
}
namespace Path\To\AnotherSpace;
use Dschledermann\JsonCoder\ListType;
use Path\To\SomeSpace\SubType;
final class SomeType
{
#[ListType(SubType::class)]
public array $list;
}
namespace Path\To\AnotherSpace;
final class SomeType
{
/** @var \Path\To\SomeSpace\SubType[] */
public array $list;
}
final class Element
{
public function __construct(
public string $str,
) {}
}
$a = ["a" => new Element("a"), "b" => new Element("b")];
$encoder = Encoder::create(Element::class);
$json = $encoder->encodeArray($a);
#[SquashIndexes]
final class Element
{
public function __construct(
public string $str,
) {}
}
final class ElementWithList
{
public function __construct(
public string $str,
/** @var string[] */
public array $list,
) {}
}
$b = new ElementWithList("something", ['a' => 'aa', 'b' => 'bb']);
$encoder = Encoder::create(ElementWithList::class);
$json = $encoder->encode($b);
final class ElementWithList
{
public function __construct(
public string $str,
/** @var string[] */
#[SquashIndexes]
public array $list,
) {}
}
$b = new ElementWithList("something", ['a' => 'aa', 'b' => 'bb']);
$encoder = Encoder::create(ElementWithList::class);
$json = $encoder->encode($b);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.