Skip to content

Instantly share code, notes, and snippets.

@connordavison
Created August 30, 2017 15:01
Show Gist options
  • Save connordavison/6e44cd0567818deba2dcb28e13fa34f5 to your computer and use it in GitHub Desktop.
Save connordavison/6e44cd0567818deba2dcb28e13fa34f5 to your computer and use it in GitHub Desktop.
<?php
use JMS\Serializer\Annotation as JMS;
use JMS\Serializer\SerializerBuilder;
class A {
/**
* @JMS\Type("integer")
*/
protected $x;
/**
* @JMS\Type("integer")
*/
protected $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
class B {
/**
* @JMS\Type("integer")
*/
protected $x;
public function __construct($x) {
$this->x = $x;
}
}
$serializer = SerializerBuilder::create()->build();
$a = new A(123, 456);
$a_json = $serializer->serialize($a, 'json');
$b = $serializer->deserialize($a_json, B::class, 'json');
/*
>>> var_dump($a)
object(A)#359 (2) {
["x":protected]=>
int(123)
["y":protected]=>
int(456)
}
=> null
>>> var_dump($a_json)
string(17) "{"x":123,"y":456}"
=> null
>>> var_dump($b)
object(B)#296 (1) {
["x":protected]=>
int(123)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment