Serialization method and deserialization method of Java data and electronic equipment
By only converting class name position coordinates and class pointer data during the serialization and deserialization of Java data, the problem of low CPU performance in the prior art is solved, and more efficient data processing and lower CPU consumption are achieved.
Patent Information
- Application Number
- CN202510367166.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-26
- Publication Date
- 2025-06-17
AI Technical Summary
During the process of serialization and deserialization of Java data, a large number of data type conversion is required, resulting in the CPU consuming a large amount of computing resources and low performance.
By storing the initial object data according to the object memory layout planned by the JVM, and only converting the class name position coordinates and class pointer data during the serialization and deserialization process, the amount of converted data is reduced.
It effectively reduces the space occupancy of data packets after serialization and deserialization, and reduces the computing resources consumed by CPU, improving CPU performance.
Smart Images

Figure CN120162084A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing. Specifically, it relates to a serialization method, a deserialization method, and an electronic device for Java data. Background Art
[0002] During the data transmission process, there are serialization and deserialization processes for Java data. Among them, the serialization process refers to the process of converting an object into a byte stream, which can save the state of the object to a file, send it to a network, or store it in a database for easy transmission or storage. The deserialization process refers to the process of converting the serialized byte stream back into the original object.
[0003] In the related art, when serializing or deserializing Java data, data type conversions are required. For example, serializing a data object into a json data format; or serializing a data object into a protostuff binary data format. This will cause the CPU (Central Processing Unit) to consume a large amount of computing resources, resulting in low CPU performance. Summary of the Invention
[0004] The purpose of the embodiments of this application is to provide a serialization method, a deserialization method, and an electronic device for Java data to improve the performance of the CPU.
[0005] In a first aspect, the embodiments of this application provide a serialization method for Java data. The method includes: storing the initial object data to be serialized in an initial data payload; the initial object data is the object memory data planned by the JVM; for each piece of the initial object data stored in the initial data payload, storing the class name of the object corresponding to the initial object data in a class name constant pool; for each piece of the initial object data stored in the initial data payload, replacing the class pointer data in the object header of the initial object data with the class name position coordinate of the class name of the initial object data in the class name constant pool to obtain a target data payload; storing the class name constant pool and the target data payload according to a preset structure to obtain a serialized target data packet. In this way, according to the object memory layout planned by the JVM for the initial object data, by virtue of the structural feature of high space utilization rate of object data in the object memory by the JVM, the space occupancy rate of the serialized data packet can be reduced. And during the serialization process of Java data, only the conversion between the class name position coordinate and the class pointer data is performed, and the amount of data to be converted is small, so that the CPU can achieve serialization by consuming less computing resources, and thus the performance of the CPU can be improved.
[0006] Optionally, if the initial object data includes first initial object data and at least one second initial object data; and the first initial object data includes a pointer to the second initial object data; then before obtaining the target data payload, the method further includes: respectively determining the data position coordinates of the first initial object data and the second initial object data in the initial data payload; in the initial data payload, replacing the pointer in the first initial object data that points to the second initial object data with the data position coordinates of the second initial object data in the initial data payload. In this way, for the case where there is pointer type data in the initial object data, the pointer type data can be replaced with the data position coordinates of the corresponding second initial object data in the initial data payload. In this way, the amount of data to be converted is further reduced, thereby further improving the performance of the CPU.
[0007] Optionally, the storing the class name constant pool and the target data payload in accordance with a preset structure to obtain a serialized target data packet includes: storing the total length of the target data packet, the number of class names of the objects in the target data packet, and the class name constant pool in a data header structure to obtain a target data header; integrating the target data header and the target data payload to obtain the target data packet. Here, a preset structure of the data packet is provided to facilitate the packaging and transmission of Java data.
[0008] Optionally, before storing the total length of the target data packet, the number of class names of the objects in the target data packet, and the class name constant pool in the data header structure to obtain a target data header, the method further includes: determining, by using an object memory calculation tool, the occupied space of the initial object data in memory to obtain the length of the initial data payload; determining the length of the class name constant pool and the length of the number of class names; determining the sum of the length of the initial data payload, the length of the class name constant pool, and the length of the number of class names as the total length of the target data packet. In this way, the total length of the target data packet can be determined more accurately. In addition, determining the lengths of each part in the target data packet one by one can reduce the risk of missing data, thereby improving the accuracy of the target data packet.
[0009] Optionally, for each of the initial object data stored in the initial data payload, storing the class name of the object corresponding to the initial object data in the class name constant pool includes: for each of the initial object data stored in the initial data payload, removing duplicates from the class name of the object corresponding to the initial object data, and storing the de-duplicated class name in the class name constant pool. In this way, for each initial object data, only a unique class name is stored in the class name constant pool, and the class name has a unique position coordinate, so that it is convenient to accurately determine the other one based on one of the class name and the position coordinate later.
[0010] In a second aspect, an embodiment of the present application provides a method for deserializing Java data, which includes: parsing a class name constant pool and a target data payload from a serialized target data packet; the class name constant pool includes the class names of the objects in the target data packet; the target data payload includes the class name position coordinates of the class names in the class name constant pool; obtaining the corresponding class name from the class name constant pool according to the class name position coordinates, and obtaining the class information corresponding to the class name according to the Java reflection mechanism; according to the class information, parsing the target object data and the class pointer data in the object header of the target object data from the target data payload; replacing the class name position coordinates with the class pointer data to obtain an initial data payload; the initial data payload includes the initial object data before serialization. In this way, during the deserialization process of Java data, only the conversion between the class name position coordinates and the class pointer data is performed, and the amount of data converted is small, so that the CPU can achieve deserialization by consuming less computing resources. Therefore, the performance of the CPU can be improved.
[0011] In addition, the initial object data follows the object memory layout planned by the JVM. Therefore, relying on the structural feature of the high space utilization rate of object data in the object memory by the JVM, the space occupancy rate of the deserialized data packet can be reduced.
[0012] In addition, the target data packet stores class names, and the class represented by the class name can be obtained through the Java reflection mechanism, so that the class pointer data of the object corresponding to the class can be obtained, and thus the initial data payload can be obtained. Therefore, not only can non-generic data deserialization be achieved, but also generic data deserialization can be achieved.
[0013] Optionally, the parsing the target object data from the target data payload according to the class information includes: if the target data payload further includes data position coordinates, then for each data position coordinate, replacing the data position coordinate with the target object data located at the data position coordinate in the target data payload.
[0014] Optionally, the target data packet further includes the total length of the target data packet and the number of class names of the object; and, parsing the target object data from the target data payload according to the class information includes: obtaining the length of the target data payload according to the total length, the length of the number of class names, and the length of the class name constant pool; calculating the length of the object instance corresponding to the currently obtained class information; and splitting out the object data corresponding to the object instance from the target data payload according to the length of the object instance to obtain the target object data. In this way, when a class information is obtained, the length of the object instance corresponding to the class information is calculated, and then the corresponding target object data is split out. In this way, the target object data is split out one by one, which improves the accuracy of the target object data to a certain extent.
[0015] Optionally, the method further includes: pointing the payload address of the initial data payload to the corresponding root object so that the data can be used normally.
[0016] In a third aspect, an embodiment of the present application provides an electronic device, including a processor and a memory, where the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the steps in the method provided in the first aspect above are run.
[0017] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps in the method provided in the first aspect above are run.
[0018] In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a computer program or instruction, and when the computer program or instruction is executed by a processor, the method described in the first aspect is run.
[0019] Other features and advantages of the present application will be described in the subsequent description, and, in part, will become apparent from the description or will be understood by implementing the embodiments of the present application. The objectives and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. Description of the Drawings
[0020] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings required to be used in the embodiments of the present application. It should be understood that the following drawings only show some embodiments of the present application, and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, other related drawings can be obtained based on these drawings without creative efforts.
[0021] Figure 1Flowchart of a method for serializing Java data provided by an embodiment of this application;
[0022] Figure 2 Schematic diagram of the object header of an initial object data provided by an embodiment of this application;
[0023] Figure 3 Schematic diagram of the object header of the initial object data after serialization provided by an embodiment of this application;
[0024] Figure 4 Packet structure diagram provided by an embodiment of this application;
[0025] Figure 5 Object data relationship diagram provided by an embodiment of this application;
[0026] Figure 6 For an embodiment of this application Figure 5 Schematic diagram of the memory occupancy of the object data shown;
[0027] Figure 7 For an embodiment of this application Figure 5 Schematic diagram of the process of storing the initial object data for the object data relationship shown;
[0028] Figure 8 For an embodiment of this application Figure 5 Schematic diagram of the process of replacing the class name position coordinates for the object data relationship shown;
[0029] Figure 9 For an embodiment of this application Figure 5 Schematic diagram of the process of replacing the data position coordinates for the object data relationship shown;
[0030] Figure 10 For an embodiment of this application Figure 5 Target packet structure diagram corresponding to the object data relationship shown;
[0031] Figure 11 Flowchart of a method for deserializing Java data provided by an embodiment of this application;
[0032] Figure 12 Schematic diagram of the process of obtaining a target packet provided by an embodiment of this application;
[0033] Figure 13 Schematic diagram of the process of obtaining a Java.lang.Class object provided by an embodiment of this application;
[0034] Figure 14 Schematic diagram of the process of splitting target object data provided by an embodiment of this application;
[0035] Figure 15 It is a schematic diagram of a process for replacing the class name position coordinates with the class pointer data provided by an embodiment of the present application;
[0036] Figure 16 It is a schematic diagram of a process for replacing the data position coordinates with the corresponding pointer data provided by an embodiment of the present application;
[0037] Figure 17 It is a schematic diagram of the structure of an electronic device for executing the serialization method or deserialization method of Java data provided by an embodiment of the present application. Detailed implementation manners
[0038] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Usually, the components of the embodiments of the present application described and shown in the accompanying drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the present application to be protected, but only represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative efforts belong to the scope of protection of the present application.
[0039] It should be noted that similar reference numerals and letters denote similar items in the following drawings. Therefore, once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. At the same time, in the description of the present application, the terms "first", "second", etc. are only used for distinguishing descriptions and cannot be understood as indicating or implying relative importance.
[0040] It should be noted that, without conflict, the embodiments in the present application or the technical features in the embodiments can be combined.
[0041] In the related art, there is a problem of low CPU performance during the serialization or deserialization of data; to solve this problem, the present application provides a serialization method and a deserialization method for Java data; further, during the serialization or deserialization process, the class pointer data of the object header is converted with its corresponding storage location information, thereby reducing the computing resources consumed by the CPU and improving the CPU performance.
[0042] It should be noted that the above serialization method and deserialization method of Java data can be implemented in a Java virtual machine (such as the Hotspot virtual machine). In addition, the serialization method and the deserialization method can be implemented in the same virtual machine or in different virtual machines. Among them, if they are implemented in different virtual machines, the pointer compression methods of different virtual machines and the configurations related to the object memory layout should be the same.
[0043] Please refer to Figure 1 , which shows a flowchart of a serialization method of Java data provided by an embodiment of the present application. As Figure 1 shown, the serialization method of the Java data includes the following steps 101 to step 104.
[0044] Step 101, storing the initial object data to be serialized into the initial data payload; the initial object data is the object memory data planned by the JVM;
[0045] The above initial object data may include, for example, application data.
[0046] The above JVM (Java Virtual Machine) can execute the bytecode generated after compiling the Java source code.
[0047] Furthermore, the object memory layout planned by the JVM is generally divided into three parts: object header (Object Header), instance data (Instance Data), and alignment padding (Padding). Among them, the object header includes a Mark Word (used to store the data during the operation of the object) and a metadata pointer (a pointer for the object to point to its class metadata, and the JVM can determine which class instance the object is through this pointer).
[0048] Therefore, the initial object data also has the above three parts, and each part stores data corresponding to the current object.
[0049] In some application scenarios, the JVM can recursively initialize the root object tree where the object data is located, and then store each data into the initial data payload according to the data hierarchy relationship represented by the root object tree.
[0050] It should be noted that in the related art, when serializing based on protocols such as the json protocol and the protostuff protocol, there is a problem of high space occupancy. Specifically, when serializing based on the json protocol, it is necessary to serialize the data object into the json data format, and the json format data has a large number of redundant characters, so it will occupy storage space. When serializing based on the protostuff protocol, it is necessary to serialize the object into the protostuff binary data format, and in order to achieve cross-platform transmission or storage, some metadata may be stored in the serialized data, so it will also occupy storage space.
[0051] In the present application, according to the object memory layout planned by the JVM for the initial object data, the high space utilization rate of the object data in the object memory by the JVM can be utilized to reduce the space occupancy rate of the serialized data packet.
[0052] Step 102: For each of the initial object data stored in the initial data payload, store the class name of the object corresponding to the initial object data into the class name constant pool; in this way, the class name constant pool includes the class names of multiple objects, and each class name corresponds to its own position coordinate. The above position coordinate can be, for example, an integer offset.
[0053] In some optional implementation manners, for each of the initial object data stored in the initial data payload, the class name of the object corresponding to the initial object data can be de-duplicated, and the de-duplicated class name is stored into the class name constant pool. In this way, for each initial object data, only a unique class name is stored in the class name constant pool, and the class name has a unique position coordinate, so as to accurately determine the other one according to one of the class name and the position coordinate subsequently. It should be noted that the class names in the class name constant pool can be stored in the form of strings.
[0054] Step 103: For each of the initial object data stored in the initial data payload, replace the class pointer data in the object header of the initial object data with the class name position coordinate of the class name of the initial object data in the class name constant pool to obtain the target data payload;
[0055] The above class pointer data is used to represent the starting address of the memory space where the loaded class is located.
[0056] Please refer to Figure 2 , which shows the object header of an initial object data provided by an embodiment of the present application. As Figure 2As shown, if the class pointer data of the object is "class pointer of com.example.Class_A", and the class name position coordinate of the class name "com.example.Class_A" of the object in the class name constant pool is 0, then replace the "class pointer of com.example.Class_A" in the object header with "0" to obtain as Figure 3 the object header shown.
[0057] Step 104: Store the class name constant pool and the target data payload according to a preset structure to obtain a serialized target data packet.
[0058] In this implementation, during the serialization process of Java data, only the conversion between the class name position coordinate and the class pointer data is performed, and the amount of data to be converted is small, so that the CPU can consume less computing resources to achieve serialization. Therefore, the performance of the CPU can be improved.
[0059] In some alternative implementations, if the initial object data includes first initial object data and at least one second initial object data; the first initial object data includes a pointer to the second initial object data; then before obtaining the target data payload, the method further includes:
[0060] First, respectively determine the data position coordinates of the first initial object data and the second initial object data in the initial data payload;
[0061] It should be noted that the initial data payload can be regarded as a data queue. After storing each initial object data in the initial data payload, each initial object data will have a corresponding position in the initial data payload. The above data position coordinates are used to represent this position.
[0062] Then, in the initial data payload, replace the pointer in the first initial object data that points to the second initial object data with the data position coordinate of the second initial object data in the initial data payload. At this time, the target data payload also includes the class name position coordinate and the data position coordinate.
[0063] In this implementation, for the case where there is pointer type data in the initial object data, the pointer type data can be replaced with the data position coordinate of the corresponding second initial object data in the initial data payload. In this way, the amount of data to be converted is further reduced, thereby further improving the performance of the CPU.
[0064] It should be noted that if the first initial object data does not include pointer type data, then no replacement process needs to be performed on each data.
[0065] In some alternative implementation manners, storing the class name constant pool and the target data payload according to the preset structure in step 104 to obtain the serialized target data packet includes:
[0066] Sub-step 1041: Store the total length of the target data packet, the number of class names of the objects in the target data packet, and the class name constant pool in the data packet header structure to obtain the target data packet header;
[0067] In some alternative implementation manners, the total length of the target data packet can be determined through the following steps:
[0068] Step 1: Use an object memory calculation tool to determine the occupied space of the initial object data in memory to obtain the length of the initial data payload;
[0069] The above object memory calculation tool can be, for example, the ehcacheSizeOf class. Subsequently, the JVM can use this tool to determine the occupied space of each initial object data in memory. Then, the total occupied space of all initial object data in memory can be determined as the length of the initial data payload.
[0070] Step 2: Determine the length of the class name constant pool and the length of the number of class names;
[0071] In some application scenarios, the class name constant pool is used to store each class name. Adjacent class names can be separated by a delimiter. Each class name is represented by at least one character, and the space occupied by each character can be preset. Each time the JVM parses a delimiter, it can determine that a class name is parsed, and then the corresponding string of each class name can be determined. For example, if the space occupied by a character is 2 bytes and a class name includes 3 characters, the space occupied by this class name is 6 bytes at this time. By analogy, the space occupied by multiple class names stored in the class name constant pool can be determined. Further, the space occupied by the delimiter can also be preset. Therefore, the length of the class name constant pool can be regarded as the sum of the space occupied by multiple class names and the space occupied by the delimiter.
[0072] The length of the above number of class names, that is, the space occupied by storing the number of class names. In some application scenarios, for example, this length can be set to a fixed value, such as 4 bytes.
[0073] Step 3: Determine the sum of the length of the initial data payload, the length of the class name constant pool, and the length of the number of class names as the total length of the target data packet.
[0074] In this implementation mode, the total length of the target data packet can be determined more accurately. In addition, by determining the lengths of the respective parts in the target data packet one by one, the risk of missing data can be reduced, thereby improving the accuracy of the target data packet.
[0075] Further, please refer to Figure 4 , which shows a data packet structure provided by an embodiment of the present application. As Figure 4 shown, the data packet structure includes a data packet header and a data payload. Among them, the data packet header is used to store the total length of the data packet, the number of class names, and the class name constant pool. The class name constant pool is used to store each class name, and adjacent class names are separated by a delimiter \r\n. Here, it is preset that the total length of the data packet occupies 8 bytes, and the number of class names occupies 4 bytes. In addition, the data payload is used to store object data.
[0076] Subsequently, the JVM can store the total length of the above target data packet, the number of class names of the objects in the target data packet, and the class name constant pool in the corresponding structures of the data packet header respectively, to obtain the target data packet header.
[0077] Sub-step 1042, integrate the target data packet header and the target data payload to obtain the target data packet. That is, pack the target data packet header and the target data payload into the target data packet.
[0078] For ease of understanding, please refer to Figures 5 to 10 . First, as Figure 5 shown, if the initial object data includes the first initial object data Data-A, the second initial object data Data-B, the second initial object data Data-C, and the second initial object data Data-D. Among them, Data-A includes a pointer Field-B pointing to Data-B, and Data-B includes a pointer Field-D pointing to Data-D. Data-A also includes a pointer Field-C pointing to Data-C, and Data-C includes data Field-E: long.
[0079] Please continue to refer to Figure 6 , the JVM can first calculate the memory occupation space corresponding to each initial object data through the ehcacheSizeOf class. Among them, the memory occupation space of Data-A is 24 bytes, the memory occupation space of Data-B is 16 bytes; the memory occupation space of Data-C is 24 bytes; the memory occupation space of Data-D is 16 bytes. At this time, the length of the initial data payload can be obtained as 80 bytes.
[0080] Please continue to refer to Figure 7 , the JVM can determine Figure 5The root object tree corresponding to the shown data relationship. Then, according to the data hierarchical relationship represented by the root object tree, each piece of data is stored into the initial data payload. At this time, the JVM can record the data position coordinates of each initial object data in the initial data payload. For example, the data position coordinate of Data-A is 0, the data position coordinate of Data-B is 1, the data position coordinate of Data-D is 2, and the data position coordinate of Data-C is 3.
[0081] Please continue to refer to Figure 8 , the JVM can obtain the class name of the object corresponding to the current initial object data, and then save it to the class name constant pool after deduplication. Each class name is separated by \r\n. At this time, the class name position coordinates of each class name in the class name constant pool, and the number of class names 4 can be obtained. That is, the class name position coordinate corresponding to the class name "com.example.Class_A" is 0; the class name position coordinate corresponding to the class name "com.example.Class_B" is 1; the class name position coordinate corresponding to the class name "com.example.Class_D" is 2; the class name position coordinate corresponding to the class name "com.example.Class_C" is 3.
[0082] Then, the JVM can replace the class pointer data in the object header of the current initial object data with the class name position coordinate corresponding to the corresponding class name. For example, replace the "com.example.Class_A class pointer" in the object header of Data-A with "0".
[0083] Then, please continue to refer to Figure 9 , for the pointer type data Field-B, Field-C, Field-D, Field-B can be replaced with the data position coordinate 1 of the corresponding Data-B in the initial data payload, Field-C can be replaced with the data position coordinate 3 of the corresponding Data-C in the initial data payload; Field-D can be replaced with the data position coordinate 2 of the corresponding Data-D in the initial data payload.
[0084] Then, the JVM can fill each piece of data into the corresponding structure of the data packet, thereby obtaining the target data packet. For example, in Figure 10 , the total length of the target data packet is 252, and the number of class names is 4. Among them, the number of class names occupies a preset length of 4 bytes, the length of the class name constant pool is 168 bytes, and the length of the target data payload is 80 bytes. Then, the JVM can store the target data packet (for example, store it in a disk file) or transmit it (for example, transmit it through the network). Thus, the serialization ends.
[0085] Based on the same inventive concept, this application also provides a method for deserializing Java data. As Figure 11 shown, this method includes:
[0086] Step 1101, parse the class name constant pool and the target data payload from the serialized target data packet; the class name constant pool includes the class names of the objects in the target data packet; the target data payload includes the class name position coordinates of the class names in the class name constant pool;
[0087] Step 1102, obtain the corresponding class name from the class name constant pool according to the class name position coordinates, and obtain the class information corresponding to the class name according to the Java reflection mechanism;
[0088] The above class information can be, for example, a Java.lang.Class object. It should be noted that the Java.lang.Class object is the class that describes classes in Java. It specifically describes the name of the class, the superclass, the implemented interfaces, fields, methods, constructors, etc., and stores a pointer to the loaded class. Further, the Java.lang.Class object is the core of the reflection mechanism. Therefore, the Java.lang.Class object corresponding to the class name can be obtained through the Java reflection mechanism.
[0089] Step 1103, according to the class information, parse the target object data and the class pointer data in the object header of the target object data from the target data payload;
[0090] For example, the JVM can calculate the size of the object instance using the Java.lang.Class object, and then, according to the size of the object instance, split the corresponding object data from the target data payload, and determine the split object data as the target object data corresponding to the object instance.
[0091] And since the Java.lang.Class object stores a pointer to the loaded class, the class pointer data in the object header of the target object data can be obtained.
[0092] Step 1104, replace the class name position coordinates with the class pointer data to obtain the initial data payload; the initial data payload includes the initial object data before serialization.
[0093] It should be noted that the above steps 1101 to 1104 are the deserialization process of Java data, which is the reverse process of the serialization process described in the above steps 101 to 104.
[0094] In this implementation, during the deserialization process of Java data, only the conversion between the position coordinates of the class name and the class pointer data is performed, and the amount of data to be converted is small, enabling the CPU to consume less computing resources to achieve deserialization. Therefore, the performance of the CPU can be improved.
[0095] In addition, the initial object data follows the object memory layout planned by the JVM. Therefore, leveraging the structural feature of the high space utilization rate of object data in the object memory by the JVM, the space occupancy rate of the data packet after deserialization can be reduced.
[0096] Furthermore, in the related art, since the generic information (information related to type parameters) of data is erased during Java runtime, and the data transmitted based on protocols such as the json protocol and the protostuff protocol does not carry any class-related information. Therefore, when transmitting generic data through the above-mentioned protocols, deserialization cannot be achieved. For example, when using generic type parameters in Java, the declared type is a parent class, but a specific subclass instance of the parent class is actually used during actual runtime. In this way, the declared parent class cannot be parsed, and thus deserialization cannot be achieved.
[0097] In this implementation, the class name is stored in the target data packet, and through the Java reflection mechanism, the class represented by the class name can be obtained, thereby obtaining the class pointer data of the object corresponding to the class, and thus obtaining the initial data payload. That is to say, this implementation can not only achieve the deserialization of non-generic data but also the deserialization of generic data.
[0098] In some alternative implementations, the parsing of the target object data from the target data payload as described in step 1103 above includes: if the target data payload further includes data position coordinates, then for each data position coordinate, replace the data position coordinate with the target object data located at the data position coordinate in the target data payload.
[0099] Here, if there are data position coordinates in the target data payload, it indicates that the initial object data includes the first initial object data and at least one second initial object data; and the first initial object data includes a pointer to the second initial object data. Therefore, the data position coordinates can be restored to the corresponding target object data thereon. Thus, the target data payload can be parsed.
[0100] In some alternative implementations, the target data packet further includes the total length of the target data packet and the number of class names of the object; and the parsing of the target object data from the target data payload as described in step 1103 above includes:
[0101] First, based on the total length, the length of the number of class names, and the length of the class name constant pool, obtain the length of the target data payload;
[0102] For example, subtracting the length of the number of class names and the length of the class name constant pool from the total length can obtain the length of the target data payload.
[0103] Then, for the currently obtained class information, calculate the length of the object instance corresponding to this class information;
[0104] Here, the JVM can, for example, use the JOL tool (Java Object Layout, a tool for analyzing object layouts in the JVM that can calculate the size of a certain object at runtime) to determine the length of the object instance.
[0105] Then, based on the length of this object instance, split out the object data corresponding to this object instance from the target data payload to obtain the target object data.
[0106] In this implementation, when obtaining a class information, calculate the length of the object instance corresponding to this class information, and then split out the corresponding target object data. In this way, the target object data is split out one by one, which improves the accuracy of the target object data to a certain extent.
[0107] It should be noted that the JVM can also, after obtaining multiple class information, associate and store the class information and the acquisition order, so that after the class information acquisition is completed, split out the target object data from the target data payload one by one according to the class information and the acquisition order.
[0108] The above target data payload is essentially just a data sequence. Therefore, in order to use the data normally, the JVM can also point the payload address of the initial data payload to the corresponding root object.
[0109] The above payload address is also a pointer pointing to the starting position of the data payload. In some application scenarios, the JVM can, for example, use a cast instruction to point the payload address of the initial data payload to the corresponding root object.
[0110] Similarly, for ease of understanding, please refer to Figures 12 to 16 . First, as Figure 12 shown, after the JVM obtains the data stream from a disk file or through a network, it can read the corresponding byte stream according to the total length of the target data packet, allocate heap memory (JVM heap, used to store all object data) space, and then write the data, then the above serialized target data packet can be obtained.
[0111] Please continue to refer to Figure 13, the JVM obtains the class name position coordinates from the target data packet and retrieves the class name at that class name position coordinate from the class name constant pool. For example, through class name position coordinate 0, the class name "com.example.Class_A" at the corresponding position is obtained. Through class name position coordinate 1, the class name "com.example.Class_B" at the corresponding position is obtained. Through class name position coordinate 2, the class name "com.example.Class_D" at the corresponding position is obtained. Through class name position coordinate 3, the class name "com.example.Class_C" at the corresponding position is obtained.
[0112] Then, the JVM can obtain the corresponding Java.lang.Class object for the class name through the Java reflection mechanism. For example, the class name "com.example.Class_A" corresponds to "class: com.example.Class_A", the class name "com.example.Class_B" corresponds to "class: com.example.Class_B", the class name "com.example.Class_D" corresponds to "class: com.example.Class_D", and the class name "com.example.Class_C" corresponds to "class: com.example.Class_C".
[0113] Please continue to refer to Figure 14 , for the Java.lang.Class objects corresponding to each class name respectively, the JVM can calculate the length of each object instance, and thus can split the target object data from the target data payload. For example, using "class: com.example.Class_A" to split out the target object data DataA. Specifically, the JVM can calculate that the length of the first object instance is 24 bytes, and then can take 24 bytes of object data starting from the initial address of the target data payload, which is the target object data DataA. Here, it should be noted that the length of the target data payload can be determined by the total length of the target data packet, the length of the number of class names, and the length of the class name constant pool. Specifically, the total length of the target data packet, 252 bytes, can subtract the length of the number of class names, 4 bytes, and then subtract the length of the class name constant pool, 168 bytes, to obtain the length of the target data payload as 80 bytes. That is, starting from the initial address of the 80-byte target data payload, the first 24 bytes of data are split out, which is the above-mentioned target object data DataA.
[0114] Please continue to refer to Figure 15, JVM can obtain class pointer data through Java.lang.Class. For example, through "class:com.example.Class_A", the class pointer data corresponding to the class name "com.example.Class_A" can be obtained. Then the "0" in the object header can be replaced with the "com.example.Class_A class pointer".
[0115] Please continue reading Figure 16 If the target data payload parsed by JVM also includes data position coordinates, the data position coordinates are replaced with the target object data at the corresponding position in the target data payload. That is, data position coordinate 1 is replaced with pointer data Field-B corresponding to Data-B, data position coordinate 2 is replaced with pointer data Field-D corresponding to Data-D, and data position coordinate 3 is replaced with pointer data Field-C corresponding to Data-C.
[0116] It can be seen that the target data payload obtained at this time is essentially just a data sequence, so its payload address can be forcibly converted to the corresponding root object. At this point, the deserialization is completed.
[0117] Those skilled in the art will appreciate that, in the above method of a specific embodiment, the order in which the steps are written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of the steps should be determined by their functions and possible internal logic.
[0118] See also Figure 17 , Figure 17 A structural diagram of an electronic device for executing a serialization method or a deserialization method of Java data provided in an embodiment of the present application, the electronic device may include: at least one processor 1701, such as a CPU, at least one communication interface 1702, at least one memory 1703 and at least one communication bus 1704. Among them, the communication bus 1704 is used to realize the direct connection and communication of these components. Among them, the communication interface 1702 of the device in the embodiment of the present application is used to communicate signaling or data with other node devices. The memory 1703 can be a high-speed RAM memory or a non-volatile memory (non-volatile memory), such as at least one disk storage. The memory 1703 can also be at least one storage device located away from the aforementioned processor. Computer-readable instructions are stored in the memory 1703. When the computer-readable instructions are executed by the processor 1701, the electronic device can execute the methods provided by the above-mentioned method embodiments.
[0119] Understandably, Figure 17The structure shown is only schematic, and the electronic device may further include more or fewer components than those shown in Figure 17 or have a configuration different from that shown in Figure 17 . Figure 17 Each component shown in may be implemented by hardware, software, or a combination thereof.
[0120] An embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the methods provided in the foregoing method embodiments can be executed.
[0121] An embodiment of the present application provides a computer program product. The computer program product includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the methods provided in the foregoing method embodiments.
[0122] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For another 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 displayed or discussed coupling or direct coupling or communication connection between each other can be through some communication interfaces. The indirect coupling or communication connection of the devices or units can be in an electrical, mechanical or other form.
[0123] In addition, the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or may be distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0124] Furthermore, in each embodiment of the present application, the functional modules can be integrated together to form an independent part, or each module can exist alone, or two or more modules can be integrated to form an independent part.
[0125] In this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.
[0126] The above are only embodiments of the present application and are not intended to limit the protection scope of the present application. For those skilled in the art, various modifications and changes can be made to the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the protection scope of the present application.
Claims
1. A method for serializing Java data, characterized in that: The method includes: The initial object data to be serialized is stored in the initial data payload; the initial object data is the object memory data planned by the JVM; For each of the initial object data stored in the initial data payload, storing the class name of the object corresponding to the initial object data in a class name constant pool; For each of the initial object data stored in the initial data payload, the class pointer data in the object header of the initial object data is replaced with the class name position coordinates of the class name of the initial object data in the class name constant pool to obtain a target data payload; The class name constant pool and the target data payload are stored according to a preset structure to obtain a serialized target data packet.
2. The method according to claim 1, characterized in that If the initial object data includes first initial object data and at least one second initial object data; and the first initial object data includes a pointer to the second initial object data; then before obtaining the target data payload, the method further includes: respectively determining data position coordinates of the first initial object data and the second initial object data in the initial data payload; In the initial data payload, the pointer in the first initial object data pointing to the second initial object data is replaced with the data position coordinates of the second initial object data in the initial data payload.
3. The method according to any one of claims 1 to 2, characterized in that: The storing the class name constant pool and the target data payload according to the preset structure to obtain a serialized target data packet includes: The total length of the target data packet, the number of class names of objects in the target data packet, and the class name constant pool are stored in a data packet header structure to obtain a target data packet header; The target data packet header and the target data payload are integrated to obtain the target data packet.
4. The method according to claim 3, characterized in that Before storing the total length of the target data packet, the number of class names of objects in the target data packet, and the class name constant pool in a data packet header structure to obtain a target data packet header, the method further includes: Determine the space occupied by the initial object data in the memory by using an object memory calculation tool to obtain the length of the initial data payload; Determine the length of the class name constant pool and the length of the number of class names; The total length of the target data packet is determined as the cumulative sum of the length of the initial data payload, the length of the class name constant pool, and the length of the number of class names.
5. The method according to claim 1, characterized in that For each of the initial object data stored in the initial data payload, storing the class name of the object corresponding to the initial object data in the class name constant pool includes: For each of the initial object data stored in the initial data payload, a deduplication process is performed on the class name of the object corresponding to the initial object data, and the deduplication-processed class name is stored in a class name constant pool.
6. A method for deserializing Java data, characterized in that: include: Parse the class name constant pool and target data payload from the serialized target data packet; The class name constant pool includes the class name of the object in the target data packet; The target data payload includes the class name position coordinates of the class name in the class name constant pool; Obtaining a corresponding class name from the class name constant pool according to the class name position coordinates, and obtaining class information corresponding to the class name according to a Java reflection mechanism; According to the class information, parsing the target object data and the class pointer data in the object header of the target object data from the target data payload; The class name position coordinates are replaced with the class pointer data to obtain an initial data payload; the initial data payload includes initial object data before serialization.
7. The method according to claim 6, characterized in that The step of parsing the target object data from the target data payload according to the class information includes: If the target data payload also includes data position coordinates, then for each data position coordinate, the data position coordinate is replaced with the target object data located at the data position coordinate in the target data payload.
8. The method according to any one of claims 6-7, characterized in that: The target data packet also includes the total length of the target data packet and the number of class names of the objects; as well as The step of parsing the target object data from the target data payload according to the class information includes: Obtaining the length of the target data payload according to the total length, the length of the number of class names, and the length of the class name constant pool; For the currently acquired class information, calculate the length of the object instance corresponding to the class information; According to the length of the object instance, object data corresponding to the object instance is segmented from the target data payload to obtain the target object data.
9. The method according to claim 8, characterized in that The method further comprises: The payload address of the initial data payload is pointed to the corresponding root object.
10. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the method according to any one of claims 1 to 9 is executed.
11. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 9 is executed.
12. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed by a processor, the method according to any one of claims 1 to 9 is performed.