-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
Add default SerializerContext to serializer, update builder #541
Add default SerializerContext to serializer, update builder #541
Conversation
For now I made it as simple as possible by using php $serializer = \JMS\Serializer\SerializerBuilder::create()
->setDefaultSerializationContextFactory(function () {
return \JMS\Serializer\SerializationContext::create()
->setSerializeNull(true)
;
})
->build()
; |
Please, don't enforce closure for the factory. Eg, in zf2 service manager, it is recommended to use factory classes which is much better. You could maybe enforce callable. This would allow invokables. |
Yep I started to create interfaces |
At first... Don't worry about verbosity. It is much better to rely on interface ;) Thanks for your work anyway. I need this. |
Now we can set a default serializer context factory by doing this: use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
use JMS\Serializer\SerializationContext;
class MySerializationContextFactory implements SerializationContextFactoryInterface
{
public function createSerializationContext()
{
return SerializationContext::create()
->setSerializeNull(true)
;
}
} Then: $serializer = \JMS\Serializer\SerializerBuilder::create()
->setDefaultSerializationContextFactory(new MySerializationContextFactory())
->build()
; |
Last commit allows to use a callable in Serializer builder for a shorter syntax. Example: $serializer = \JMS\Serializer\SerializerBuilder::create()
->setDefaultSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(false)
;
})
->build()
; |
57bb4f4
to
f60cbac
Compare
What about this PR ? @schmittjoh |
To me looks a bit too much 500 lines of code, just to have a default implementation of the context... that anyway changes for each usecase |
Mmh in theses 500 lines, its about 200 lines for testing, 120 for license, a bunch of one-method interfaces... And I just needed it this morning because I don't want to create a wrapper around serializer service to only create a new context with Maybe another solution is to set So that's why I think a default context factory in builder is not déconnant :D |
is not irrevelant* :P |
Personally I can agree with having a context factories, but what is the use case for the callable context factories? They are already part of a specific implementation, so probably they should stay in your project, while the default factories should stay here... is it? |
the callable context factory is to avoid to create a class which just call |
What do you think about renaming some variables as in #645 ? (i have just removed the "default" prefix that was looking redundant to me) |
Okey, its shorter |
I close it in favor to #645 |
This PR adds a default
SerializerContext
factory andSerializerContext
factory inSerializer
class to avoid to pass a context every time we callserialize
,deserialize
,toArray
orfromArray
.It also update the serializer builder by adding
setDefaultSerializerContextFactory
andsetDefaultDeserializerContextFactory
methods, allowing to pass a callable which create a context.Now we can set a default serializer context factory by doing this: