序列化和编码之间有什么区别?

时间:2018-08-01 00:21:54

标签: symfony serialization

我正在使用Symfony\Component\Serializer\Serializer,但不确定[Dagger/MissingBinding] MyFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided. public abstract void inject(@org.jetbrains.annotations.NotNull() ^ A binding with matching key exists in component: FragmentComponent serialize方法之间的区别是什么。它们具有相同的签名,在实践中它们似乎提供相同的输出。

2 个答案:

答案 0 :(得分:3)

基于官方的 docs ,序列化涉及两个阶段:标准化和编码。规范化将输入数据转换为数组,同时将数组的紧缩编码降低为所需的格式(JSONXML或其他格式)。

答案 1 :(得分:2)

Symfony序列化器的 serialize 方法是其 encode 方法的包装。请注意,您可以分别调用 encode 方法。序列化方法可以在 encode 方法之前调用 normalize 方法,具体取决于所请求的编码器(例如json)是否需要归一化。 如果您打算进行JSON序列化,则序列化程序的 encode 方法最终将调用 json_encode PHP本机方法。实际上,此方法执行序列化...

例如,如果您查看PHP的 jsonSerializable 本机类的jsonSerialize方法,则可以阅读以下说明:

  

将对象序列化为可以通过json_encode()本机序列化的值。

因此,至少在JSON格式的情况下,我们可以说编码实际上是序列化的,但级别较低。

如果直接调用 encode 方法而不使用 serialize 方法,则会将数据序列化为期望的格式,但不会利用 normalize的优势过程。