 .../EventSubscriber/ResourceResponseSubscriberTest.php     |  2 +-
 core/modules/serialization/src/Encoder/JsonEncoder.php     | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php b/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php
index 87c9059..89770fb 100644
--- a/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php
+++ b/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php
@@ -48,7 +48,7 @@ public function testSerialization($data, $expected_response = FALSE) {
     $resource_response_subscriber->onResponse($event);
 
     // Content is a serialized version of the data we provided.
-    $this->assertEquals($expected_response !== FALSE ? $expected_response : json_encode($data), $event->getResponse()->getContent());
+    $this->assertEquals($expected_response !== FALSE ? $expected_response : json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), $event->getResponse()->getContent());
   }
 
   public function providerTestSerialization() {
diff --git a/core/modules/serialization/src/Encoder/JsonEncoder.php b/core/modules/serialization/src/Encoder/JsonEncoder.php
index 63e1ddb..18c6066 100644
--- a/core/modules/serialization/src/Encoder/JsonEncoder.php
+++ b/core/modules/serialization/src/Encoder/JsonEncoder.php
@@ -4,6 +4,8 @@
 
 use Symfony\Component\Serializer\Encoder\DecoderInterface;
 use Symfony\Component\Serializer\Encoder\EncoderInterface;
+use Symfony\Component\Serializer\Encoder\JsonDecode;
+use Symfony\Component\Serializer\Encoder\JsonEncode;
 use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
 
 /**
@@ -21,6 +23,18 @@ class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderIn
   /**
    * {@inheritdoc}
    */
+  public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null) {
+    // 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);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function supportsEncoding($format) {
     return in_array($format, static::$format);
   }
