{"id":12940,"date":"2021-10-21T17:44:33","date_gmt":"2021-10-21T12:14:33","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=12940"},"modified":"2021-10-21T17:44:33","modified_gmt":"2021-10-21T12:14:33","slug":"unable-to-serialize-value-magento2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/unable-to-serialize-value-magento2\/","title":{"rendered":"Magento 2 &#8211; Issue Fix &#8211; CRITICAL Error: Unable To Serialize Value"},"content":{"rendered":"<p>In this post I am going to explain How to Unable to serialize value.<\/p>\n<p>Error : After Magento 2 version upgrade error on check page is as below.<\/p>\n<p>&#8220;report.CRITICAL: Unable to serialize value. Error: Malformed UTF-8 characters, possible incorrectly encoded.&#8221;<\/p>\n<p>Please follow the steps to resolve this issue.<\/p>\n<h4><strong>1. Create registration.php file at below Path<\/strong><\/h4>\n<p>Path: <strong>app\/code\/&lt;vendor&gt;\/&lt;yourmodule&gt;<\/strong><\/p>\n<pre>&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n'&lt;vendor&gt;_&lt;yourmodule&gt;',\r\n__DIR__\r\n);\r\n<\/pre>\n<h4><strong>2. Create module.xml file at below path<\/strong><\/h4>\n<p>Path: <strong>app\/code\/&lt;vendor&gt;\/&lt;yourmodule&gt;\/etc<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" ?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\r\n&lt;module name=\"&lt;vendor&gt;_&lt;yourmodule&gt;\" setup_version=\"1.0.0\"&gt;\r\n&lt;sequence&gt;\r\n&lt;module name=\"Magento_Framework\"\/&gt;\r\n&lt;\/sequence&gt;\r\n&lt;\/module&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<h4><strong>3. Create di.xml file at below path<\/strong><\/h4>\n<p>Path: <strong>app\/code\/&lt;vendor&gt;\/&lt;yourmodule&gt;\/etc<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" ?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\r\n&lt;preference for=\"Magento\\Framework\\Serialize\\Serializer\\Json\" type=\"&lt;vendor&gt;\\&lt;yourmodule&gt;\\Rewrite\\Magento\\Framework\\Serialize\\Serializer\\Json\"\/&gt;\r\n&lt;preference for=\"Magento\\Framework\\Serialize\\Serializer\\JsonHexTag\" type=\"&lt;vendor&gt;\\&lt;yourmodule&gt;\\Rewrite\\Magento\\Framework\\Serialize\\Serializer\\JsonHexTag\"\/&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<h4><strong>4. Create Json.php file at below path<\/strong><\/h4>\n<p>Path: <strong>app\/code\/&lt;vendor&gt;\/&lt;yourmodule&gt;\/Rewrite\/Magento\/Framework\/Serialize\/Serializer<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace &lt;vendor&gt;\\&lt;yourmodule&gt;\\Rewrite\\Magento\\Framework\\Serialize\\Serializer;\r\n\r\nclass Json extends \\Magento\\Framework\\Serialize\\Serializer\\Json\r\n{\r\npublic function utf8ize($mixed)\r\n{\r\nif (is_array($mixed)) {\r\nforeach ($mixed as $key =&gt; $value) {\r\n$mixed[$key] = $this-&gt;utf8ize($value);\r\n}\r\n} elseif (is_string($mixed)) {\r\nreturn mb_convert_encoding($mixed, \"UTF-8\", \"UTF-8\");\r\n}\r\n\r\nreturn $mixed;\r\n}\r\n\/**\r\n* @inheritDoc\r\n* @since 101.0.0\r\n*\/\r\npublic function serialize($data)\r\n{\r\n$result = json_encode($this-&gt;utf8ize($data));\r\nif (false === $result) {\r\nthrow new \\InvalidArgumentException(\"Unable to serialize value. Error: \" . json_last_error_msg());\r\n}\r\nreturn $result;\r\n}\r\n\r\n\/**\r\n* @inheritDoc\r\n* @since 101.0.0\r\n*\/\r\npublic function unserialize($string)\r\n{\r\n$result = json_decode($string, true);\r\nif (json_last_error() !== JSON_ERROR_NONE) {\r\nthrow new \\InvalidArgumentException(\"Unable to unserialize value. Error: \" . json_last_error_msg());\r\n}\r\nreturn $result;\r\n}\r\n}<\/pre>\n<h4><strong>5. Create JsonHexTag.php file at below path<\/strong><\/h4>\n<p>Path: <strong>app\/code\/&lt;vendor&gt;\/&lt;yourmodule&gt;\/Rewrite\/Magento\/Framework\/Serialize\/Serializer<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace &lt;vendor&gt;\\&lt;yourmodule&gt;\\Rewrite\\Magento\\Framework\\Serialize\\Serializer;\r\n\r\nclass JsonHexTag extends \\Magento\\Framework\\Serialize\\Serializer\\JsonHexTag\r\n{\r\n\/**\r\n* @inheritDoc\r\n* @since 102.0.1\r\n*\/\r\npublic function serialize($data): string\r\n{\r\n$data = $this-&gt;utf8convert($data);\r\n$result = json_encode($data, JSON_HEX_TAG);\r\nif (false === $result) {\r\nthrow new \\InvalidArgumentException('Unable to serialize value.');\r\n}\r\nreturn $result;\r\n}\r\n\r\npublic function utf8convert($mixed, $key = null)\r\n{\r\nif (is_array($mixed)) {\r\nforeach ($mixed as $key =&gt; $value) {\r\n$mixed[$key] = $this-&gt;utf8convert($value, $key);\r\n}\r\n} elseif (is_string($mixed)) {\r\n$fixed = mb_convert_encoding($mixed, \"UTF-8\", \"UTF-8\");\r\nreturn $fixed;\r\n}\r\nreturn $mixed;\r\n}\r\n}\r\n<\/pre>\n<p>That&#8217;s it.<\/p>\n<p>Note: In order to install this extension, after adding files you need to run upgrade and deploy commands.<\/p>\n<p>If you have any query about the above code then you can ask in the comment section below.<\/p>\n<p>Check this <a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-tutorials\/product-stock-information\/\">Best Magento 2 Tutorial<\/a>.<\/p>\n<p>Thank You.<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775803375\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain How to Unable to serialize value. Error : After Magento 2 version upgrade error on check page is as below. &#8220;report.CRITICAL: Unable to serialize value. Error: Malformed UTF-8 characters, possible incorrectly encoded.&#8221; Please follow the steps to resolve this issue. 1. Create registration.php file at below Path&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/unable-to-serialize-value-magento2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":10,"featured_media":13194,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,2138],"tags":[729,2379,9,10,2377],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12940"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=12940"}],"version-history":[{"count":5,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12940\/revisions"}],"predecessor-version":[{"id":13193,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12940\/revisions\/13193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/13194"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=12940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=12940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=12940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}