diff --git a/advagg.module b/advagg.module
index fcd1fc1..0f18733 100644
--- a/advagg.module
+++ b/advagg.module
@@ -3123,14 +3123,26 @@ function advagg_add_css_inline($data = NULL, $media = 'all', $prefix = NULL, $su
  * We use HTML-safe strings, i.e. with <, > and & escaped.
  */
 function advagg_drupal_to_js($var) {
+  // Different versions of PHP handle json_encode() differently.
+  static $php550;
   static $php530;
+  if (!isset($php550)) {
+    $php550 = version_compare(PHP_VERSION, '5.5.0', '>=');
+  }
   if (!isset($php530)) {
     $php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
   }
 
   // json_encode on PHP prior to PHP 5.3.0 doesn't support options.
   if ($php530) {
-    return json_encode($var, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS);
+    // Default json encode options.
+    $options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
+    if ($php550) {
+      // Output partial json if PHP >= 5.5.0.
+      $options |= JSON_PARTIAL_OUTPUT_ON_ERROR;
+    }
+    // Encode to JSON.
+    return @json_encode($var, $options);
   }
 
   // if json_encode exists, use it.
