class JsonSerializableNormalizer extends AbstractNormalizer

A normalizer that uses an objects own JsonSerializable implementation.

Traits

Constants

CIRCULAR_REFERENCE_LIMIT

OBJECT_TO_POPULATE

GROUPS

ATTRIBUTES

ALLOW_EXTRA_ATTRIBUTES

DEFAULT_CONSTRUCTOR_ARGUMENTS

CALLBACKS

CIRCULAR_REFERENCE_HANDLER

IGNORED_ATTRIBUTES

CIRCULAR_REFERENCE_LIMIT_COUNTERS

Properties

protected SerializerInterface $serializer from SerializerAwareTrait
protected $defaultContext from AbstractNormalizer
protected $circularReferenceLimit from AbstractNormalizer
protected callable|null $circularReferenceHandler from AbstractNormalizer
protected ClassMetadataFactoryInterface|null $classMetadataFactory from AbstractNormalizer
protected NameConverterInterface|null $nameConverter from AbstractNormalizer
protected $callbacks from AbstractNormalizer
protected $ignoredAttributes from AbstractNormalizer
protected $camelizedAttributes from AbstractNormalizer

Methods

object|null
extractObjectToPopulate(string $class, array $context, string $key = null)

Extract the object_to_populate field from the context if it exists and is an instance of the provided $class.

setSerializer(SerializerInterface $serializer)

Sets the serializer.

__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, array $defaultContext = array())

Sets the {@link ClassMetadataFactoryInterface} to use.

setCircularReferenceLimit(int $circularReferenceLimit) deprecated

Sets circular reference limit.

setCircularReferenceHandler(callable $circularReferenceHandler) deprecated

Sets circular reference handler.

setCallbacks(array $callbacks) deprecated

Sets normalization callbacks.

setIgnoredAttributes(array $ignoredAttributes) deprecated

Sets ignored attributes for normalization and denormalization.

bool
hasCacheableSupportsMethod()

No description

bool
isCircularReference(object $object, array $context)

Detects if the configured circular reference limit is reached.

mixed
handleCircularReference($object)

Handles a circular reference.

string[]|AttributeMetadataInterface[]|bool
getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false)

Gets attributes to normalize using groups.

bool
isAllowedAttribute(object|string $classOrObject, string $attribute, string|null $format = null, array $context = array())

Is this attribute allowed?

array
prepareForDenormalization(object|array $data)

Normalizes the given data to an array. It's particularly useful during the denormalization process.

getConstructor(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes)

Returns the method to use to construct an object. This method must be either the object constructor or static.

object
instantiateObject(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)

Instantiates an object using constructor parameters when needed.

denormalizeParameter(ReflectionClass $class, ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)

No description

array
createChildContext(array $parentContext, string $attribute)

No description

array|string|int|float|bool
normalize(mixed $object, string $format = null, array $context = array())

Normalizes an object into a set of arrays/scalars.

bool
supportsNormalization(mixed $data, string $format = null)

Checks whether the given class is supported for normalization by this normalizer.

bool
supportsDenormalization(mixed $data, string $type, string $format = null)

Checks whether the given class is supported for denormalization by this normalizer.

object
denormalize(mixed $data, string $class, string $format = null, array $context = array())

Denormalizes data back into an object of the given class.

Details

protected object|null extractObjectToPopulate(string $class, array $context, string $key = null)

Extract the object_to_populate field from the context if it exists and is an instance of the provided $class.

Parameters

string $class The class the object should be
array $context The denormalization context
string $key They in which to look for the object to populate. Keeps backwards compatibility with AbstractNormalizer.

Return Value

object|null an object if things check out, null otherwise

setSerializer(SerializerInterface $serializer)

Sets the serializer.

Parameters

SerializerInterface $serializer A SerializerInterface instance

__construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, array $defaultContext = array())

Sets the {@link ClassMetadataFactoryInterface} to use.

Parameters

ClassMetadataFactoryInterface $classMetadataFactory
NameConverterInterface $nameConverter
array $defaultContext

AbstractNormalizer setCircularReferenceLimit(int $circularReferenceLimit) deprecated

deprecated since Symfony 4.2

Sets circular reference limit.

Parameters

int $circularReferenceLimit Limit of iterations for the same object

Return Value

AbstractNormalizer

AbstractNormalizer setCircularReferenceHandler(callable $circularReferenceHandler) deprecated

deprecated since Symfony 4.2

Sets circular reference handler.

Parameters

callable $circularReferenceHandler

Return Value

AbstractNormalizer

AbstractNormalizer setCallbacks(array $callbacks) deprecated

deprecated since Symfony 4.2

Sets normalization callbacks.

Parameters

array $callbacks Help normalize the result

Return Value

AbstractNormalizer

Exceptions

InvalidArgumentException if a non-callable callback is set

AbstractNormalizer setIgnoredAttributes(array $ignoredAttributes) deprecated

deprecated since Symfony 4.2

Sets ignored attributes for normalization and denormalization.

Parameters

array $ignoredAttributes

Return Value

AbstractNormalizer

bool hasCacheableSupportsMethod()

Return Value

bool

protected bool isCircularReference(object $object, array $context)

Detects if the configured circular reference limit is reached.

Parameters

object $object
array $context

Return Value

bool

Exceptions

CircularReferenceException

protected mixed handleCircularReference($object)

Handles a circular reference.

If a circular reference handler is set, it will be called. Otherwise, a {@class CircularReferenceException} will be thrown.

Parameters

$object

Return Value

mixed

Exceptions

CircularReferenceException

protected string[]|AttributeMetadataInterface[]|bool getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false)

Gets attributes to normalize using groups.

Parameters

string|object $classOrObject
array $context
bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}

Return Value

string[]|AttributeMetadataInterface[]|bool

Exceptions

LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided

protected bool isAllowedAttribute(object|string $classOrObject, string $attribute, string|null $format = null, array $context = array())

Is this attribute allowed?

Parameters

object|string $classOrObject
string $attribute
string|null $format
array $context

Return Value

bool

protected array prepareForDenormalization(object|array $data)

Normalizes the given data to an array. It's particularly useful during the denormalization process.

Parameters

object|array $data

Return Value

array

protected ReflectionMethod|null getConstructor(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes)

Returns the method to use to construct an object. This method must be either the object constructor or static.

Parameters

array $data
string $class
array $context
ReflectionClass $reflectionClass
array|bool $allowedAttributes

Return Value

ReflectionMethod|null

protected object instantiateObject(array $data, string $class, array $context, ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)

Instantiates an object using constructor parameters when needed.

This method also allows to denormalize data into an existing object if it is present in the context with the object_to_populate. This object is removed from the context before being returned to avoid side effects when recursively normalizing an object graph.

Parameters

array $data
string $class
array $context
ReflectionClass $reflectionClass
array|bool $allowedAttributes
string $format

Return Value

object

Exceptions

RuntimeException
MissingConstructorArgumentsException

protected denormalizeParameter(ReflectionClass $class, ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)

Parameters

ReflectionClass $class
ReflectionParameter $parameter
$parameterName
$parameterData
array $context
$format

protected array createChildContext(array $parentContext, string $attribute)

Parameters

array $parentContext
string $attribute

Return Value

array

array|string|int|float|bool normalize(mixed $object, string $format = null, array $context = array())

Normalizes an object into a set of arrays/scalars.

Parameters

mixed $object Object to normalize
string $format Format the normalization result will be encoded as
array $context Context options for the normalizer

Return Value

array|string|int|float|bool

Exceptions

InvalidArgumentException Occurs when the object given is not an attempted type for the normalizer
CircularReferenceException Occurs when the normalizer detects a circular reference when no circular reference handler can fix it
LogicException Occurs when the normalizer is not called in an expected context

bool supportsNormalization(mixed $data, string $format = null)

Checks whether the given class is supported for normalization by this normalizer.

Parameters

mixed $data Data to normalize
string $format The format being (de-)serialized from or into

Return Value

bool

bool supportsDenormalization(mixed $data, string $type, string $format = null)

Checks whether the given class is supported for denormalization by this normalizer.

Parameters

mixed $data Data to denormalize from
string $type The class to which the data should be denormalized
string $format The format being deserialized from

Return Value

bool

object denormalize(mixed $data, string $class, string $format = null, array $context = array())

Denormalizes data back into an object of the given class.

Parameters

mixed $data Data to restore
string $class The expected class to instantiate
string $format Format the given data was extracted from
array $context Options available to the denormalizer

Return Value

object

Exceptions

BadMethodCallException Occurs when the normalizer is not called in an expected context
InvalidArgumentException Occurs when the arguments are not coherent or not supported
UnexpectedValueException Occurs when the item cannot be hydrated with the given data
ExtraAttributesException Occurs when the item doesn't have attribute to receive given data
LogicException Occurs when the normalizer is not supposed to denormalize
RuntimeException Occurs if the class cannot be instantiated