CodersLab-core / jms-serializer-strict-json

Provide strict JSON deserialization with JMS Serializer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JMS Serializer Strict JSON Build Status

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

About

Provide strict JSON deserialization with JMS Serializer

License:Apache License 2.0


Languages

Language:PHP 100.0%