JMS serializer error when property does not exist

I am creating a symfony2 project and using the JMS serializer to serialize my object to store the database (I know this is not the best way).

But now my problem is when I change the property of my object and deserialize the Json string, the JMS serializer ignores non-existent properties, and does not throw an error, which is actually great. But I would like to register such an event.

Below is an example illustrating my question

Json string from my database:

$dataToBeDeserialized = {"title":"testing123","text":"Lorem Ipsum"} 

TestClass:

 /** * @Type("string") */ protected title; /** * @Type("string") */ protected text; 

Deserialization Method:

 $this->serializer = SerializerBuilder::create()->build(); $this->serializer->deserialize($dataToBeDeserialized, 'TestClass', 'json'); 

It leads to:

 TestClass { title: "testing123", text: "Lorem Ipsum"} 

But when I change my Testclass and rename (or delete) the title, say β€œtitle2”, the deserializer ignores the β€œtitle” attribute in the Json line. and this leads to:

  TestClass { title2: "", text: "Lorem Ipsum"} 

Well no problem. The data in the database is incorrect. But I would like to register this problem. How can I do it? I do not want, if possible, to use the JMS serializer code (since I can no longer update it). And finding empty properties in my TestClass is also not the best way, since they can be null.

+5
source share
1 answer

JMS Serializer allows you to configure event handlers and event listeners, in your case, I think that an event listener will be sufficient, since you just wait for a certain situation to appear (an unsurpassed attribute). At this point, you just want to write it to the log, so the course will be as follows:

  • Create a listener class that implements JMS \ Serializer \ EventDispatcher \ EventSubscriberInterface
  • Make the listener write a log line when your condition is met.
  • Subscribe this listener to an event.

I will write a more detailed description when I get home from work this evening, but I thought it might help you already.

Check out the docs for more info: http://jmsyst.com/bundles/JMSSerializerBundle/master/configuration#event-dispatcher And default subscribers can give you the key: https://github.com/schmittjoh/serializer/tree/ master / src / JMS / Serializer / EventDispatcher / Subscriber

0
source

All Articles