what is the difference between Serializing and encoding?

前端 未结 2 2096
南笙
南笙 2021-02-05 02:57

what is the difference between Serializing and encoding? and when should i use each in a web service?

2条回答
  •  深忆病人
    2021-02-05 03:03

    "Serialization" is the process of converting data (which may include arrays, objects and similar structures) into a single string so it can be stored or transmitted easily. For instance, a single database field can't store an array because database engines don't understand that concept. To be stored in a database, an array must either be broken down into individual elements and stored in multiple fields (which only works if none of the elements are themselves arrays or objects) or it must be serialized so it can be stored as a single string in a single field. Later, the string can be retrieved from the database and unserialized, yielding a copy of the original array.

    "Encoding" is a very general term, referring to any format in which data can be represented, or the process of converting data into a particular representation.

    For example, in most programming languages, a sequence of data elements can be encoded as an array. (Usually we call arrays "data structures" rather than "encodings", but technically all data structures are also encodings.) Likewise, the word "encoding" itself can be encoded as a sequence of English letters. Those letters themselves may be encoded as written symbols, or as bit patterns in a text encoding such as ASCII or UTF8.

    The process of encoding converts a given piece of data into another representation (another encoding, if you want to be technical about it.)

    As you may have already realized, serialization is an example of an encoding process; it transforms a complex data structure into a single sequence of text characters.

    That sequence of characters can then be further encoded into some sort of binary representation for more compact storage or quicker transmission. I don't know what form of binary encoding is used in BSON, but presumably it is more efficient than the bit patterns used in text encodings.

提交回复
热议问题