justblackbird/jms-serializer-strict-json

Provide strict JSON deserialization

0.2.0 2017-04-25 15:11 UTC

This package is not auto-updated.

Last update: 2024-12-17 08:06:44 UTC


README

Deserialize JSON using strict types validation

Installation

Run in the command line:

composer require justblackbird/jms-serializer-strict-json

Usage

Use the StrictJsonDeserializationVisitor from the package instead of JMSSerializer built in JsonDeserializationVisitor.

For example, if you use the following code to instantiate JMS serializer:

use JMS\Serializer\SerializerBuilder;

$serializer = SerializerBuilder::create()->build();

You should change it to something like:

use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\SerializerBuilder;
use JustBlackBird\JmsSerializerStrictJson\StrictJsonDeserializationVisitor;

$naming_strategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
$serializer = SerializerBuilder::create()
    ->setPropertyNamingStrategy($naming_strategy)
    ->setDeserializationVisitor('json', new StrictJsonDeserializationVisitor($naming_strategy))
    ->build();

Then use the serializer as you used to.

License

Apache 2.0 (c) Dmitriy Simushev