-
-
Notifications
You must be signed in to change notification settings - Fork 590
Serialization Context Naming Strategy #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 10 commits
0714257
cc188fd
784ab9c
e03769f
8850851
0e370a8
766c7a9
f42048f
8c2eb6f
d8a95ad
2763659
6f29e42
380f529
8247a39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,13 +209,20 @@ public function accept($data, ?array $type = null) | |
| continue; | ||
| } | ||
|
|
||
| $this->context->pushPropertyMetadata($propertyMetadata); | ||
| /** Metadata changes based on context, should not be cached */ | ||
| $contextSpecificMetadata = $propertyMetadata; | ||
| if (null !== $this->context->getPropertyNamingStrategy()) { | ||
| $contextSpecificMetadata = clone $propertyMetadata; | ||
| $contextSpecificMetadata->serializedName = $this->context->getPropertyNamingStrategy()->translateName($propertyMetadata); | ||
| } | ||
|
|
||
| $this->context->pushPropertyMetadata($contextSpecificMetadata); | ||
| try { | ||
| $v = $this->visitor->visitProperty($propertyMetadata, $data); | ||
| $this->accessor->setValue($object, $v, $propertyMetadata, $this->context); | ||
| $v = $this->visitor->visitProperty($contextSpecificMetadata, $data); | ||
| $this->accessor->setValue($object, $v, $contextSpecificMetadata, $this->context); | ||
| } catch (NotAcceptableException $e) { | ||
| if (true === $propertyMetadata->hasDefault) { | ||
| $this->accessor->setValue($object, $propertyMetadata->defaultValue, $propertyMetadata, $this->context); | ||
| if (true === $contextSpecificMetadata->hasDefault) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid it can has some side effects if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah true. Should we clone always or leave this part code as it was before?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would left it as it used to be :) |
||
| $this->accessor->setValue($object, $contextSpecificMetadata->defaultValue, $contextSpecificMetadata, $this->context); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,11 @@ class PropertyMetadata extends BasePropertyMetadata | |
| */ | ||
| public $serializedName; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| public $preContextSerializedName; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to be not used anymore. |
||
|
|
||
| /** | ||
| * @var array|null | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JMS\Serializer\Tests\Benchmark; | ||
|
|
||
| use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
| use JMS\Serializer\SerializationContext; | ||
|
|
||
| class JsonContextNamingSerializationBench extends JsonSerializationBench | ||
| { | ||
| protected function createContext(): SerializationContext | ||
| { | ||
| return parent::createContext()->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JMS\Serializer\Tests\Fixtures; | ||
|
|
||
| use JMS\Serializer\Annotation as Serializer; | ||
|
|
||
| class CustomDeserializationObjectWithInnerClass | ||
| { | ||
| /** | ||
| * @Serializer\Type("JMS\Serializer\Tests\Fixtures\CustomDeserializationObject") | ||
| */ | ||
| #[Serializer\Type(name: CustomDeserializationObject::class)] | ||
| private $someProperty; | ||
|
|
||
| public function __construct(CustomDeserializationObject $value) | ||
| { | ||
| $this->someProperty = $value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JMS\Serializer\Tests\Fixtures; | ||
|
|
||
| use JMS\Serializer\Annotation as Serializer; | ||
|
|
||
| class CustomDeserializationObjectWithSerializedName | ||
| { | ||
| /** | ||
| * @Serializer\Type("string") | ||
| * @Serializer\SerializedName("name") | ||
| */ | ||
| #[Serializer\Type(name: 'string')] | ||
| #[Serializer\SerializedName(name: 'name')] | ||
| public $someProperty; | ||
|
|
||
| public function __construct($value) | ||
| { | ||
| $this->someProperty = $value; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.