Serialization and deserialization method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202211308443.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-25
- Publication Date
- 2026-09-29
- Estimated Expiration
- 2042-10-25
AI Technical Summary
[0004]本申请实施例的目的是提供一种序列化和反序列化方法及装置、电子设备和存储介质,能够解决在对结构化数据进行序列化和反序列化时,在数据结构中增加或删除属性、修改属性名称或类型时,会造成反序列化失败的问题
[0010]本申请通过建立第一映射表,并对第一映射表以及属性值进行序列化,在针对序列化数据进行反序列化时,提供校验依据。反序列化时通过预设了第二映射表,将反序列化结果与第二映射表进行比较,增加相关校验,可以避免在反序列时,当数据结构中增加或删除属性、修改属性名称或类型后,导致反序列化失败,确保反序列化的成功。
Smart Images

Figure CN115617875B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of computer technology, specifically relating to a serialization and deserialization method and apparatus, electronic device and storage medium. Background Technology
[0002] In related technologies, serialization and deserialization are used to save and reconstruct data in binary form. Serialization permanently saves the byte sequence of an object to a hard drive or transmits it over a network, while deserialization restores the byte sequence back to the object. This is commonly used in the computer field.
[0003] When structured data is saved and retrieved using serialization and deserialization, it is more convenient, efficient, and reduces storage space. However, when the data structure changes, such as adding or deleting attributes, or modifying attribute names or types, deserialization will fail. Summary of the Invention
[0004] The purpose of this application is to provide a serialization and deserialization method, apparatus, electronic device, and storage medium that can solve the problem that deserialization fails when adding or deleting attributes, or modifying attribute names or types in the data structure during the serialization and deserialization of structured data.
[0005] In a first aspect, embodiments of this application provide a serialization and deserialization method, comprising: constructing a first mapping table based on structured data to be serialized; serializing the first mapping table; obtaining attribute values based on the first mapping table; serializing the attribute values to obtain serialized data; constructing a second mapping table based on the structured data to be serialized; deserializing the serialized data; comparing the deserialized result with the second mapping table; and based on the comparison result, deserializing the attribute values and assigning them to obtain deserialized structured data.
[0006] Secondly, embodiments of this application provide a serialization and deserialization apparatus, including: a first construction module, a first serialization module, a first acquisition module, a second serialization module, a second construction module, a first deserialization module, and a second acquisition module. The first construction module is used to construct a first mapping table based on structured data that needs to be serialized. The first serialization module is used to serialize the first mapping table.
[0007] The first acquisition module retrieves attribute values based on the first mapping table. The second serialization module serializes the attribute values to obtain serialized data. The second construction module constructs a second mapping table based on the structured data to be serialized. The first deserialization module deserializes the serialized data. The second acquisition module compares the deserialization result with the second mapping table, deserializes the attribute values based on the comparison result, and assigns values to obtain the deserialized structured data.
[0008] Thirdly, embodiments of this application provide an electronic device including a processor and a memory, the memory storing a program or instructions that can run on the processor, the program or instructions implementing the steps of the serialization and deserialization methods as described in the first aspect when executed by the processor.
[0009] Fourthly, embodiments of this application provide a readable storage medium on which a program or instructions are stored, and when the program or instructions are executed by a processor, the steps of the serialization and deserialization methods as described in the first aspect are implemented.
[0010] This application establishes a first mapping table and serializes the first mapping table and attribute values, providing a basis for verification during deserialization of serialized data. During deserialization, a second mapping table is preset, and the deserialization result is compared with the second mapping table to add relevant verification. This avoids deserialization failures caused by adding or deleting attributes, or modifying attribute names or types in the data structure, thus ensuring successful deserialization. Attached Figure Description
[0011] Figure 1 One of the flowcharts of the serialization and deserialization methods provided in the embodiments of this application is shown;
[0012] Figure 2 The second schematic diagram of the serialization and deserialization method provided in the embodiments of this application is shown;
[0013] Figure 3 The third schematic diagram of the serialization and deserialization method provided in the embodiments of this application is shown;
[0014] Figure 4 The fourth schematic diagram illustrates the serialization and deserialization method provided in the embodiments of this application;
[0015] Figure 5 The fifth illustration shows a flowchart of the serialization and deserialization method provided in the embodiments of this application;
[0016] Figure 6 This is illustrated as a sixth flowchart of the serialization and deserialization method provided in an embodiment of this application;
[0017] Figure 7 A structural block diagram of the serialization and deserialization apparatus provided in an embodiment of this application is shown;
[0018] Figure 8 A structural block diagram of the electronic device provided in an embodiment of this application is shown;
[0019] Figure 9 This paper illustrates a schematic diagram of the memory structure of serialized data provided in an embodiment of this application.
[0020] Figure 10 The seventh illustration shows a flowchart of the serialization and deserialization method provided in the embodiments of this application.
[0021] in, Figures 7 to 9 The correspondence between the reference numerals and component names in the attached drawings is as follows:
[0022] 100: Serialization and deserialization apparatus; 110: First building module; 120: First serialization module; 130: First acquisition module; 140: Second serialization module; 150: Second building module; 160: First deserialization module; 170: Second acquisition module; 1000: Electronic device; 1002: Processor; 1004: Memory. Detailed Implementation
[0023] The technical solutions of the embodiments of this application will be clearly described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application are within the scope of protection of this application.
[0024] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.
[0025] The following is in conjunction with the appendix Figures 1 to 10 The serialization and deserialization methods, apparatuses, electronic devices, and storage media provided in this application will be described in detail through specific embodiments and application scenarios.
[0026] This application provides a serialization and deserialization method. Figure 1 This illustration shows one of the flowcharts of the serialization and deserialization methods provided in an embodiment of this application, such as... Figure 1 As shown, serialization and deserialization methods include:
[0027] Step S102: Construct the first mapping table based on the structured data that needs to be serialized.
[0028] Step S104: Serialize the first mapping table.
[0029] Step S106: Obtain attribute values based on the first mapping table.
[0030] Step S108: Serialize the attribute values to obtain serialized data.
[0031] Step S110: Construct a second mapping table based on the structured data that needs to be serialized.
[0032] Step S112: Deserialize the serialized data.
[0033] Step S114: Compare the deserialized result with the second mapping table. Based on the comparison result, deserialize the attribute values and assign them to obtain the deserialized structured data.
[0034] As can be understood, serialization refers to converting an object into an ordered stream of bytes for transmission over a network or storage in a local file. Deserialization refers to reconstructing the object from the object's state and descriptive information stored in the byte stream.
[0035] Specifically, in the serialization scheme of this embodiment, a first mapping table is pre-built within the structure, the first mapping table is serialized, the attribute values corresponding to the attribute objects are obtained through the first mapping table, the attribute values are serialized, and finally, serialized data is obtained.
[0036] Specifically, in the deserialization scheme of this embodiment, a second mapping table is pre-built within the structure, the serialized data is then deserialized, the deserialization result is compared with the second mapping table, the attribute values are deserialized and assigned according to the comparison result, and finally, the structured data is obtained.
[0037] In this embodiment, a mapping table is pre-set during both serialization and deserialization. A first mapping table is pre-set during serialization, and a second mapping table is pre-set during deserialization. By comparing the deserialized result with the second mapping table and adding relevant verification, deserialization failure can be avoided when adding or deleting attributes, or modifying attribute names or types in the data structure, thus ensuring the success of deserialization.
[0038] In some embodiments of this application, Figure 2 This is a second schematic flowchart illustrating the serialization and deserialization method provided in an embodiment of this application. Figure 2 As shown, based on the structured data that needs to be serialized, a first mapping table is constructed, specifically including:
[0039] Step S202: Obtain the first attribute name of the structured data that needs to be serialized.
[0040] Step S204: Obtain the first attribute type corresponding to the first attribute name.
[0041] Step S206: Construct a first mapping table based on the first attribute name and the first attribute type.
[0042] In this embodiment, the first attribute name and the first attribute type of the structured data are first obtained, and then the first attribute name and the first attribute type are added to the first mapping table.
[0043] It is understood that the first attribute name and the first attribute type corresponding to the first attribute name form a key-value pair. In this embodiment, the first mapping table is used to store the key-value pairs of the first attribute name and the first attribute type that need to be serialized.
[0044] For example, suppose the first attribute name includes: ID, name, and age. The attribute type corresponding to ID is an integer, the attribute type corresponding to name is a string, and the attribute type corresponding to age is an integer. Then ID and integer form a key-value pair, name and string form a key-value pair, and age and integer form a key-value pair.
[0045] In this embodiment, by constructing a first mapping table of key-value pairs of first attribute names and first attribute types, the correspondence between attribute names and attribute types in the structured data that needs to be serialized can be clearly defined, providing a basis for verification when deserializing serialized data.
[0046] In some embodiments of this application, Figure 3 The third schematic diagram of the serialization and deserialization method provided in the embodiments of this application is shown. Figure 3 As shown, the first mapping table is serialized, specifically including:
[0047] Step S302: Obtain the size of the first mapping table.
[0048] Step S304: Serialize the size of the first mapping table.
[0049] Step S306: Traverse the first mapping table and serialize the first attribute name and the first attribute type.
[0050] In this embodiment, the size of the first mapping table is first calculated. The size of the mapping table is an integer value, which is the number of key-value pairs in the first mapping table. For example, if the first mapping table has three key-value pairs, then the size of the first mapping table is 3. After obtaining the size of the first mapping table, it is serialized. Then, the first mapping table is traversed, and all key-value pairs are serialized.
[0051] In this embodiment, by calculating the size of the first mapping table and then serializing the first mapping table based on its size, the orderly serialization can be guaranteed, thereby improving the accuracy of structured data serialization.
[0052] In some embodiments of this application, Figure 4 The fourth schematic diagram of the serialization and deserialization method provided in the embodiments of this application is shown. Figure 4 As shown, based on the first mapping table, the attribute values are obtained, specifically including:
[0053] Step S402: Based on the first attribute name, find the first attribute object corresponding to the first attribute name.
[0054] Step S404: Obtain the attribute value of the first attribute object.
[0055] Understandably, for each first attribute name, there exists a first attribute object. This first attribute object has attribute values. The attribute value is obtained by looking up the first attribute object using the first attribute name. In the first mapping table, there may be multiple first attribute names. For different first attribute names, the serialization process proceeds sequentially: first, the attribute value corresponding to the first first attribute name is searched and serialized; then, the attribute value corresponding to the second first attribute name is searched and serialized; and so on, until the serialization of attribute values corresponding to all first attribute names is completed.
[0056] In this embodiment, the corresponding attribute value is obtained through the first attribute name. The process is simple and the result is accurate. When there are multiple first attribute names, the attribute value corresponding to each first attribute name can be accurately obtained and serialized, providing a basis for subsequent deserialization of serialized data.
[0057] In some embodiments of this application, Figure 5 The fifth illustration shows a flowchart of the serialization and deserialization method provided in the embodiments of this application. Figure 5 As shown, based on the structured data that needs to be serialized, a second mapping table is constructed, specifically including:
[0058] Step S502: Obtain the name of the second attribute of the structured data to be serialized.
[0059] Step S504: Obtain the second attribute type corresponding to the second attribute name.
[0060] Step S506: Construct a second mapping table based on the second attribute name and the second attribute type.
[0061] In this embodiment, the second attribute name and the second attribute type of the structured data are first obtained, and then the second attribute name and the second attribute type are added to the second mapping table.
[0062] It is understood that the second attribute name and the second attribute type corresponding to the second attribute name form a key-value pair. In this embodiment, the second mapping table is used to store the key-value pairs of the second attribute name and the second attribute type that need to be serialized.
[0063] It is understandable that the first mapping table stores key-value pairs of the first attribute name and the first attribute type that need to be serialized. Therefore, when deserializing the serialized data, the first attribute name and the first attribute type after deserialization can be compared with the key-value pairs of the second attribute name and the second attribute type stored in the second mapping table. This can solve the problem of deserialization failure caused by adding or deleting attributes, or modifying attribute names or types in the data structure during deserialization.
[0064] In some embodiments of this application, Figure 6 The sixth illustration shows a flowchart of the serialization and deserialization method provided in this application embodiment, as shown below. Figure 6 As shown, deserialization of serialized data specifically includes:
[0065] Step S602: Deserialize the serialized data to obtain the size of the first mapping table.
[0066] Step S604: Based on the size of the first mapping table, the serialized data is deserialized in a loop to obtain the first attribute name and the first attribute type.
[0067] In this embodiment, when deserializing a serialized object, the size of the first mapping table, i.e., an integer, is deserialized first.
[0068] In this embodiment, the first attribute name and the first attribute type are deserialized in a loop according to the size of the first mapping table to obtain the first attribute name and the first attribute type in the first mapping table.
[0069] In this embodiment, by first deserializing the size of the first mapping table and then deserializing according to the size of the first mapping table, the orderly deserialization can be guaranteed, and the accuracy of deserialization of the first attribute name and the first attribute type can be improved.
[0070] In some embodiments of this application, the deserialization result is compared with a second mapping table, specifically including:
[0071] Compare the first attribute name with the second attribute name, and compare the first attribute type corresponding to the first attribute name with the second attribute type corresponding to the second attribute name.
[0072] In this embodiment, by comparing the first attribute name and the second attribute name in the first mapping table obtained through deserialization, it can be determined whether an attribute has been added or deleted, or whether the attribute name or type has been modified. For example, if the key-value pairs in the first and second mapping tables are the same, the first attribute name is obtained through deserialization, and a search is performed in the second mapping table. If a second attribute name with the same first attribute name is found, it means that the attribute name has not been modified; otherwise, the modified attribute name can be determined. Similarly, by comparing the first and second attribute types, it can be determined whether the attribute type has been modified. If the key-value pairs in the first and second mapping tables are different, if the second mapping table has more key-value pairs than the first mapping table, then an attribute has been added; otherwise, the attribute has been deleted.
[0073] In this embodiment, by constructing a second mapping table of key-value pairs of second attribute names and second attribute types, it can be compared with the first mapping table to provide verification for deserialization and avoid deserialization failure.
[0074] In some embodiments of this application, based on the comparison results, attribute values are deserialized and assigned values to obtain deserialized structured data, specifically including:
[0075] If the first attribute name and the second attribute name match, and the first attribute type and the second attribute type match, the attribute value of the serialized data is deserialized, and the attribute value is assigned to the second attribute object corresponding to the second attribute name, thus obtaining the deserialized structured data.
[0076] Understandably, if the first attribute name and the second attribute name are identical, and the first attribute type and the second attribute type are also identical, it means that the attribute has not changed. The attribute value is then directly deserialized and assigned to the second attribute object corresponding to the second attribute name.
[0077] In this embodiment, when the attributes remain unchanged, the attribute values are directly assigned to obtain structured data, thereby achieving deserialization and ensuring the accuracy of deserialization.
[0078] In some embodiments of this application, based on the comparison results, the attribute values are deserialized and assigned values to obtain structured data, and the method further includes:
[0079] If the first attribute name and the second attribute name are inconsistent, and / or the first attribute type and the second attribute type are inconsistent, a temporary object is created, the attribute value is deserialized, and the attribute value is assigned to the temporary attribute object to obtain the deserialized structured data.
[0080] For example, if a new attribute is added to the data structure during deserialization, with the attribute name being the third attribute name and the attribute type being the third attribute type, and a key-value pair of the third attribute name and the third attribute type is added to the second mapping table, then the second mapping table will have one more key-value pair than the first mapping table, and deserialization will still be successful.
[0081] To illustrate further, if during deserialization, a fourth attribute name (which could be any of the first attribute names) is deleted from the data structure, and the key-value pair containing the fourth attribute name and its corresponding attribute type is also deleted from the second mapping table, then the second mapping table will have one less key-value pair compared to the first mapping table, and deserialization will still succeed. However, because the second mapping table lacks a key-value pair for the fourth attribute, the deserialized first attribute name and type will fail to find a match in the predefined second mapping table. A temporary variable (or temporary object) will be created, the attribute value corresponding to the fourth attribute name will be deserialized, and assigned to the temporary variable. The deserialized structure will then contain no fourth attribute.
[0082] To give another example, if the type of the fifth attribute is changed during deserialization and updated synchronously in the second mapping table, the key-value pair of the fifth attribute name can still be deserialized successfully. However, since the type of the fifth attribute in the second mapping table has been modified, the deserialized fifth attribute name and type will fail to be found in the predefined second mapping table. A temporary variable is then created, and the attribute value of the fifth attribute name is deserialized and assigned to the temporary variable.
[0083] To give another example, if the name of the sixth attribute is modified during deserialization and updated synchronously in the second mapping table, the key-value pair of the sixth attribute can still be deserialized successfully. However, since the name of the sixth attribute in the second mapping table has been modified, the deserialized attribute name and type cannot be found in the predefined second mapping table. A temporary variable is created, the attribute value corresponding to the name of the sixth attribute is deserialized, and the value is assigned to the temporary variable.
[0084] In this embodiment, by comparing the first attribute name, the first attribute type, and the second mapping table obtained through deserialization, it is possible to clearly identify situations where attributes are added or deleted, or attribute names or types are modified, thereby avoiding deserialization failure.
[0085] In some embodiments of this application, the serialized data memory structure includes multiple loop substructures, each loop substructure including a first mapping table size, a first attribute name, a first attribute type, and an attribute value.
[0086] In this embodiment, the memory structure of the serialized data is as follows: Figure 9 As shown, the first mapping table size 300, the first attribute name 302, the first attribute type 304, and the attribute value 306 form a circular substructure 308, corresponding to one of the first attribute names. When there are multiple first attribute names, there are multiple circular substructures 308.
[0087] The serialization and deserialization methods provided in this application can be executed by a serialization and deserialization device. This application uses an example of a serialization and deserialization device executing the serialization and deserialization methods to illustrate the serialization and deserialization device provided in this application.
[0088] Some embodiments of this application provide a serialization and deserialization apparatus. Figure 7 A structural block diagram of the serialization and deserialization apparatus provided in an embodiment of this application is shown, as follows: Figure 7 As shown, the serialization and deserialization apparatus 100 includes a first construction module 110, a first serialization module 120, a first acquisition module 130, a second serialization module 140, a second construction module 150, a first deserialization module 160, and a second acquisition module 170. The first construction module 110 constructs a first mapping table based on the structured data to be serialized. The first serialization module 120 serializes the first mapping table. The first acquisition module 130 acquires attribute values based on the first mapping table. The second serialization module 140 serializes the attribute values to obtain serialized data. The second construction module 150 constructs a second mapping table based on the structured data to be serialized. The first deserialization module 160 deserializes the serialized data. The second acquisition module 170 compares the deserialization result with the second mapping table, and based on the comparison result, deserializes the attribute values and assigns them, obtaining the deserialized structured data.
[0089] In this embodiment, a mapping table is pre-set during both serialization and deserialization. A first mapping table is pre-set during serialization, and a second mapping table is pre-set during deserialization. By comparing the deserialized result with the second mapping table and adding relevant verification, deserialization failure can be avoided when adding or deleting attributes, or modifying attribute names or types in the data structure, thus ensuring the success of deserialization.
[0090] The serialization and deserialization apparatus 100 provided in this application embodiment can implement the various processes of the above-described serialization and deserialization method embodiments and achieve the same technical effect. To avoid repetition, it will not be described again here.
[0091] The serialization and deserialization devices in this application embodiment can be electronic devices or components within electronic devices, such as integrated circuits or chips. The electronic device can be a terminal or other devices besides a terminal. For example, the electronic device can be a mobile phone, tablet computer, laptop computer, PDA, in-vehicle electronic device, mobile internet device (MID), augmented reality (AR) / virtual reality (VR) device, robot, wearable device, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), etc. It can also be a server, network attached storage (NAS), personal computer (PC), television (TV), ATM, or self-service machine, etc. This application embodiment does not specifically limit the scope of the device.
[0092] The serialization and deserialization apparatus in this application embodiment can be an apparatus with an operating system. This operating system can be Android, iOS, or other possible operating systems; this application embodiment does not specifically limit the specific operating system used.
[0093] The serialization and deserialization apparatus provided in this application embodiment can implement the various processes implemented in the above method embodiments, and will not be described again here to avoid repetition.
[0094] Optionally, such as Figure 8 As shown, this application embodiment also provides an electronic device 1000, which includes a processor 1002 and a memory 1004. The memory 1004 stores a program or instructions that can run on the processor 1002. When the program or instructions are executed by the processor 1002, they implement the various steps of the above method embodiments and can achieve the same technical effect. To avoid repetition, they will not be described again here.
[0095] It should be noted that the electronic devices in the embodiments of this application include the aforementioned mobile electronic devices and non-mobile electronic devices.
[0096] The processor 1002 is used to construct a first mapping table based on the structured data that needs to be serialized.
[0097] Processor 1002 is used to serialize the first mapping table.
[0098] Processor 1002 is used to retrieve attribute values based on the first mapping table.
[0099] Processor 1002 is used to serialize attribute values to obtain serialized data.
[0100] Processor 1002 is used to construct a second mapping table based on structured data that needs to be serialized.
[0101] Processor 1002 is used to deserialize serialized data.
[0102] Processor 1002 is used to compare the deserialization result with the second mapping table, and based on the comparison result, deserialize the attribute values and assign them to obtain the deserialized structured data.
[0103] The processor 1002 provided in this application embodiment can implement the various processes of the above-described serialization and deserialization method embodiments and achieve the same technical effect. To avoid repetition, it will not be described again here.
[0104] The memory 1004 can be used to store software programs and various data. The memory 1004 may primarily include a first storage area for storing programs or instructions and a second storage area for storing data. The first storage area may store the operating system, application programs or instructions required for at least one function (such as sound playback, image playback, etc.). Furthermore, the memory 1004 may include volatile memory or non-volatile memory, or both. The non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory can be random access memory (RAM), static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct memory bus RAM (DRRAM). The memory 1004 in this embodiment includes, but is not limited to, these and any other suitable types of memory.
[0105] Processor 1002 may include one or more processing units; optionally, processor 1002 integrates an application processor and a modem processor, wherein the application processor mainly handles operations involving the operating system, user interface, and applications, and the modem processor mainly handles wireless communication signals, such as a baseband processor. It is understood that the aforementioned modem processor may also not be integrated into processor 1002.
[0106] This application also provides a readable storage medium storing a program or instructions. When the program or instructions are executed by a processor, they implement the various processes of the above-described serialization and deserialization method embodiments and achieve the same technical effect. To avoid repetition, they will not be described again here.
[0107] The processor is the processor in the electronic device described in the above embodiments. The readable storage medium includes computer-readable storage media, such as computer read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk.
[0108] This application also provides a chip, which includes a processor and a communication interface. The communication interface and the processor are coupled. The processor is used to run programs or instructions to implement the various processes of the above-described serialization and deserialization method embodiments and achieve the same technical effect. To avoid repetition, it will not be described again here.
[0109] It should be understood that the chip mentioned in the embodiments of this application may also be referred to as a system-on-a-chip, system chip, chip system, or system-on-a-chip, etc.
[0110] This application provides a computer program product stored in a storage medium. The program product is executed by at least one processor to implement the various processes of the serialization and deserialization method embodiments described above, and can achieve the same technical effect. To avoid repetition, it will not be described again here. Specific implementation examples:
[0112] In the research on related technologies concerning structured data serialization and deserialization, the following descriptions address the problem of deserialization failure due to differences in data structures. The first related technology describes different version numbers corresponding to different data structures, which can avoid failures caused by version inconsistencies in serialization and deserialization. However, associating version numbers with data structures leads to code redundancy and maintenance difficulties when the data structure is frequently modified. For example, modifying the data structure 10 times requires 10 corresponding version numbers, and parsing will require parsing different data structures based on the version. The second related technology describes generating different attribute ordinals for different attributes, and performing serialization and deserialization based on the binding relationship between attribute ordinals and attribute values, thus avoiding deserialization failures. However, associating ordinals with attributes means that changes to attribute names or types will still cause deserialization failures. The third related technology describes different attributes corresponding to different attribute operation classes, and performing serialization and deserialization based on the read / write methods in the attribute operation classes and the order of attribute description ordinals, avoiding compatibility issues between systems after changing object attributes. However, associating ordinals with attributes means that changing the order of attributes in the data structure will cause deserialization failures.
[0113] For example, suppose the structured data 'student' is defined as follows:
[0114]
[0115] like Figure 10 As shown, the serialization and deserialization method in this embodiment specifically includes:
[0116] Step S702: A first mapping table is predefined within the data structure to store key-value pairs of the first attribute name and the first attribute type that need to be serialized.
[0117] For structured data, a first mapping table `mapSerialize1` is predefined in the `student` structure to store key-value pairs containing the first attribute name and type to be serialized. Here, the key is the attribute name, and the value is the attribute type. Since only the attributes `id`, `name`, and `age` (corresponding to ID, name, and age respectively) need to be serialized, `mapSerialize1` stores three attribute key-value pairs: `key = id, value = int`, `key = name, value = string`, and `key = age, value = int`. Here, `int` represents an integer type, and `string` represents a string type.
[0118] Step S704: When the structured data is serialized, calculate the size of the first mapping table and serialize the size of the first mapping table.
[0119] When serializing the student structure, first calculate the size of the first mapping table mapSerialize1, which is an integer data, and find that the size is 3, and then serialize its size.
[0120] Step S706: Traverse the first attribute name and first attribute type in the first mapping table and perform serialization.
[0121] Iterate through the first mapping table mapSerialize1 and serialize the names and types (id, int, name, string, age, int) of the attributes that need to be serialized in the student structure in sequence.
[0122] Step S708: Based on the first attribute name, find the corresponding first attribute object and serialize the attribute value.
[0123] Based on the attribute name stored in the key of the first mapping table mapSerialize1, the attribute object is located and the attribute value is serialized. For example, first, the attribute object (id) is located based on the attribute name (id) and then serialized. The same process applies to attributes name and age.
[0124] Finally, serialized data is obtained.
[0125] Step S710: A second mapping table is predefined within the structure to store key-value pairs of the second attribute name and the second attribute type that need to be serialized.
[0126] In the student structure, a second mapping table mapSerialize2 is predefined to store the key-value pairs of the second attribute name and the second type attribute that need to be serialized, where the key is the attribute name and the value is the attribute type. Since only the attributes id, name, and age need to be serialized, mapSerialize2 stores three attribute key-value pairs: key = id, value = int, key = name, value = string, and key = age, value = int.
[0127] Step S712: Deserialize the size of the first mapping table.
[0128] When deserializing a serialized object, the first deserialization is performed on the size of the first mapping table, which is an integer. Since the size of the first mapping table during serialization is 3, the size of the first mapping table during deserialization is also 3.
[0129] Step S714: Based on the size of the first mapping table, deserialize the first attribute name and the first attribute type in a loop.
[0130] Based on the size of the first mapping table, deserialize the first attribute name and the first attribute type in a loop to get id, int, name, string, age, int.
[0131] Step S716: Compare the first attribute name and first attribute type after deserialization with the second attribute name and second attribute type in the second mapping table, and obtain structured data based on the comparison result.
[0132] The deserialized first attribute name and first attribute type are sequentially searched in the predefined second mapping table. First, the attribute name is searched, and then the attribute type is compared. If both the attribute name and type are successfully compared, the attribute value is deserialized and assigned to the object corresponding to the attribute name. If the comparison fails, a temporary object of the deserialized attribute type is created, the attribute value is deserialized, and assigned to the temporary object.
[0133] Specifically, the deserialized first attribute name and first attribute type (id, int; name, string; age, int) are sequentially searched in the predefined second mapping table mapSerialize2. First, key-value pairs with the attribute name being id are searched. If found, the attribute types are compared. If both the name id and the type int match, the attribute value is deserialized and assigned to the id object. Otherwise, a temporary object of the deserialized attribute type is created, the attribute value is deserialized, and assigned to the temporary object. The same process is followed for the attributes name and age.
[0134] Furthermore, if during deserialization, an attribute named "sex" (gender) is added to the "student" structure, with the attribute name being "sex" and the attribute type being "int", and key-value pairs of the attribute "sex" are added to the second mapping table "mapSerialize2", then "mapSerialize2" will store four attribute key-value pairs: key=id, value=int; key=name, value=string; key=age, value=int; and key=sex, value=int. Deserialization will still succeed because the "sex" attribute was not present during serialization. Therefore, the value of the "sex" attribute in the deserialized structure will be an integer default value, which can be set to 0 (here, the number 0 represents male and the number 1 represents female).
[0135] Furthermore, if during deserialization, the `id` attribute is deleted from the `student` structure, and the `id` key-value pair is also deleted from the mapping table `mapSerialize2`, then `mapSerialize2` still stores two attribute key-value pairs: `key=name, value=string` and `key=age, value=int`. Deserialization will still succeed because the `id` key-value pair is missing from the second mapping table `mapSerialize2`. Therefore, the deserialized attribute name and type cannot be found in the predefined second mapping table `mapSerialize2`. A temporary variable of type `int` is created, the `id` attribute value is deserialized, and assigned to the temporary variable. The deserialized structure will then lack the `id` attribute (i.e., the variable `id` has been deleted from the structure).
[0136] Furthermore, if the type of id is changed to string during deserialization and updated synchronously in the second mapping table mapSerialize2, the key-value pair of the attribute id will still be key=id, value=string, and deserialization will still succeed. However, since the type of the attribute id has been modified in the second mapping table mapSerialize2, the deserialized attribute name and type will fail to be found in the predefined second mapping table mapSerialize2. A temporary variable of type int is created, the value of the id attribute is deserialized and assigned to the temporary variable, and the value of the id attribute in the deserialized structure is the default string value, which can be set to empty.
[0137] Furthermore, if the name of id is changed to num during deserialization and updated synchronously in the second mapping table mapSerialize2, the key-value pair of the attribute num will be key=num and value=int, and deserialization will still succeed. Since the name of the attribute id has been modified in the second mapping table mapSerialize2, the name and type of the deserialized attribute will fail to be found in the predefined second mapping table mapSerialize2. A temporary variable of type int will be created, the value of the id attribute will be deserialized and assigned to the temporary variable, and the value of the num attribute in the deserialized structure will be the default integer value.
[0138] In this embodiment, a mapping table is predefined during both serialization and deserialization to store key-value pairs of attribute names and types that need to be serialized. Therefore, when deserializing the serialized data, the attribute names and types after deserialization can be compared with the key-value pairs of attribute names and types stored in the mapping table. This can prevent deserialization failure caused by adding or deleting attributes, or modifying attribute names or types in the data structure.
[0139] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element. Furthermore, it should be noted that the scope of the methods and apparatuses in the embodiments of this application is not limited to performing functions in the order shown or discussed, but may also include performing functions substantially simultaneously or in the reverse order, depending on the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.
[0140] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a computer software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of this application.
[0141] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of this application.
Claims
1. A serialization and deserialization method, characterized in that, include: Based on the structured data that needs to be serialized, construct the first mapping table; Serialize the first mapping table; Based on the first mapping table, obtain the attribute value; The attribute values are serialized to obtain serialized data; Based on the structured data that needs to be deserialized, construct a second mapping table; The serialized data is deserialized; The deserialized result is compared with the second mapping table. Based on the comparison result, the attribute values are deserialized and assigned values to obtain the deserialized structured data. The construction of the first mapping table based on the structured data that needs to be serialized specifically includes: Obtain the first attribute name of the structured data that needs to be serialized; Get the first attribute type corresponding to the first attribute name; Based on the first attribute name and the first attribute type, construct the first mapping table; Wherein, the first attribute name and the first attribute type corresponding to the first attribute name form a key-value pair, the first mapping table is used to store the key-value pairs of the first attribute name and the first attribute type that need to be serialized; the second mapping table stores the key-value pairs of the second attribute name and the second attribute type. The step of comparing the deserialized result with the second mapping table specifically includes: Compare the first attribute name with the second attribute name, and compare the first attribute type corresponding to the first attribute name with the second attribute type corresponding to the second attribute name; In the case where the key-value pair of the first mapping table is the same as the key-value pair of the second mapping table, if the second attribute name is found in the second mapping table with the same first attribute name, it is determined that the attribute name has not been modified; otherwise, it is determined that the attribute name has been modified. The process of deserializing and assigning attribute values based on the comparison results to obtain structured data also includes: If the first attribute name and the second attribute name are inconsistent, and / or the first attribute type and the second attribute type are inconsistent, a temporary object is created, the attribute value is deserialized, and the attribute value is assigned to the temporary object to obtain the deserialized structured data.
2. The serialization and deserialization method according to claim 1, characterized in that, The serialization of the first mapping table specifically includes: Get the size of the first mapping table; Serialize the size of the first mapping table; Traverse the first mapping table and serialize the first attribute name and the first attribute type.
3. The serialization and deserialization method according to claim 1, characterized in that, The step of obtaining attribute values based on the first mapping table specifically includes: Based on the first attribute name, find the first attribute object corresponding to the first attribute name; Get the attribute value of the first attribute object.
4. The serialization and deserialization method according to claim 1, characterized in that, The construction of the second mapping table based on the structured data that needs to be deserialized specifically includes: Obtain the name of the second attribute of the structured data that needs to be deserialized; Get the type of the second attribute corresponding to the second attribute name; The second mapping table is constructed based on the second attribute name and the second attribute type.
5. The serialization and deserialization method according to claim 4, characterized in that, The deserialization of the serialized data specifically includes: The serialized data is deserialized to obtain the size of the first mapping table; Based on the size of the first mapping table, the serialized data is deserialized in a loop to obtain the first attribute name and the first attribute type.
6. The serialization and deserialization method according to claim 1, characterized in that, Based on the comparison results, the attribute values are deserialized and assigned values to obtain deserialized structured data, specifically including: If the first attribute name matches the second attribute name and the first attribute type matches the second attribute type, the attribute value of the serialized data is deserialized, and the attribute value is assigned to the second attribute object corresponding to the second attribute name to obtain the deserialized structured data.
7. The serialization and deserialization method according to any one of claims 1 to 6, characterized in that, The serialized data memory structure includes multiple loop substructures, each loop substructure including a first mapping table size, a first attribute name, a first attribute type, and an attribute value.
8. A serialization and deserialization apparatus, characterized in that, include: The first building module is used to construct the first mapping table based on the structured data that needs to be serialized; The first serialization module is used to serialize the first mapping table; The first acquisition module is used to acquire attribute values based on the first mapping table; The second serialization module is used to serialize the attribute values to obtain serialized data; The second building module is used to construct a second mapping table based on the structured data that needs to be deserialized; The first deserialization module is used to deserialize the serialized data; The second acquisition module is used to compare the deserialization result with the second mapping table, and based on the comparison result, deserialize the attribute values and assign them to obtain the deserialized structured data. The first construction module is specifically used to obtain the first attribute name of the structured data to be serialized; obtain the first attribute type corresponding to the first attribute name; and construct the first mapping table based on the first attribute name and the first attribute type. Wherein, the first attribute name and the first attribute type corresponding to the first attribute name form a key-value pair, the first mapping table is used to store the key-value pairs of the first attribute name and the first attribute type that need to be serialized; the second mapping table stores the key-value pairs of the second attribute name and the second attribute type. The second acquisition module is specifically used to compare the first attribute name with the second attribute name, and to compare the first attribute type corresponding to the first attribute name with the second attribute type corresponding to the second attribute name. In the case where the key-value pair of the first mapping table is the same as the key-value pair of the second mapping table, if the second attribute name is found in the second mapping table with the same first attribute name, it is determined that the attribute name has not been modified; otherwise, it is determined that the attribute name has been modified. The second acquisition module is further configured to, in the event that the first attribute name and the second attribute name are inconsistent, and / or the first attribute type and the second attribute type are inconsistent, create a temporary object, deserialize the attribute value, assign the attribute value to the temporary object, and obtain the deserialized structured data.
9. An electronic device, characterized in that, include: A memory that stores programs or instructions; A processor for implementing the serialization and deserialization methods as described in any one of claims 1 to 7 when executing the program or instructions.
10. A readable storage medium having a program or instructions stored thereon, characterized in that, When the program or instructions are executed by the processor, they implement the steps of the serialization and deserialization method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Object serializing method, object deserializing method, device and system
CN101661391A