Serialization and deserialization method and device, electronic equipment and storage medium
By sorting and serializing the fields in the structured data by version number and serializing, the data redundancy problem caused by structured data field update is solved, and the efficiency of serialization and deserialization operations is improved.
Patent Information
- Application Number
- CN202311469864.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-06
- Publication Date
- 2025-05-06
AI Technical Summary
During software development, field updates of structured data lead to redundancy of serialized data, thereby reducing the efficiency of serialization and deserialization operations.
By sorting the fields in the structured data by version numbers, the data to be serialized is generated, and the field numbers are not added during the serialization process, and the serialization process is directly performed.
It reduces data redundancy, improves the efficiency of serialization and deserialization operations, and solves the problem of low operation efficiency in the prior art.
Smart Images

Figure CN119938635A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and in particular to a serialization and deserialization method, apparatus, device and storage medium. Background Art
[0002] In the software development process, serialization libraries are usually used for data communication, data storage and other operations; for example, the information transmitted when the client and server communicate is packaged through the data structure defined by the serialization library, and then compiled into a binary code stream for transmission or storage.
[0003] Structured data is a common data structure defined by a serialization library during the interaction between clients and servers. With the iterative updates of programs in the client or server, structured data fields are usually updated. In order to ensure data compatibility, it is usually necessary to add the field numbers of each field of the structured data to the serialized data after the structured data is serialized. This will cause data redundancy, resulting in low efficiency of serialization and deserialization operations. Summary of the invention
[0004] In view of the above problems, the present application provides a serialization method and deserialization method, device, equipment and storage medium to at least solve the technical problem of low efficiency of serialization and deserialization operations in the related art.
[0005] According to the first aspect of an embodiment of the present application, a serialization method is provided, comprising: sorting the above-mentioned fields according to the version numbers corresponding to the above-mentioned fields in the structured data to obtain the data to be serialized corresponding to the above-mentioned structured data; the above-mentioned structured data includes the fields from the time when the above-mentioned structured data is created to the current moment; based on the above-mentioned data to be serialized, generating the serialization result of the above-mentioned structured data.
[0006] According to the second aspect of an embodiment of the present application, a deserialization method is provided, comprising: obtaining a serialization result to be deserialized, the serialization result being obtained by the serialization method of the first aspect; based on the current version of the compiler, deserializing the serialization result to obtain structured data corresponding to the current version.
[0007] According to the third aspect of an embodiment of the present application, a serialization device is provided, and the above-mentioned information retrieval optimization device includes: a sorting unit, used to sort the above-mentioned fields according to the version numbers corresponding to the above-mentioned fields in the structured data, and obtain the data to be serialized corresponding to the above-mentioned structured data; the above-mentioned structured data includes each field from the time when the above-mentioned structured data is created to the current moment; a generating unit, used to generate the serialization result of the above-mentioned structured data based on the above-mentioned data to be serialized.
[0008] According to the fourth aspect of an embodiment of the present application, a serialization device is provided, including an acquisition unit for acquiring a serialization result to be deserialized, wherein the serialization result is obtained by the serialization method of the first aspect; and a deserialization unit for deserializing the serialization result based on a current version of a compiler to obtain structured data corresponding to the current version.
[0009] According to the fifth aspect of the embodiments of the present application, there is also provided an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the serialization method of the first aspect through the computer program, and / or execute the deserialization method of the second aspect.
[0010] According to the sixth aspect of an embodiment of the present application, a computer-readable storage medium is also provided, in which a computer program is stored, wherein the computer program is configured to execute the serialization method of the first aspect and / or the deserialization method of the second aspect when running.
[0011] In an embodiment of the present application, the above-mentioned fields are sorted according to the version numbers corresponding to the fields in the structured data to obtain the data to be serialized corresponding to the above-mentioned structured data; the above-mentioned structured data includes the fields from the creation of the above-mentioned structured data to the current moment; based on the above-mentioned data to be serialized, a method for generating the serialization result of the above-mentioned structured data; because when serializing data, the present application first sorts the fields in the structured data according to the version numbers, and the sorted data is serialized, the serialized data required by the compiler of different versions can be accurately obtained, and there is no need to add the field numbers of the fields of the structured data to the serialized data; therefore, the present application can not only reduce data redundancy, but also improve the efficiency of serialization and deserialization operations, and can solve the technical problem of low efficiency of serialization and deserialization operations in related technologies. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Moreover, the same reference numerals are used throughout the drawings to represent the same components. In the drawings:
[0013] Figure 1 is a schematic diagram of an application environment of an optional data processing method according to an embodiment of the present application;
[0014] Figure 2 is a schematic diagram of an application environment of another optional data processing method according to an embodiment of the present application;
[0015] Figure 3 is a flowchart of an optional serialization method according to an embodiment of the present application;
[0016] Figure 4 is a flowchart of another optional serialization method according to an embodiment of the present application;
[0017] Figure 5 is a flowchart of another optional serialization method according to an embodiment of the present application;
[0018] Figure 6 is a flowchart of another optional serialization method according to an embodiment of the present application;
[0019] Figure 7 is a flowchart of another optional serialization method according to an embodiment of the present application;
[0020] Figure 8 is a flowchart of another optional serialization method according to an embodiment of the present application;
[0021] Fig. 9 is a flowchart of an optional deserialization method according to an embodiment of the present application;
[0022] Fig.10 is a flowchart of another optional deserialization method according to an embodiment of the present application;
[0023] Fig.11 is a flowchart of another optional deserialization method according to an embodiment of the present application;
[0024] Fig.12 It is a structural schematic diagram of a serialization device provided in an embodiment of the present application;
[0025] Fig.13 It is a structural schematic diagram of a deserialization device provided in an embodiment of the present application;
[0026] Fig.14 is a structural schematic diagram of an electronic device provided in an embodiment of the present application;
[0027] Fig.15 is a schematic diagram of the structure of another electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0028] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only embodiments of a part of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0029] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0030] Optionally, according to one aspect of an embodiment of the present application, a data processing method is provided, which includes a method for serializing and a method for deserializing structured data; the execution subjects of the above-mentioned serialization method and deserialization method can be different electronic devices, for example, the serialization method is executed on a terminal, and the deserialization method is executed in a server; the execution subjects of the above-mentioned serialization method and deserialization method can also be the same electronic device.
[0031] As an optional implementation, the above data processing method can be applied to, but is not limited to, Figure 1 In the application environment shown. The application environment may include, but is not limited to: a terminal device 102 for human-computer interaction with a user, a network 110, and a server 112. A data processing application client with serialization and deserialization functions is running in the terminal device 102. The terminal device 102 includes a display 108, a processor 106, and a memory 104. The display 108 is used to present the data to be serialized. The processor 106 is used to perform serialization operations on structured data. The memory 104 is used to store the data to be serialized and the serialization results. In addition, the server 112 includes a database 114 and a processing engine 116, and the database is used to store the serialization results to be deserialized. The processing engine 116 is used to deserialize the above serialization results based on the current version of the compiler to obtain structured data corresponding to the above current version.
[0032] The specific process is as follows: the terminal device 102 executes step S102 and sends a data request to the server 112. After receiving the data request, the server 112 executes steps S104-S108, sorts the above fields according to the version number corresponding to each field in the structured data, and obtains the data to be serialized corresponding to the above structured data; the above structured data includes each field from the creation of the above structured data to the current moment, and the above version number is used to represent the order in which the above structured data is modified; based on the above data to be serialized, the serialization result of the above structured data is generated; and the serialization result is sent to the terminal device 102. After receiving the serialization result sent by the server 112, the terminal device 102 executes steps S110-S112 and sends the search text entered by the user to the server 112. The server 112 executes step S108 to obtain the serialization result to be deserialized; based on the current version of the compiler, the above serialization result is deserialized to obtain the structured data corresponding to the above current version.
[0033] In one or more embodiments, the serialization method described above can be applied to Figure 2 In the application environment shown. Figure 2 As shown, human-computer interaction can be performed between user 202 and user device 204. User device 204 includes memory 206 and processor 208. In this embodiment, user device 204 can, but is not limited to, refer to and execute the operations performed by the above-mentioned terminal device 102 to obtain structured data corresponding to the data request. In an example, user device 204 can also serialize the structured data in the application, so that the structured data leaves the memory space and is stored in the physical hard disk of user device 204 for long-term storage.
[0034] Optionally, the terminal device 102 and the user device 204 include but are not limited to mobile phones, laptops, tablet computers, PDAs, MIDs (Mobile Internet Devices), desktop computers, smart TVs, etc. The application clients with serialization and deserialization functions in the embodiments of the present application include but are not limited to video clients, instant messaging clients, browser clients, etc. The network may include but are not limited to: wired networks, wireless networks, wherein the wired network includes: local area networks, metropolitan area networks and wide area networks, and the wireless network includes: Bluetooth, WIFI and other networks that implement wireless communication. The server 112 may be a single server, or a server cluster consisting of multiple servers, or a cloud server, wherein the cloud server includes but is not limited to a private cloud server or a public cloud server. The above is only an example, and no limitation is made to this in the present embodiment.
[0035] In the related art, structured data is a data structure defined by a serialization library that is common in the process of interaction between clients and servers. With the iterative update of the program in the client or server, the fields of structured data will usually be updated. In order to ensure data compatibility, it is usually necessary to add the field numbers of each field of the structured data to the serialized data after the structured data is serialized. This will cause data redundancy, resulting in low efficiency of serialization and deserialization operations.
[0036] In order to solve the above technical problems, as an optional implementation method, Figure 3 As shown, the embodiment of the present application provides a serialization method, comprising the following steps:
[0037] S302, sorting the above fields according to the version numbers corresponding to the fields in the structured data to obtain the data to be serialized corresponding to the above structured data; the above structured data includes the fields from the time when the above structured data is created to the current moment, and the above version numbers are used to represent the order in which the above structured data is modified.
[0038] In an embodiment of the present application, before serializing the structured data to be serialized, it is necessary to sort the fields in the structured data according to the version numbers corresponding to the fields, and after sorting, obtain the data to be serialized corresponding to the structured data. The structured data here includes but is not limited to structured data, or customized object data. The following embodiments are described by taking the structured data as a structure as an example.
[0039] Specifically, for example, when structure S1 is created, it includes two fields, A and B. The version number corresponding to fields A and B is V1. From creation to the current moment, structure S1 has been modified twice. The first modification is to add field D, and the version number corresponding to field D is V2. The second modification adds fields C and E, and the version numbers corresponding to fields C and E are V3. After sorting the fields in structure S1 according to their corresponding version numbers, the data to be serialized is [A, B, D, C, E].
[0040] S304: Generate a serialization result of the structured data based on the data to be serialized.
[0041] Specifically, for example, based on the data to be serialized [A, B, D, C, E] corresponding to structure S1, a serialization operation is performed on structure S1 to obtain a serialization result of structure S1. Since the present application first sorts the fields in the structured data according to the version number when serializing the data, and the data in the serialization result obtained after the sorted data is serialized is also the serialized data corresponding to the sorted fields, it is possible to accurately obtain the serialization data required by the compiler of different versions. It should be noted that the serialization result in the embodiment of the present application includes but is not limited to data in Json (JavaScript Object Notation) format or Xml (Extensible Markup Language) format.
[0042] In an embodiment of the present application, the above-mentioned fields are sorted according to the version numbers corresponding to the fields in the structured data to obtain the data to be serialized corresponding to the above-mentioned structured data; the above-mentioned structured data includes all fields from the creation of the above-mentioned structured data to the current moment, and the above-mentioned version number is used to characterize the order in which the above-mentioned structured data is modified; based on the above-mentioned data to be serialized, a method for generating a serialization result of the above-mentioned structured data; because when serializing data, the present application first sorts the fields in the structured data according to the version number, after the sorted data is serialized, the serialized data required by the compiler of different versions can be accurately obtained, and there is no need to add the field numbers of the fields of the structured data to the serialized data; therefore, the present application can not only reduce data redundancy, but also improve the efficiency of serialization and deserialization operations, thereby solving the technical problem of low efficiency of serialization and deserialization operations in related technologies.
[0043] In one or more embodiments, Figure 4 As shown, the embodiment of the present application also provides a serialization method, comprising the following steps:
[0044] S402, in the process of creating the structured data, configuring an initial version number for each field in the structured data;
[0045] S404: In the process of modifying the structured data, a corresponding version number is configured for an existing field in the structured data.
[0046] Specifically, in the process of creating the above structured data, for example, the structured data includes a structure S1, which includes two fields A and B when created, and the fields A and B are configured with an initial version number V1.
[0047] In the process of modifying the above structured data, the above existing fields include newly added fields and fields obtained by updating the original data. For example, the structure S1 has been modified twice from its creation to the current moment. The first modification is to add field D, and the version number corresponding to the newly added field D is V2; the second modification adds field C and field E, and the version number corresponding to the newly added field C and field E is V3. In another example, in the process of modifying the above structured data, it also includes modifying the original data in the structured data. For example, when the structure S1 is created, it includes two fields A and B. When modifying the structure S1, field A or field B can be updated, such as replacing field A with field F, then the structure S1 currently includes field F and field B. The version number corresponding to field F is V2, and the version number corresponding to field B is V1.
[0048] S406, sorting the fields in the structured data according to the version numbers corresponding to the fields, to obtain data to be serialized corresponding to the structured data; the structured data includes all fields from the time when the structured data is created to the current moment, and the version numbers are used to indicate the order in which the structured data is modified;
[0049] S408: Generate a serialization result of the structured data based on the data to be serialized.
[0050] The above steps S406-S408 have been clearly explained in the previous text and will not be repeated here.
[0051] In one or more embodiments, Figure 5 As shown, the embodiment of the present application also provides a serialization method, comprising the following steps:
[0052] S502, determining the sequence of version numbers corresponding to the fields in the structured data;
[0053] S504, sorting the above fields according to the order of the version numbers corresponding to the above fields to obtain the data to be serialized corresponding to the above structured data.
[0054] Specifically, in the embodiment of the present application, for example, structure S1 includes a field set of {A, B, C, D, E, F}. The version number corresponding to field A and field D is V1, the version number of field B and field E is V2, and the version number corresponding to field C and field F is V3. The above fields of structure S1 are sorted in the order of the version numbers corresponding to the above fields, and the data to be serialized corresponding to the above structure S1 is [A, D, B, E, C, F].
[0055] S506: Generate a serialization result of the structured data based on the data to be serialized.
[0056] The above step S506 has been clearly explained in the previous text and will not be repeated here.
[0057] In one or more embodiments, Figure 6 As shown, the embodiment of the present application also provides a serialization method, comprising the following steps:
[0058] S602, in the process of creating the structured data, configuring an initial version number for each field in the structured data;
[0059] S604: In the process of modifying the structured data, a corresponding version number is configured for an existing field in the structured data.
[0060] S606, determining the sequence of version numbers corresponding to the fields in the structured data;
[0061] S608, sorting the above fields according to the order of the version numbers corresponding to the above fields to obtain the data to be serialized corresponding to the above structured data;
[0062] S610: Generate a serialization result of the structured data based on the data to be serialized.
[0063] Specifically, steps S602-S610 in the embodiment of the present application have been clearly described above and will not be repeated here.
[0064] In one or more embodiments, Figure 7 As shown, the embodiment of the present application also provides a serialization method, comprising the following steps:
[0065] S702: In the process of creating the structured data, an initial version number is configured for each field in the structured data.
[0066] S704: In the process of modifying the structured data, a corresponding version number is configured for an existing field in the structured data.
[0067] S706, determining the sequence of version numbers corresponding to the fields in the structured data;
[0068] S708, determining fields having the same version number;
[0069] S710, based on the relative positions of the fields with the same version number in the structured data, arranging the fields with the same version number to form a field set corresponding to the same version number;
[0070] S712, sorting the above-mentioned field sets based on the sequence of version numbers corresponding to the above-mentioned field sets to obtain the data to be serialized corresponding to the above-mentioned structured data.
[0071] Specifically, for example, structure S1 includes a field set of {A, B, C, D, E, F, G}. Field A, field D and field G have the same version number V1, field B and field E have the same version number V2, and field C and field F have the same version number V3. Based on the relative positions of the above fields with the same version number in S1, for example, field G is before field D, the above fields with the same version number are arranged to form a field set corresponding to the above same version number, including the field set corresponding to version number V1 is {A, G, D}; similarly, the field set corresponding to version number V3 is {C, F}, and the field set corresponding to version number V2 is {E, B}. Sort the above fields of structure S1 in the order of the version numbers corresponding to the above fields, and obtain the data to be serialized corresponding to the above structure S1 as [{A, G, D}, {C, F}, {E, B}].
[0072] S714: Generate a serialization result of the structured data based on the data to be serialized.
[0073] The above steps S702-S704 and step S714 have been clearly explained in the previous text and will not be repeated here.
[0074] In one or more embodiments, Figure 8 As shown, the embodiment of the present application also provides a serialization method, comprising the following steps:
[0075] S802, in the process of creating the structured data, configuring an initial version number for each field in the structured data;
[0076] S804, in the process of modifying the structured data, adding a new field to a target position in the structure, and configuring a corresponding version number for the new field; wherein the target position includes any position in the structure.
[0077] Specifically, in the embodiment of the present application, for example, when the structure S1 is created, it includes two fields A and B, and the version number corresponding to field A and field B is V1. From the creation to the current moment, the structure S1 has been modified twice. The first modification is to add field D after field A and before field B, and the version number corresponding to field D is V2; the second modification is to modify field B to field C, and field B is not deleted after the modification, and the version number corresponding to field C is V3. Compared with the serialization method in the related art that can only add new fields at the end of the structure and does not allow any fields to be added inside the nested structure, the present application can realize the addition of new fields at any position of the structure, and ensure the compatibility of data during the serialization and deserialization process of data.
[0078] S806, sorting the fields in the structured data according to the version numbers corresponding to the fields, to obtain data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment, and the version numbers are used to indicate the order in which the structured data is modified;
[0079] S808: Generate a serialization result of the structured data based on the data to be serialized.
[0080] The above step S802 and steps S806-S808 have been clearly explained in the previous text and will not be repeated here.
[0081] In one or more embodiments, Fig. 9 As shown, the embodiment of the present application also provides a deserialization method, comprising the following steps:
[0082] S902, obtaining a serialization result to be deserialized, where the serialization result is obtained by any of the serialization methods in the above examples;
[0083] S904, based on the current version of the compiler, deserialize the serialization result to obtain structured data corresponding to the current version.
[0084] Specifically, in the embodiment of the present application, the compiler here includes but is not limited to a deserialization program corresponding to the program that performs the serialization operation. For example, the current version of application 1 in the terminal includes structure S1. When application 1 sends an upgrade request to the server, the server serializes the upgrade data, and the upgrade data includes the modified structure S1. The terminal receives the serialization result corresponding to the modified structure S1 sent by the server, and based on the current version of application A, deserializes the above serialization result to obtain structured data corresponding to the above current version.
[0085] In one or more embodiments, Fig.10 As shown, the embodiment of the present application also provides a deserialization method, comprising the following steps:
[0086] S1002, obtaining a serialization result to be deserialized, where the serialization result is obtained by any of the serialization methods in the above examples;
[0087] S1004, in the case where the current version is smaller than the highest version corresponding to the field set in the serialization result, identifying serialized data in the serialization result whose version is smaller than or equal to the current version, and discarding serialized data whose version is larger than the current version;
[0088] S1006, deserialize the serialized data whose version is less than or equal to the current version to obtain structured data corresponding to the current version.
[0089] Specifically, in an embodiment of the present application, for example, the current version V1 of application 1 in the terminal includes a structure S1, and the field set in S1 is {A, B, C}, and the versions corresponding to field A, field B, and field C are all V1. When application 1 sends a data request to the server, the server determines the response data corresponding to the data request, and the field set included in the structure S1 in the response data is {A, B, C, D, E, F}, and the version numbers corresponding to field D and field E are V3, and the version number corresponding to field F is V2. The fields in the response data are sorted according to the version number to obtain the data to be serialized [{A, B, C}, {F}, {D, E}], and the serialized data is serialized to obtain the corresponding serialization result [{A', B', C'}, {F'}, {D', E'}].
[0090] It should be noted that the serialization result [{A', B', C'}, {F'}, {D', E'}] here is a set of multiple serialized data (A', B', C', F', D', E'), and the multiple serialized data are the serialized data obtained after serializing the fields to be serialized {A, B, C, D, E, F}. In the process of deserializing the serialization result, application 1 deserializes the serialization results {A', B', C'} corresponding to the known fields in the serialization result, field A, field B and field C according to the fields of the structure S1 included in the current version V1, and discards the unknown serialized data {F'}, {D', E'} at the end. The data corresponding to the structure S1 of the above current version is obtained. Based on the above technical means, the present application can realize that the old version of the program can correctly parse the data of the new version of the program, that is, the forward compatibility of the program is improved.
[0091] In one or more embodiments, Fig.11 As shown, the embodiment of the present application also provides a deserialization method, comprising the following steps:
[0092] S1102, obtaining a serialization result to be deserialized, where the serialization result is obtained by any of the serialization methods in the above examples;
[0093] S1104, when the current version is greater than the highest version corresponding to the field set in the serialization result, deserialize each serialized data in the serialization result to obtain deserialized data;
[0094] S1106, adding newly added fields of each version from the highest version to the current version to the end of the deserialized data, and setting the newly added fields to null values to obtain structured data of the current version.
[0095] Specifically, in an embodiment of the present application, for example, the current version V3 of application 1 in the terminal includes a structure S1, and the field set in S1 is {A, B, C, D, E, F}. The versions corresponding to field A, field B, and field C are all V1, the version numbers corresponding to field D and field E are V3, and the version number corresponding to field F is V2. When application 1 sends a data request to the server, the server determines the response data corresponding to the data request, and the field set included in the structure S1 in the response data is {A, B, C}. The fields in the response data are sorted according to the version number to obtain the data to be serialized [{A, B, C}], and the serialized data is serialized to obtain the corresponding serialization result [{A', B', C'}]. In the process of deserializing the serialized result, application 1 deserializes the serialized results {A', B', C'} corresponding to fields A, B and C in the serialized result according to the fields of structure S1 in the current version V3, adds fields F, D and E to the end of the serialized result {A', B', C'}, and sets fields D, E and F to null values. Based on the above technical means, the application can realize that the new version of the program can correctly parse the data of the old version, that is, improve the backward compatibility of the program.
[0096] Based on the above embodiments, as an optional implementation method, the embodiment of the present application also provides a data processing method, which includes a serialization method and a deserialization method. When modifying the structure in the data processing method provided in the example of the present application, the following rules must be met: 1. Do not delete the original fields in the structure. 2. All newly added fields in the structure can be set to null values. 3. Each change in the structure corresponds to an incremented version number, and the newly added fields correspond to the version number.
[0097] The above serialization method includes: step S11, based on the compile-time reflection mechanism, adding an incremental version number to the newly added field each time the structure is modified.
[0098] Step S12, sort all the fields with version numbers in the structure of the data to be serialized by version number; if the version numbers of multiple fields are the same, the order can be determined according to their relative positions in the structure. When the structure is serialized, the fields in the structure are serialized according to the above sorting result, so as to ensure that the relative position of each field in the serialized data corresponds to the above sorting result.
[0099] The deserialization method comprises: step S21, in the case where the current version is less than the highest version corresponding to the field set in the serialization result, identifying serialized data in the serialization result whose version is less than or equal to the current version, and discarding serialized data whose version is greater than the current version;
[0100] Step S22, deserialize the serialized data whose version is less than or equal to the current version to obtain the structured data of the current version.
[0101] In the embodiment of the present application, forward compatibility of data can be achieved by normally parsing all known fields in the current compiler and discarding the unknown data at the end.
[0102] In one embodiment, the deserialization method further includes: step S31, when the current version is greater than the highest version corresponding to the field set in the serialization result, deserializing all serialized data in the serialization result;
[0103] S32, adding newly added fields of each version from the highest version to the current version to the end of the obtained deserialized data, and setting the newly added fields to null values to obtain structured data of the current version.
[0104] The new version of the compiler needs to correctly deserialize the existing fields in the serialized data corresponding to the old version of the structure, and also needs to set the newly added fields that do not exist in the old version of the data to null values. Since the layout of all newly added fields in the serialized data is behind the old fields, the old fields of the old and new versions of the data are arranged the same in the entire serialized data, and are all located in front of the serialized data corresponding to the newly added fields. Therefore, the new version of the program can parse the old fields in the old version of the data normally according to the format of the new version of the data. When all the old fields are read, the compiler starts to read the new version of the fields. When the compiler has read and parsed all the serialized data, the compiler needs to set the remaining newly added fields that have not yet been read to null values to achieve backward compatibility of the data.
[0105] The above technical solution of the present application has the following beneficial technical effects:
[0106] 1) During serialization, there is no need to store field numbers or related information in the generated serialized data, which reduces data redundancy of the serialized data.
[0107] 2) When deserializing data, there is no need to read and analyze the field number and other related information of the field, which can speed up the deserialization operation of the data.
[0108] 3) Through version numbers and compile-time reflection, the location of newly added fields in the serialized data is always located after the old fields, which can achieve forward or backward compatibility when the serialization protocol is upgraded.
[0109] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should know that the present invention is not limited by the described action sequence, because according to the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.
[0110] According to another aspect of the embodiment of the present application, a serialization device for implementing the above serialization method is also provided. Fig.12 As shown, the device comprises:
[0111] The sorting unit 1202 is used to sort the fields in the structured data according to the version numbers corresponding to the fields, and obtain the data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment;
[0112] The generating unit 1204 is used to generate a serialization result of the structured data based on the data to be serialized.
[0113] In an embodiment of the present application, the above-mentioned fields are sorted according to the version numbers corresponding to the fields in the structured data to obtain the data to be serialized corresponding to the above-mentioned structured data; the above-mentioned structured data includes all fields from the creation of the above-mentioned structured data to the current moment, and the above-mentioned version number is used to characterize the order in which the above-mentioned structured data is modified; based on the above-mentioned data to be serialized, a method for generating a serialization result of the above-mentioned structured data; because when serializing data, the present application first sorts the fields in the structured data according to the version number, after the sorted data is serialized, the serialized data required by the compiler of different versions can be accurately obtained, and there is no need to add the field numbers of the fields of the structured data to the serialized data; therefore, the present application can not only reduce data redundancy, but also improve the efficiency of serialization and deserialization operations, thereby solving the technical problem of low efficiency of serialization and deserialization operations in related technologies.
[0114] In one or more embodiments, the serialization device further includes:
[0115] A first configuration unit, configured to configure an initial version number for each field in the structured data in the process of creating the structured data;
[0116] The second configuration unit is used to configure corresponding version numbers for existing fields in the structured data during the process of modifying the structured data.
[0117] In one or more embodiments, the sorting unit 1202 includes:
[0118] A determination module, used to determine the sequence of version numbers corresponding to the fields in the above structured data;
[0119] The sorting module is used to sort the above fields in the order of the version numbers corresponding to the above fields to obtain the data to be serialized corresponding to the above structured data.
[0120] In one or more embodiments, the above-mentioned sorting module includes:
[0121] an arranging subunit, for arranging the fields with the same version number in the structured data to form a field set corresponding to the same version number based on the relative positions of the fields with the same version number in the structured data;
[0122] The sorting subunit is used to sort the above-mentioned field sets based on the sequence of version numbers corresponding to the above-mentioned field sets, so as to obtain the data to be serialized corresponding to the above-mentioned structured data.
[0123] In one or more embodiments, the structured data includes a structure, and the second configuration unit further includes:
[0124] A configuration module is used to add a new field to a target position in the structure and configure a corresponding version number for the new field during the process of modifying the structured data; wherein the target position includes any position in the structure.
[0125] According to another aspect of the embodiment of the present application, a serialization device for implementing the above deserialization method is also provided. Fig.13 As shown, the device comprises:
[0126] An acquiring unit 1302 is used to acquire a serialization result to be deserialized, where the serialization result is obtained by any serialization method in the above embodiments;
[0127] The deserialization unit 1304 is used to perform deserialization processing on the serialization result based on the current version of the target program to obtain a structure matching the current version.
[0128] In one or more embodiments, it is characterized in that the deserialization unit 1304 includes:
[0129] A first processing module is used to identify serialized data in the serialization result whose version is less than or equal to the current version, and discard serialized data whose version is greater than the current version, when the current version is less than the highest version corresponding to the field set in the serialization result;
[0130] The second processing module is used to deserialize the serialized data whose version is less than or equal to the current version to obtain the structured data corresponding to the current version.
[0131] In one or more embodiments, the deserialization unit 1304 includes:
[0132] A third processing module is used to deserialize each serialized data in the serialization result to obtain deserialized data when the current version is greater than the highest version corresponding to the field set in the serialization result;
[0133] The fourth processing module is used to add the newly added fields of each version from the above-mentioned highest version to the above-mentioned current version at the end of the deserialized data, and set the above-mentioned newly added fields to null values to obtain the structured data of the above-mentioned current version.
[0134] According to another aspect of the embodiment of the present application, an electronic device for implementing the above serialization method is also provided. The electronic device may be Figure 1 The terminal device or server shown in the figure. This embodiment is described by taking the electronic device as a server as an example. Fig.14 As shown, the electronic device includes a memory 1402 and a processor 1404. The memory 1402 stores a computer program, and the processor 1404 is configured to execute the steps in any of the above method embodiments through the computer program.
[0135] Optionally, in this embodiment, the electronic device may be at least one network device among a plurality of network devices of a computer network.
[0136] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0137] S11, sorting the fields in the structured data according to the version numbers corresponding to the fields, to obtain data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment, and the version numbers are used to indicate the order in which the structured data is modified;
[0138] S12: Generate a serialization result of the structured data based on the data to be serialized.
[0139] Among them, the memory 1402 can be used to store software programs and modules, such as the program instructions / modules corresponding to the serialization method and device in the embodiment of the present application, and the processor 1404 executes various functional applications and data processing by running the software programs and modules stored in the memory 1402, that is, realizing the above-mentioned serialization method. The memory 1402 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 1402 may further include a memory remotely arranged relative to the processor 1404, and these remote memories may be connected to the terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof. Among them, the memory 1402 can be specifically used, but not limited to, for storing data to be serialized, and serialization results.
[0140] As an example, Fig.14 As shown, the memory 1402 may include, but is not limited to, the sorting unit 1202 and the generating unit 1204 in the serialization device. In addition, it may also include, but is not limited to, other module units in the serialization device, which will not be described in detail in this example.
[0141] Optionally, the transmission device 1406 is used to receive or send data via a network. Specific examples of the above-mentioned network may include a wired network and a wireless network. In one example, the transmission device 1406 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers via a network cable so as to communicate with the Internet or a local area network. In one example, the transmission device 1406 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0142] In addition, the electronic device mentioned above further includes: a connection bus 1408, which is used to connect various module components in the electronic device mentioned above.
[0143] According to another aspect of the embodiment of the present application, an electronic device for implementing the above deserialization method is also provided. The electronic device may be Figure 1 The terminal device or server shown in the figure. This embodiment is described by taking the electronic device as a server as an example. Fig.15 As shown, the electronic device includes a memory 1502 and a processor 1504. The memory 1502 stores a computer program, and the processor 1504 is configured to execute the steps in any of the above method embodiments through the computer program.
[0144] Optionally, in this embodiment, the electronic device may be at least one network device among a plurality of network devices of a computer network.
[0145] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0146] S21, obtaining a serialization result to be deserialized, where the serialization result is obtained by the serialization method of the above embodiment;
[0147] S22, based on the current version of the compiler, deserialize the serialization result to obtain structured data corresponding to the current version.
[0148] Among them, the memory 1502 can be used to store software programs and modules, such as the program instructions / modules corresponding to the serialization method and device in the embodiment of the present application, and the processor 1504 executes various functional applications and data processing by running the software programs and modules stored in the memory 1502, that is, realizing the above-mentioned serialization method. The memory 1502 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 1502 may further include a memory remotely arranged relative to the processor 1504, and these remote memories may be connected to the terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof. Among them, the memory 1502 can be specifically used, but not limited to, for storing structured data.
[0149] As an example, Fig.15 As shown, the memory 1502 may include, but is not limited to, the acquisition unit 1302 and the deserialization unit 1304 in the serialization device. In addition, it may also include, but is not limited to, other module units in the serialization device, which will not be described in detail in this example.
[0150] Optionally, the transmission device 1506 is used to receive or send data via a network. Specific examples of the above-mentioned network may include a wired network and a wireless network. In one example, the transmission device 1506 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers via a network cable so as to communicate with the Internet or a local area network. In one example, the transmission device 1506 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0151] In addition, the electronic device mentioned above further includes: a connection bus 1508, which is used to connect various module components in the electronic device mentioned above.
[0152] In other embodiments, the electronic device may be a node in a distributed system, wherein the distributed system may be a blockchain system, and the blockchain system may be a distributed system formed by connecting the multiple nodes in the form of network communication. Among them, the nodes may form a peer-to-peer (P2P) network, and any form of computing device, such as a server, terminal, or other electronic device, may become a node in the blockchain system by joining the peer-to-peer network.
[0153] In one or more embodiments, the present application also provides a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs the above-mentioned serialization method. Wherein, the computer program is configured to execute the steps of any of the above-mentioned method embodiments when running.
[0154] Optionally, in this embodiment, the computer-readable storage medium may be configured to store a computer program for performing the following steps:
[0155] S11, sorting the fields in the structured data according to the version numbers corresponding to the fields, to obtain data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment, and the version numbers are used to represent the order in which the structured data is modified;
[0156] S12, based on the data to be serialized, generating a serialization result of the structured data. Or a computer program for executing the following steps:
[0157] S21, obtaining a serialization result to be deserialized, wherein the serialization result is obtained by the serialization method of the above embodiment;
[0158] S22, based on the current version of the compiler, deserialize the serialization result to obtain structured data corresponding to the current version.
[0159] Optionally, in this embodiment, a person of ordinary skill in the art may understand that all or part of the steps in the various methods of the above embodiments may be completed by instructing hardware related to the terminal device through a program, and the program may be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0160] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0161] If the integrated units in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above computer-readable storage medium. Based on such understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling one or more computer devices (which can be personal computers, servers or network devices, etc.) to perform all or part of the steps of the methods of various embodiments of the present invention.
[0162] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0163] In the several embodiments provided in the present application, it should be understood that the disclosed client can be implemented in other ways. Among them, the device embodiments described above are only schematic, for example, the division of units is only a logical function division, and there may be other division methods in actual implementation, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0164] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0165] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0166] The above are only preferred embodiments of the present invention. It should be pointed out that, for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
[0167] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
Claims
1. A serialization method, characterized in that: The method comprises: Sort the fields in the structured data according to the version numbers corresponding to the fields, and obtain the data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment; Based on the data to be serialized, a serialization result of the structured data is generated.
2. The method according to claim 1, characterized in that Before sorting the fields in the structured data according to the version numbers corresponding to the fields, the method further includes: In the process of creating the structured data, configuring an initial version number for each field in the structured data; In the process of modifying the structured data, corresponding version numbers are configured for existing fields in the structured data.
3. The method according to claim 1 or 2, characterized in that: The step of sorting the fields in the structured data according to the version numbers corresponding to the fields to obtain the data to be serialized corresponding to the structured data includes: Determining the order of version numbers corresponding to the fields in the structured data; The fields are sorted in the order of the version numbers corresponding to the fields to obtain the data to be serialized corresponding to the structured data.
4. The method according to claim 3, characterized in that The step of sorting the fields according to the order of the version numbers corresponding to the fields to obtain the data to be serialized corresponding to the structured data includes: For the fields with the same version number in the structured data, based on the relative positions of the fields with the same version number in the structured data, the fields with the same version number are arranged to form a field set corresponding to the same version number; Based on the sequence of version numbers corresponding to the field sets, the field sets are sorted to obtain the data to be serialized corresponding to the structured data.
5. The method according to claim 2, characterized in that: The structured data includes a structure, and in the process of modifying the structured data, configuring a corresponding version number for an existing field in the structured data includes: In the process of modifying the structured data, a new field is added to a target position in the structure, and a corresponding version number is configured for the new field; wherein the target position includes any position in the structure.
6. A deserialization method, characterized in that: The method comprises: Obtaining a serialization result to be deserialized, wherein the serialization result is obtained by the method according to any one of claims 1 to 5; Based on the current version of the compiler, the serialization result is deserialized to obtain structured data corresponding to the current version.
7. The method according to claim 6, characterized in that The deserialization process is performed on the serialization result based on the current version of the compiler to obtain structured data corresponding to the current version, including: In the case where the current version is smaller than the highest version corresponding to the field set in the serialization result, identifying serialized data in the serialization result whose version is smaller than or equal to the current version, and discarding serialized data whose version is larger than the current version; Deserialize the serialized data whose version is less than or equal to the current version to obtain structured data corresponding to the current version.
8. The method according to claim 6, characterized in that The deserialization process is performed on the serialization result based on the current version of the target program to obtain structured data corresponding to the current version, including: In the case where the current version is greater than the highest version corresponding to the field set in the serialization result, deserializing each serialized data in the serialization result to obtain deserialized data; Add new fields of each version from the highest version to the current version to the end of the deserialized data, and set the new fields to null values to obtain structured data of the current version.
9. A serialization device, characterized in that: The device comprises: A sorting unit, used to sort the fields in the structured data according to the version numbers corresponding to the fields, to obtain the data to be serialized corresponding to the structured data; the structured data includes the fields from the time when the structured data is created to the current moment; A generating unit is used to generate a serialization result of the structured data based on the data to be serialized.
10. A deserialization device, characterized in that: The device comprises: An acquisition unit, used for acquiring a serialization result to be deserialized, wherein the serialization result is obtained by the method according to any one of claims 1 to 5; The deserialization unit is used to perform deserialization processing on the serialization result based on the current version of the compiler to obtain structured data corresponding to the current version.
11. An electronic device, characterized in that: The method comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method according to any one of claims 1 to 8.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: The program is executed by a processor to implement the method according to any one of claims 1 to 8.