A simple object transmission serialization method and device
Patent Information
- Application Number
- CN202210008892.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-05
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2042-01-05
AI Technical Summary
[0004]为了解决现有框架编码出来的对象字节空间过大,结构内部有过于多余的数据体,反解析也不够干脆,导致java对象传输效率过慢的问题,本发明的目的是提供一种简约的对象传输序列化方法及装置,用于简化java对象序列化传输过程
[0004] In order to solve the problem that the byte space of the object encoded by the existing framework is too large, there are overly redundant data bodies inside the structure, and the anti-parsing is not straightforward enough, resulting in too slow Java object transmission efficiency, the purpose of the present invention is to provide a simple object transmission serialization method and device for simplifying the Java object serialization transmission process.
Smart Images

Figure CN114385146B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Java object transmission, and particularly to a simple object transmission serialization method and device. Background Art
[0002] Existing serialization frameworks with relatively high efficiency on the market, such as protobuf, thrift, fastjson, etc., including the serialization provided by the JDK. The purpose of serialization is to solve the problem of object transmission over the network. Among them, the sender will convert the object into a byte sequence before sending, and the receiver will decode the received byte sequence back into an object. This entire process is serialization and deserialization.
[0003] Since these frameworks need to consider generality, various object descriptions, version descriptions, and attribute descriptions will be added during object serialization. The advantage of this is that it is more general and can cross languages, but the disadvantage is that the byte space of the serialized object becomes larger and the transmission efficiency becomes slower. In fact, in specific application scenarios, a too general protocol is not required. Summary of the Invention
[0004] In order to solve the problem that the byte space of the object encoded by the existing framework is too large, there are overly redundant data bodies inside the structure, and the anti-parsing is not straightforward enough, resulting in too slow Java object transmission efficiency, the purpose of the present invention is to provide a simple object transmission serialization method and device for simplifying the Java object serialization transmission process.
[0005] The technical solution of the present invention provides a simple object transmission serialization method and device. The method includes the following steps:
[0006] S1: Construct a simple object serialization format, where the simple object serialization format includes: a serialization class number, a variable-length character length, and class attributes; among them, there are multiple class attributes;
[0007] S2: Meta-information extraction. According to the annotations of the serialization class, extract the meta-information of the serialization class into the memory in advance; there is a mapping table of the serialization class in the memory;
[0008] S3: Generate an object byte sequence. The network sender reflects and parses the class information according to the mapping table of the serialization class to generate an object byte sequence;
[0009] S4: Object byte sequence parsing. The network receiver parses the received object byte sequence and decodes the object byte sequence back into an object.
[0010] Furthermore, in step S4, the specific process of de-encoding the object byte sequence into the object is as follows: obtaining the serialized class number in the first byte of the object byte sequence, and obtaining the metadata of the corresponding class through the memory according to the serialized class number; according to the class attributes and metadata, parsing the class attribute data in sequence, and de-encoding the object byte sequence into the object.
[0011] Furthermore, when parsing the attribute data of a class, if variable-length attribute data of the string class is encountered, its size is obtained through metadata and byte array header data. For other basic type attribute data, the length of the basic type specified in the Java specification is followed.
[0012] A simple object transmission serialization device, comprising:
[0013] Simple object serialization format building module: The simple object serialization format includes: serialization class number, variable length character length and class attributes; wherein the class attributes include multiple;
[0014] Meta-information extraction module: extracts the metadata of the serialized class into memory in advance according to the annotation of the serialized class; the mapping table of the serialized class exists in the memory;
[0015] Object byte sequence generation module: The network sender generates the object byte sequence based on the mapping table of the serialized class and the information of the reflection parsing class;
[0016] Object byte sequence parsing module: The network receiver parses the received object byte sequence and decodes the object byte sequence into an object.
[0017] A computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the simplified object transmission serialization method are implemented.
[0018] A computer-readable storage medium stores a computer program, which implements the steps of a simplified object transmission serialization method when executed by a processor.
[0019] Compared with the prior art, the beneficial effects of the present invention include: improving the network transmission efficiency of Java objects. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 This is a flow chart of a simplified object transmission serialization method provided by the present invention. DETAILED DESCRIPTION
[0021] In order to make the objectives, technical solutions and advantages of the present invention clearer and more understandable, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0022] The present invention provides a simple object transmission serialization method and device. Please refer to Figure 1 , Figure 1 which is a flowchart of the method of the present invention; the method includes the following steps:
[0023] S1: Construct a simple object serialization format, where the simple object serialization format includes: a serialization class number, a variable-length character length, and class attributes; among which there are multiple class attributes;
[0024] As an embodiment, please refer to Table 1 below. Table 1 is the simple object serialization format.
[0025] Table 1 Simple Object Serialization Format
[0026]
[0027] In the table, fields 1, 2, and 3 indicate different attributes of the class;
[0028] In the embodiments of the present application, the "person class" is taken as an example for illustration.
[0029] According to the simple object serialization format, the "person class" is defined. For the purpose of explanation, the present application provides some code for illustrative purposes only.
[0030] "Person
[0031] @SjbClassSerial(classNo = 1) / / Identify the class number
[0032] Class Person{
[0033] @SjbFieldSerial(fieldNo = 1) / / The position in the attribute serialization, the first part, performs string UTF encoding, tentatively 3 bytes represent one character, and the string corresponds to the protocol variable length
[0034] String name;
[0035] @SjbFieldSerial(fieldNo = 2) / / The second block in the attribute serialization, 4 bytes
[0036] Int age;
[0037] @SjbFieldSerial(fieldNo = 3) / / The 3rd block of 8 bytes
[0038] Long birthday;
[0039] @SjbFieldSerial(fieldNo = 4)
[0040] String address;
[0041] @SjbFieldSerial(fieldNo = 5)
[0042] Long creatTime;
[0043] }
[0044] ”
[0045] In the person class, the first part is the class number; the second part is the variable-length character length. In this embodiment, it is tentatively assumed that 3 bytes represent one character according to UTF encoding for the string, and the string corresponds to a variable length in the protocol.
[0046] For the other parts, they correspond to the attribute values of the person class in sequence. For example, in this embodiment, the person class includes the following attributes: name, birthday, address, creatTime.
[0047] S2: Meta-information extraction. According to the annotations of the serialized class, the meta-information of the serialized class is extracted in advance into the memory; there is a mapping table of the serialized class in the memory.
[0048] As an embodiment, the mapping table of the class is shown in Table 2;
[0049] Table 2 Class mapping table
[0050] Number Class Full Path 1 Com.sjb.entiry.Person 2 Com.sjb.entiry.Car … …
[0051] In Table 2, the class with class number 1 is the person class in this embodiment.
[0052] According to the class mapping table, there is further a class-corresponding attribute lookup table in the memory, as shown in Table 3:
[0053] Table 3 Class-corresponding attribute lookup table
[0054]
[0055] In Tables 2 and 3, there is metadata stored.
[0056] S3: Generate the object byte sequence. The network sender generates the object byte sequence by reflecting and parsing the class information according to the mapping table of the serialization class.
[0057] As an example, the object byte sequence of the person class is as follows:
[0058] 1|0 6|0 27|-27 -68 -96 -28 -72 -119|0 0 0 25|0 0 1 125 -118 -89 -124-27|-26 -83 -90 -26 -79 -119 -27 -72 -126 -26 -76 -86 -27 -79 -79 -27 -116 -70 -23 -121 -111 -24 -98 -115 -26 -72 -81|0 0 0 4-99 127 27-124
[0059] This is the result after extracting the metadata. The object byte sequence is sent to the network receiver as a string of data.
[0060] S4: Object byte sequence parsing. The network receiver parses the received object byte sequence and decodes the object byte sequence into an object.
[0061] In step S4, the specific process of decoding the object byte sequence into an object is as follows: Obtain the serialization class number in the first byte of the object byte sequence, and obtain the metadata of the corresponding class through memory according to the serialization inner class number; According to the class attributes and metadata, parse out the attribute data of the class in turn, and decode the object byte sequence into an object.
[0062] When parsing the variable-length attribute data of the string class, obtain its size through the metadata and the data at the head of the byte array, and the attribute data of other basic types is read according to the length of the basic type specified by the java specification.
[0063] According to the above process, the parsing is as follows:
[0064] 1 represents the class number
[0065] 0 6 represents the length of the first string attribute name
[0066] 0 27 represents the length of the second string attribute address
[0067] -27 -68 -96 -28 -72 -119 represents the name value
[0068] 0 0 0 25 represents the age value
[0069] 0 0 1 125 -118 -89 -124 -27 represents the birthday value
[0070] -26 -83 -90 -26 -79 -119 -27 -72 -126 -26 -76 -86 -27 -79 -79 -27 -116 -70 -23 -121 -111 -24 -98 -115 -26 -72 -81 represents the address value
[0071] 0 0 0 4 -99 127 27 -124 represents createTime.
[0072] A simple object transmission serialization device, the device includes:
[0073] Simple object serialization format construction module: The simple object serialization format includes: serialization class number, variable-length character length, and class attributes; where there are multiple class attributes;
[0074] Meta-information extraction module: According to the annotation of the serialization class, extract the meta-information of the serialization class to the memory in advance; there is a mapping table of the serialization class in the memory;
[0075] Object byte sequence generation module: The network sender reflects and parses the class information according to the mapping table of the serialization class, and generates an object byte sequence;
[0076] Object byte sequence parsing module: The network receiver parses the received object byte sequence and decodes the object byte sequence into an object.
[0077] A computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, and when the processor executes the computer program, it implements the steps of the simple object transmission serialization method.
[0078] A computer-readable storage medium, the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the steps of the simple object transmission serialization method.
[0079] To better illustrate the beneficial effects of the present invention, the present invention compares two serialization methods, jdk and fastjson, and conducts tests.
[0080] Comparison of test results with 5 attributes of the Persion object
[0081] Table 4 Test results of local serialization and deserialization of a single object
[0082]
[0083]
[0084] Table 5 Local Serialization and Deserialization Test Results for 1 Million Objects
[0085]
[0086] Table 6 Local Serialization and Deserialization Test for 10 Million Objects
[0087]
[0088] Table 7 10,000 - time Network Communication Test
[0089]
[0090] Through the overall performance of the test, the simple object serialization protocol has an absolute advantage in the transmission of single - entity objects.
[0091] It should be understood that the magnitudes of the sequence numbers of the steps in the above - mentioned embodiments do not imply the sequence of execution. The execution sequence of each process should be determined by its function and internal logic, and should not constitute any limitation to the implementation process of the embodiments of this application.
[0092] The beneficial effect of the present invention is that it improves the network transmission efficiency of Java objects.
[0093] The specific implementation manners of the present invention described above do not constitute a limitation to the protection scope of the present invention. Any other corresponding changes and deformations made according to the technical concept of the present invention should be included within the protection scope of the claims of the present invention.
Claims
1. A simple object transfer serialization method, characterized in that: It includes the following steps: S1: Construct a simple object serialization format, which consists of: a serialization class number, a variable-length character length, and class attributes; where there are multiple class attributes; S2: Meta-information extraction. According to the annotations of the serialization class, extract the meta-information of the serialization class into memory in advance; There is a mapping table of the serialization class in memory; S3: Generate an object byte sequence. The network sender reflects and parses the class information according to the mapping table of the serialization class to generate an object byte sequence; S4: Object byte sequence parsing. The network receiver parses the received object byte sequence and decodes the object byte sequence into an object; In step S4, the specific process of decoding the object byte sequence into an object is: obtain the serialization class number in the first byte of the object byte sequence, and obtain the corresponding class metadata through memory according to the serialization inner class number; according to the class attributes and metadata, sequentially parse out the attribute data of the class, and decode the object byte sequence into an object; When parsing the attribute data of the class, when encountering the variable-length data of the string class attribute, obtain its size through the metadata and the data at the head of the byte array, and for the attribute data of other basic types, read it according to the basic type length specified by the Java specification; The mapping table of the class includes: class number and full class path; There is a class corresponding attribute lookup table in memory for the mapping table of the class; The class corresponding attribute lookup table includes: class number, attribute position, attribute name, and attribute length; The attribute length of the variable-length character is UNKnown.
2. A simple object transmission serialization device, characterized in that: The device includes: Simple object serialization format construction module: The simple object serialization format includes: a serialization class number, a variable-length character length, and class attributes; where there are multiple class attributes; Meta-information extraction module: According to the annotations of the serialization class, extract the meta-information of the serialization class into memory in advance; there is a mapping table of the serialization class in memory; Object byte sequence generation module: The network sender reflects and parses the class information according to the mapping table of the serialization class to generate an object byte sequence; Object byte sequence parsing module: The network receiver parses the received object byte sequence and decodes the object byte sequence into an object; The specific process of decoding the object byte sequence into an object is: obtain the serialization class number in the first byte of the object byte sequence, and obtain the corresponding class metadata through memory according to the serialization inner class number; according to the class attributes and metadata, sequentially parse out the attribute data of the class, and decode the object byte sequence into an object; When parsing the attribute data of the class, when encountering the variable-length data of the string class attribute, obtain its size through the metadata and the data at the head of the byte array, and for the attribute data of other basic types, read it according to the basic type length specified by the Java specification; The mapping table of the class includes: class number and full class path; There is a class corresponding attribute lookup table in memory for the mapping table of the class; The class corresponding attribute lookup table includes: class number, attribute position, attribute name, and attribute length; The attribute length of the variable-length character is UNKnown.
3. A computer device, characterized in that, It includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps of a simple object transfer serialization method as described in claim 1.
4. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the steps of a simple object transfer serialization method as described in claim 1.
Citation Information
Patent Citations
Methods and devices for serializing and deserializing object type column in database
CN107341262A
Method and apparatus for serialization and deserialization
CN109117209A
Factory equipment sub-system communicating method and apparatus thereof
CN1848846A