what is the difference between Serializing and encoding?

前端 未结 2 2095
南笙
南笙 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.

    0 讨论(0)
  • 2021-02-05 03:05

    Serializing is about moving structured data over a storage/transmission medium in a way that the structure can be maintained. Encoding is more broad, like about how said data is converted to different forms, etc. Perhaps you could think about serializing being a subset of encoding in this example.

    With regard to a web service, you will probably be considering serializing/deserializing certain data for making/receiving requests/responses - effectively transporting "messages". Encoding is at a lower level, like how your messaging/web service type/serialization mechanism works under the hood.

    0 讨论(0)
提交回复
热议问题