diff --git a/src/bundle/Resources/config/serializer.yaml b/src/bundle/Resources/config/serializer.yaml index 2d457670..0bbf6085 100644 --- a/src/bundle/Resources/config/serializer.yaml +++ b/src/bundle/Resources/config/serializer.yaml @@ -29,6 +29,14 @@ services: tags: - { name: ibexa.rest.serializer.normalizer, priority: -400 } + Ibexa\Rest\Output\Normalizer\NativeArrayObjectNormalizer: + tags: + - { name: ibexa.rest.serializer.normalizer, priority: -500 } + + Ibexa\Rest\Output\Normalizer\JsonSerializableNormalizer: + tags: + - { name: ibexa.rest.serializer.normalizer, priority: -500 } + ibexa.rest.serializer.object_normalizer: class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer tags: diff --git a/src/lib/Output/Generator/Json.php b/src/lib/Output/Generator/Json.php index 3fdb2bed..7383b0bc 100644 --- a/src/lib/Output/Generator/Json.php +++ b/src/lib/Output/Generator/Json.php @@ -99,7 +99,9 @@ public function getData(): DataObjectInterface #[\Override] public function getEncoderContext(array $data): array { - return []; + return [ + 'json_encode_options' => JSON_PRETTY_PRINT, + ]; } /** diff --git a/src/lib/Output/Normalizer/JsonSerializableNormalizer.php b/src/lib/Output/Normalizer/JsonSerializableNormalizer.php new file mode 100644 index 00000000..989cc385 --- /dev/null +++ b/src/lib/Output/Normalizer/JsonSerializableNormalizer.php @@ -0,0 +1,36 @@ + $context + * + * {@inheritDoc} + */ + public function normalize($object, ?string $format = null, array $context = []): string + { + assert($object instanceof JsonSerializable); + + return $object->jsonSerialize(); + } + + public function supportsNormalization($data, ?string $format = null): bool + { + return $data instanceof JsonSerializable; + } +} diff --git a/src/lib/Output/Normalizer/NativeArrayObjectNormalizer.php b/src/lib/Output/Normalizer/NativeArrayObjectNormalizer.php new file mode 100644 index 00000000..c48a0365 --- /dev/null +++ b/src/lib/Output/Normalizer/NativeArrayObjectNormalizer.php @@ -0,0 +1,36 @@ + $context + * + * {@inheritDoc} + */ + public function normalize($object, ?string $format = null, array $context = []): mixed + { + assert($object instanceof NativeArrayObject); + + return $object->count() === 0 ? $object : $this->normalizer->normalize($object, $format, $context); + } + + public function supportsNormalization($data, ?string $format = null): bool + { + return $data instanceof NativeArrayObject; + } +}