diff core/modules/serialization/src/Encoder/JsonEncoder.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
line wrap: on
line diff
--- a/core/modules/serialization/src/Encoder/JsonEncoder.php	Thu Feb 28 13:11:55 2019 +0000
+++ b/core/modules/serialization/src/Encoder/JsonEncoder.php	Thu May 09 15:34:47 2019 +0100
@@ -28,12 +28,40 @@
    * {@inheritdoc}
    */
   public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
+    $this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
+    $this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
+  }
+
+  /**
+   * Instantiates a JsonEncode instance.
+   *
+   * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
+   *   once Drupal requires Symfony 4.2 or higher.
+   */
+  private function getJsonEncode() {
     // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
     // embedded into HTML.
     // @see \Symfony\Component\HttpFoundation\JsonResponse
     $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
-    $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options);
-    $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE);
+    $reflection = new \ReflectionClass(JsonEncode::class);
+    if (array_key_exists('OPTIONS', $reflection->getConstants())) {
+      return new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
+    }
+    return new JsonEncode($json_encoding_options);
+  }
+
+  /**
+   * Instantiates a JsonDecode instance.
+   *
+   * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
+   *   once Drupal requires Symfony 4.2 or higher.
+   */
+  private function getJsonDecode() {
+    $reflection = new \ReflectionClass(JsonDecode::class);
+    if (array_key_exists('ASSOCIATIVE', $reflection->getConstants())) {
+      return new JsonDecode([JsonDecode::ASSOCIATIVE => TRUE]);
+    }
+    return new JsonDecode(TRUE);
   }
 
   /**