Skip to content

Instantly share code, notes, and snippets.

@wimleers
Created September 27, 2017 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wimleers/fc4098b25086a25ace7f8ef679984166 to your computer and use it in GitHub Desktop.
Save wimleers/fc4098b25086a25ace7f8ef679984166 to your computer and use it in GitHub Desktop.
modules/json82/json82.info.yml | 5 +++++
modules/json82/json82.services.yml | 5 +++++
modules/json82/src/Json82Encoder.php | 14 ++++++++++++++
modules/json82/src/Json82ServiceModifier.php | 28 ++++++++++++++++++++++++++++
4 files changed, 52 insertions(+)
diff --git a/modules/json82/json82.info.yml b/modules/json82/json82.info.yml
new file mode 100644
index 0000000..3eabde8
--- /dev/null
+++ b/modules/json82/json82.info.yml
@@ -0,0 +1,5 @@
+name: JSON 8.2 compat
+type: module
+core: 8.x
+dependencies:
+ - serialization
diff --git a/modules/json82/json82.services.yml b/modules/json82/json82.services.yml
new file mode 100644
index 0000000..8006193
--- /dev/null
+++ b/modules/json82/json82.services.yml
@@ -0,0 +1,5 @@
+services:
+ serializer.encoder.json83:
+ class: Drupal\serialization\Encoder\JsonEncoder
+ tags:
+ - { name: encoder, format: json83 }
diff --git a/modules/json82/src/Json82Encoder.php b/modules/json82/src/Json82Encoder.php
new file mode 100644
index 0000000..60bb400
--- /dev/null
+++ b/modules/json82/src/Json82Encoder.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Drupal\json82;
+
+use Drupal\serialization\Encoder\JsonEncoder as BaseJsonEncoder;
+use Symfony\Component\Serializer\Encoder\JsonDecode;
+use Symfony\Component\Serializer\Encoder\JsonEncode;
+
+// Revert the JSON encoder changes in Drupal 8.3.
+// @see https://www.drupal.org/node/2813755
+class JsonEncoder extends BaseJsonEncoder {
+ public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
+ }
+}
\ No newline at end of file
diff --git a/modules/json82/src/Json82ServiceModifier.php b/modules/json82/src/Json82ServiceModifier.php
new file mode 100644
index 0000000..4b085c1
--- /dev/null
+++ b/modules/json82/src/Json82ServiceModifier.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\json82;
+
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\DependencyInjection\ServiceModifierInterface;
+
+class Json82ServiceProvider implements ServiceModifierInterface {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function alter(ContainerBuilder $container) {
+ // Keep 'format=json' using the 8.2 JSON encoding.
+ $container->getDefinition('serializer.encoder.json')->setClass(JsonEncoder::class);
+
+ // Add a new service with the >= 8.3 JSON encoding.
+ if ($container->has('http_middleware.negotiation') && is_a($container->getDefinition('http_middleware.negotiation')
+ ->getClass(), '\Drupal\Core\StackMiddleware\NegotiationMiddleware', TRUE)
+ ) {
+ $container->getDefinition('http_middleware.negotiation')
+ ->addMethodCall('registerFormat', [
+ 'json83',
+ ['application/json'], // Question: is it okay to have multiple formats with the same MIME type?
+ ]);
+ }
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment