Closed signmeuptwice closed 5 years ago
I tested:
composer create-project symfony/skeleton blog
cd blog
composer require jms/serializer-bundle
Edit config/packages/jms_serializer.yaml
with :
jms_serializer:
property_naming:
id: jms_serializer.identical_property_naming_strategy
visitors:
xml_serialization:
format_output: '%kernel.debug%'
and the naming strategy was applied as expected.
Please provide more info to test your issue (as example a skeleton project on a github repo)
Hi goetas;
Thanks for the reply this is a big project; I am not sure how I could provide a "skeleton project" other than what you tested already.. I tried again and I still cannot get it to work. I have the exact same settings as you posted above and I flushed my cache multiple times.
a specificity maybe not mentionned is that I am Using AbstractController
since Controller
is deprecated in SF4
here is my non working instantiation:
use JMS\Serializer\SerializerInterface;
/**
* @param SerializerInterface $serializer
*/
public function searchBarLookup (SerializerInterface $serializer) {
....
$data = $serializer->serialize($list, 'json');
return $this->json($data);
}
It works if I instantiate a new serializer only (like the following) but as a global setting I have no luck at all.
$serializer = \JMS\Serializer\SerializerBuilder::create()
->setPropertyNamingStrategy(
new SerializedNameAnnotationStrategy(
new IdenticalPropertyNamingStrategy()
)
)
->build();
If I intentionally put a bad character in jms_serializer.identical_property_naming_strategy
of the yaml
file then it crashes so I guess Symfony is reading the file and "loading" the setting.
Is it possible that the global setting gets overridden somewhere down the line ?
I have the same issue. Symfony 4.3.2.
If you are using the bundle, consider https://github.com/schmittjoh/JMSSerializerBundle/pull/767
I had to manually delete the cache (var/cache/dev|prod
) to get the new naming strategy working. bin/console cache:clear
was not enough.
I think this is recurring jms/serializer-bundle 4.0.1 and Symfony 6.0.7
jms_serializer:
default_context:
serialization:
serialize_null: true
visitors:
json_serialization:
options:
- JSON_UNESCAPED_SLASHES
- JSON_PRESERVE_ZERO_FRACTION
property_naming:
id: jms_serializer.identical_property_naming_strategy
handlers:
datetime:
default_format: "Y-m-d\\TH:i:sP" # ATOM
default_timezone: "UTC"
This works
public function __construct() {
$this->serializer = SerializerBuilder::create()
->setPropertyNamingStrategy(
new SerializedNameAnnotationStrategy(
new IdenticalPropertyNamingStrategy()
)
)
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->build();
}
This works
public function __construct(SerializerInterface $serializer) {
$this->serializer = $serializer;
}
This does not work like settings in yaml are ignored
$this->serializer = SerializerBuilder::create()
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->build();
Could be I am doing it wrong but would one have expressions enabled with respect to jms_serializer.yaml ?
Just a side note: I have the same issue with setSerializeNull not respected from default_context
$context = SerializationContext::create()->setGroups($groups)->setSerializeNull(true);
$this->serializer->serialize($x);
SerializerBuilder
and SerializationContext
build the context and the serializer instances by ignoring any symfony configuration. The should never be used when using the serializer bundle integration.
I was pointed to this by google because I am trying to find out how to set groups when calling serialize() without the need to programmatically set the options I already have configured in the bundle. Is that possible? From what I found, the only option is to create a new SerializationContext to set the groups, which has the effect of losing the default settings from serializer.yaml
If i autowire the SerializerInterface in Symfony 6.4 the settings from the yaml don't seem to be respected. For me only creating this works:
$this->serializer = SerializerBuilder::create()
->setPropertyNamingStrategy(
new SerializedNameAnnotationStrategy(
new IdenticalPropertyNamingStrategy()
)
)
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->build();
}
Would appreciate it if somebody could point me to a solution here. Don't like that constructor-thingy very much. Though I really do like the Serializer otherwise!
symfony 4.2 "jms/serializer-bundle": "^3.1",
using dependency injecton in my controller function with
SerializerInterface $serializer
in my
config > packages > jms_serializer.yaml
I have the following code but it has not effect whatsoever. Is this normal ? How do we override the default_
naming strategy toidentical
?I am NOT using any other settings anywhere. I literally just installed the bundle and try to configure it.