search
HomeBackend DevelopmentPHP TutorialAnalysis of the difference between serialization and deserialization operations of SERIALIZE and JSON in PHP

The example in this article describes the difference between the serialization and deserialization operations of SERIALIZE and JSON in PHP. Share it with everyone for your reference, the details are as follows:

What is the difference between SERIALIZE and JSON serialization and deserialization in PHP? We can take a look at this issue with the editor. The specific operation details are as follows.

In PHP, what is the difference between serialize and json to serialize or deserialize an object or array?

Assume an object and an array:

$web = new stdClass;
$web->site = 'tantengvip';
$web->owner = 'tuntun';
$web->age = 5;
//和
$web = array();
$web['site'] = 'tantengvip';
$web['owner'] = 'tuntun';
$web['age'] = 5;

Use the serialize function and unserialize function to serialize and deserialize them respectively, and see what the printed results are, as follows:

Use the serialize method:

var_dump(serialize($web));
var_dump(unserialize(serialize($web)));
var_dump(json_encode($web));
var_dump(json_decode(json_encode($web)));

The result:

string 'O:8:"stdClass":3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=87)
object(stdClass)[127]
 public 'site' => string 'tantengvip' (length=10)
 public 'owner' => string 'tuntun' (length=6)
 public 'age' => int 5
string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)
object(stdClass)[127]
 public 'site' => string 'tantengvip' (length=10)
 public 'owner' => string 'tuntun' (length=6)
 public 'age' => int 5

Using json method:

var_dump(serialize($web));
var_dump(unserialize(serialize($web)));
var_dump(json_encode($web));
var_dump(json_decode(json_encode($web),true));

Result:

string 'a:3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=74)
array (size=3)
 'site' => string 'tantengvip' (length=10)
 'owner' => string 'tuntun' (length=6)
 'age' => int 5
string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)
array (size=3)
 'site' => string 'tantengvip' (length=10)
 'owner' => string 'tuntun' (length=6)
 'age' => int 5

We found that for such an object or array defined previously, serialize and json are used to serialize, and the deserialized result is the same as the original, and there is no What is the difference, except that the serialization format is different.

So what is the difference between them? The following text summarizes it very well, so I won’t explain it myself. You can write code to verify it.

Use json serialization and deserialization

Advantages:

Variables are still readable after serialization

Can be used by other systems because JSON format is standard

Disadvantages:

Only for UFT-8 data Valid, other encodings may not work well

Only valid for stdClass class examples

Use serialize method to serialize and deserialize

Advantages:

Allows non-UTF-8 variables

Supports other than stdClass examples Other examples

Disadvantages:

The encoded text is unreadable to humans

Cannot be referenced by systems in other languages ​​

Okay, write a code to see:

class Test
{
  private $pri = 'pri';
  public $class = 'Test';
  public function __construct()
  {
    $this->class = 'Test construct';
    $this->pri = 'pri construct';
  }
}
$test = new Test();
var_dump(serialize($test));
var_dump(unserialize(serialize($test)));
var_dump(json_encode($test));
var_dump(json_decode(json_encode($test)));

Result:

string 'O:4:"Test":2:{s:9:"�Test�pri";s:13:"pri construct";s:5:"class";s:14:"Test construct";}' (length=86)
object(Test)[127]
 private 'pri' => string 'pri construct' (length=13)
 public 'class' => string 'Test construct' (length=14)
string '{"class":"Test construct"}' (length=26)
object(stdClass)[127]
 public 'class' => string 'Test construct' (length=14)

We found that, JSON serialization and deserialization lose the private member variables in the class, while serialize serialization and deserialization can be used as long as they are variables of the class, but the member methods of the class cannot be serialized or deserialized.

In general, it is better to use json, because json is a cross-platform universal format. In addition to json, it is also better to use xml. So when should you use serialize?

When deserializing a class, the magic method __wakeUp() will be called by default, which allows the object to re-establish various states that were not retained during serialization. For example: database connection, etc. That's another question, I won't delve into it here

I hope this article will be helpful to everyone in PHP programming.

For more articles on the difference analysis between serialization and deserialization operations of SERIALIZE and JSON in PHP, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在Java中,我们如何使用flexjson序列化对象列表?在Java中,我们如何使用flexjson序列化对象列表?Sep 05, 2023 pm 11:09 PM

Flexjson是一个轻量级库,用于序列化和反序列化Java对象>和来自JSON格式。我们可以使用JSONSerializer类的serialize()方法序列化对象列表。此方法可以对目标实例执行浅层序列化。我们需要将列表类型的对象列表作为参数传递给serialize()方法。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerial

Java序列化如何影响性能?Java序列化如何影响性能?Apr 16, 2024 pm 06:36 PM

序列化对Java性能的影响:序列化过程依赖于反射,会显著影响性能。序列化需要创建字节流存储对象数据,导致内存分配和处理成本。序列化大对象会消耗大量内存和时间。序列化后的对象在网络上传输时会增加负载量。

如何使用Java中的Jackson库对属性的顺序进行序列化?如何使用Java中的Jackson库对属性的顺序进行序列化?Aug 28, 2023 pm 12:45 PM

@JsonPropertyOrder是在类级别使用的注释。它采用字段列表作为属性,该列表定义字段在对象JSON序列化生成的字符串中出现的顺序。可以首先序列化注释声明中包含的属性(按定义的顺序),然​​后序列化定义中未包含的任何属性。语法public@interfaceJsonPropertyOrder示例importcom.fasterxml.jackson.core.*;importcom.fasterxml.jackson.databind.*;importcom.fasterxml.jac

C++ 函数库如何进行序列化和反序列化?C++ 函数库如何进行序列化和反序列化?Apr 18, 2024 am 10:06 AM

C++函数库序列化和反序列化指南序列化:创建输出流并将其转换为存档格式。将对象序列化到存档中。反序列化:创建输入流并将其从存档格式恢复。从存档中反序列化对象。实战示例:序列化:创建输出流。创建存档对象。创建对象并将其序列化到存档中。反序列化:创建输入流。创建存档对象。创建对象并从存档中反序列化。

如何使用Java中的flexjson库序列化一个map?如何使用Java中的flexjson库序列化一个map?Aug 26, 2023 pm 08:13 PM

Flexjson是一个轻量级库,用于将Java对象序列化为JSON格式以及反序列化为JSON格式。我们还可以使用JSONSerializer类的serialize()方法来序列化Map,它对目标实例执行浅层序列化。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerializeMapTest{  publ

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化Jul 29, 2023 am 10:49 AM

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化序列化和反序列化是在计算机科学中常用的数据处理技巧之一。在PHP中,我们可以使用serialize()和unserialize()函数来实现数据的序列化和反序列化操作。本文将为您详细介绍如何使用这两个函数,并提供相关代码示例。一、什么是序列化和反序列化在计算机编

Java 中接口和抽象类的序列化和反序列化Java 中接口和抽象类的序列化和反序列化May 02, 2024 am 08:33 AM

接口无法直接序列化,抽象类可以序列化但前提是不包含非静态、非瞬态字段或覆盖writeObject()和readObject()方法,具体实例可通过实现接口的具体类或覆盖writeObject()和readObject()方法的抽象类实现。

golang函数类型的序列化与反序列化golang函数类型的序列化与反序列化Apr 29, 2024 am 08:15 AM

GoLang函数类型可通过encoding/gob包实现序列化和反序列化。序列化:注册自定义类型并使用gob.NewEncoder将函数类型编码为字节数组。反序列化:使用gob.NewDecoder从字节数组反序列化函数类型。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools