Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(date 1471698005000)
+++ src/wp-includes/functions.php	(date 1471698079000)
@@ -2871,9 +2871,6 @@
  * @return string|false The JSON encoded string, or false if it cannot be encoded.
  */
 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
-	// Prepare the data for JSON serialization.
-	$data = _wp_json_prepare_data( $data );
-
 	/*
 	 * json_encode() has had extra params added over the years.
 	 * $options was added in 5.3, and $depth in 5.5.
@@ -2887,6 +2884,9 @@
 		$args = array( $data );
 	}
 
+	// Prepare the data for JSON serialization.
+	$args[0] = _wp_json_prepare_data( $data );
+
 	$json = @call_user_func_array( 'json_encode', $args );
 
 	// If json_encode() was successful, no need to do more sanity checking.
@@ -2897,7 +2897,7 @@
 	}
 
 	try {
-		$args[0] = _wp_json_sanity_check( $args[0], $depth );
+		$args[0] = _wp_json_sanity_check( $data, $depth );
 	} catch ( Exception $e ) {
 		return false;
 	}
