惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - x3d

deepin 23 一个可用 mysql-workbench 版本 8.0.36 基于 Maxwell 实现 MySQL 数据实时迁移到 Mongodb 本地开发环境 通过Podman 手动从头搭建 Ubuntu 容器镜像 intel ax203/ax201 无线网卡驱动 firmware 居然有问题 Direct firmware load for iwlwifi-bz-b0-hr-b0-94.ucode failed with error -2 在安卓平板上搭建 webdav 服务 近期不要选aliyun cloud linux 镜像 一点 开始收割了 PHP Composer 虚拟依赖包 - 实现按需载入钉钉对应功能模块的 php sdk 盘点之原地踏步三 盘点之原地踏步二 盘点之原地踏步一:weui 的一点探索 apijson 初探 Apache Synapse ESB erupt api Think3 ORM 《超文本和超链接》的时间线整理 MEAF框架概念检索工具 现代企业架构框架MEAF初次解读
PHP 中优雅的将JSON/XML/YAML 等数据反序列化成指定的类对象
x3d · 2023-07-26 · via 博客园 - x3d

这个小事情何以需要记上一笔?实在是因为当用了各种编程语言以后,发现系统 I/O处,尤其对外的接口Interface最重要,它或许可以被称为 Specification ,规约。

PHP 是混合型编程风格的语言,不强求完全的OOP。但是代码不OOP化的话,又得不到更多的开发工具的支持。尤其在PHP中如果只是用数组结构处理这种 接口处的强spec场景,在Editor/IDE中的代码就很难达到易读性(不便于它们理解,也就不便于人理解)。

因此以前在很多项目中,会尝试手动做这样的对象化的结构转换,将接收到的请求报文,映射成一个预先定义了清晰结构的Class实例化对象上。这个过程,专业叫法应该叫 “序列化/反序列化(serialize / unserialize)”。多数编程语言都有相应的内置的方法,PHP也有。

这种序列化反序列化,狭义的讲法,是指编程语言级别的算法。不过我们面临的不仅仅是这种情况,甚至更多的不是这种情况,框架会在底层解决掉,应用中往往都很少意识到。更多的情况是跨系统间的数据结构的序列化,比如XML<=> Java/PHP/...、以及当前更主流的 JSON <=> Java/PHP/...

像多数有点年岁的编程语言比如Java一样,PHP 也不太可能原生支持 JSON 类型,最多类型化为对象式访问结构,当然PHP中可以直接decode(某种“反序列化”)为更原生的数组结构或者stdclass(像是某种匿名类对象)。如前所述,这种过程在实际应用开发中,是非常重要的涉及系统稳定性维护的环节。

因此,严肃认真的人会系统性的去研究,并实现解决方案,比如 Symfony/Serializer 组件。

The Serializer component is meant to be used to turn objects into a specific format (XML, JSON, YAML, ...) and the other way around.

原理倒还是以 PHP的 数组为中介,进行两个方向的转换,

它将序列化的过程拆解成了两个子过程,encode/decode;normalize/denoralize 。

示意图如下:

安装:

 composer require symfony/serializer`

用法:

比如预先定义一个对象结构:

class Person
{
    private int $age;
    private string $name;
    private bool $sportsperson;
    private ?\DateTimeInterface $createdAt;

    // Getters
    public function getAge(): int
    {
        return $this->age;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    // Issers
    public function isSportsperson(): bool
    {
        return $this->sportsperson;
    }

    // Setters
    public function setAge(int $age): void
    {
        $this->age = $age;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function setSportsperson(bool $sportsperson): void
    {
        $this->sportsperson = $sportsperson;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt = null): void
    {
        $this->createdAt = $createdAt;
    }
}

然后就可以从其它数据源如xml来反序列化成对象。

use App\Model\Person;

$data = <<<EOF
<person>
    <name>foo</name>
    <age>99</age>
    <sportsperson>false</sportsperson>
</person>
EOF;

$person = $serializer->deserialize($data, Person::class, 'xml');

有更多高级用法,详见:https://symfony.com/doc/current/components/serializer.html