Object serialization method, object deserialization method, and apparatus

By optimizing Java object serialization through adaptive variable-length integer encoding and bitmap tagging technology, the problems of storage space waste and low efficiency in Java serialization schemes are solved, and efficient object storage and transmission are achieved.

CN122173167APending Publication Date: 2026-06-09SHENZHEN QUANLIANRONG TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHENZHEN QUANLIANRONG TECH CO LTD
Filing Date
2026-02-28
Publication Date
2026-06-09

AI Technical Summary

Technical Problem

Existing Java serialization schemes result in large data volumes, consume a lot of storage space, waste resources significantly, and have low deserialization efficiency.

Method used

An adaptive variable-length integer encoding technique is adopted, which automatically selects the target encoding length from the variable-length encoding rule table based on the number of significant digits of the integer value, encodes the integer value wrapper class object, and optimizes the serialization and deserialization process by combining bitmap marking and reflection mechanism.

Benefits of technology

It reduces storage space usage and improves serialization and deserialization efficiency, making it suitable for efficient storage and transmission of objects in Java applications.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122173167A_ABST
    Figure CN122173167A_ABST
Patent Text Reader

Abstract

This application relates to the field of computer software, providing an object serialization method, an object deserialization method, and an apparatus. The object serialization method includes: obtaining an object to be serialized; if the object to be serialized is an integer wrapper class object, selecting a target encoding length from a variable-length encoding rule table based on the effective number of bits of the integer value encapsulated within the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value; and encoding the integer value based on the target encoding length to obtain an encoded byte array. This application employs variable-length integer encoding technology, which reduces the number of bytes occupied by small values, thereby reducing storage space usage and improving serialization storage efficiency. It is suitable for efficient storage and transmission of objects in Java applications.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer software technology, and in particular to object serialization methods, object deserialization methods and apparatus. Background Technology

[0002] In Java application development, object serialization is the process of converting an object's state into a byte stream for storage on disk, in a database, or over a network; deserialization is the opposite process, restoring the byte stream back into an object.

[0003] Existing Java serialization solutions include Java native serialization, which can be implemented by implementing the Serializable interface. However, data obtained through Java native serialization is large in size and consumes a lot of storage space, resulting in wasted resources. Summary of the Invention

[0004] This application aims to address at least one of the technical problems existing in related technologies. To this end, this application proposes an object serialization method that reduces storage space usage and improves serialization storage efficiency, making it suitable for efficient storage and transmission of objects in Java applications.

[0005] This application also proposes an object deserialization method with clear and efficient decoding logic, which improves deserialization efficiency.

[0006] The object serialization method according to the first aspect of this application includes: Get the object to be serialized; When the object to be serialized is an integer wrapper class object, the target encoding length is selected from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value. Based on the target encoding length, the integer value is encoded to obtain an encoded byte array.

[0007] The object serialization method according to the embodiments of this application, compared with most existing object serialization schemes that do not optimize for numeric types, occupy fixed storage space, and have low storage efficiency leading to resource waste, adopts an adaptive variable-length integer encoding technology for integer wrapper class objects. Based on the effective number of digits of the integer value, the target encoding length is automatically selected from the variable-length encoding rule table, and the integer value is encoded based on the adaptively selected target encoding length. This makes small values ​​occupy fewer bytes, reduces the storage space occupied, and improves the serialization storage efficiency, making it suitable for efficient storage and transmission of objects in Java applications.

[0008] According to one embodiment of this application, the number of significant digits of the integer value is determined in the following manner: Determine the absolute value of the integer value; Bitwise operations are used to calculate the minimum number of bits required to represent the absolute value of the integer value, thus obtaining the effective number of bits of the integer value.

[0009] According to one embodiment of this application, encoding the integer value based on the target encoding length to obtain an encoded byte array includes: Determine the sign bit of the integer value; Based on the target encoding length, the effective data bits of the integer value are subjected to variable-length encoding to obtain a variable-length encoding result; A length marker bit and the sign bit are added to the high bits of the variable-length encoding result to obtain the encoded byte array; the value of the length marker bit is determined based on the target encoding length.

[0010] According to one embodiment of this application, when the object to be serialized is a wrapper class object, and the primitive type value encapsulated within the wrapper class object is zero, the method further includes: Bitmap markers are applied to zero-value fields in wrapper class objects to obtain marked data. The marked data and non-zero value values ​​are serialized into byte arrays.

[0011] According to one embodiment of this application, when the object to be serialized is an array type object, the method further includes: Based on the length of the array, determine the required number of bitmap bytes and initialize the bitmap array; each bit of the bitmap array is used to indicate whether the element at the corresponding index position in the array is null; Traverse the array, set the bit corresponding to the null element to the first flag value, and set the bit corresponding to the non-null element to the second flag value to obtain the bitmap data; The length of the array, the bitmap data, and the non-empty elements are serialized into a byte array.

[0012] According to one embodiment of this application, after obtaining the object to be serialized, the method further includes: Obtain all getter methods of the object to be serialized; each getter method corresponds one-to-one with an object property. Sort the property names corresponding to each getter method to obtain the property sorting order; Based on the attribute sorting order, iterate through and call each getter method to obtain the current attribute value; Mark whether the current attribute value is empty or zero to obtain the marked data; The marked data and attribute values ​​that are not empty or zero are serialized into byte arrays.

[0013] According to one embodiment of this application, when the object to be serialized is a Boolean type object, the method further includes: If the boolean object is null, an empty byte array is returned; If the boolean value of the boolean type object is true, then a byte array containing the first preset byte representation is returned; If the boolean value of the boolean type object is false, then a byte array containing the second preset byte representation is returned; The first preset byte representation is different from the second preset byte representation.

[0014] According to one embodiment of this application, when it is determined that the object to be serialized is not null, the method further includes: Obtain the object type of the object to be serialized, and obtain the serializer corresponding to the object type from the serializer factory; If the serializer corresponding to the object type does not exist in the cache, a new serializer corresponding to the object type is created based on the object type, the new serializer corresponding to the object type is called, the object to be serialized is converted into a byte array, and the new serializer corresponding to the object type is stored in the cache. If the serializer corresponding to the object type exists in the cache, then the serializer in the cache is called to convert the object to be serialized into a byte array.

[0015] The object deserialization method according to the second aspect of this application includes: Read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit; Based on the target encoding length, parse the sign bit and data bit in the encoded byte array; Based on the sign bit and the data bit, the original integer value is reconstructed, and the integer wrapper class object is recovered based on the original integer value.

[0016] According to the object deserialization method of this application embodiment, the original integer value is reconstructed by parsing the length marker bit, sign bit, and data bits of the encoded byte array, thereby realizing object deserialization. This avoids complex condition judgments and redundant operations, and the decoding logic is clear and efficient, improving deserialization efficiency. Furthermore, after serializing the integer wrapper class object by performing variable-length integer encoding based on the effective number of digits of the integer value, the corresponding deserialization process is executed to restore the integer wrapper class object, which together reduces the data size, saves storage space and network bandwidth, and is suitable for efficient storage and transmission of objects in Java applications.

[0017] According to one embodiment of this application, the method further includes: Read the array length and bitmap data from the encoded byte array; Based on the value of each bit in the bitmap data, determine whether the element at the corresponding index position in the array is empty, and obtain the empty value judgment result; Based on the array length and the result of the null value determination, the array type object is restored.

[0018] According to one embodiment of this application, the method further includes: Based on the labeled data, empty and zero-value positions are filled with zero values.

[0019] According to one embodiment of this application, the method further includes: The object's state is restored by invoking the setter method using reflection.

[0020] According to one embodiment of this application, the method further includes: If the encoded byte array contains empty bytes, return null. If the first byte of the byte data is represented by a first preset byte, then return true; If the first byte of the byte data is represented by a second preset byte, then return false; If the first byte of the byte data is represented by other bytes, an exception is thrown.

[0021] An object serialization apparatus according to a third aspect embodiment of this application includes: The acquisition module is used to acquire the object to be serialized; The length selection module is used to select a target encoding length from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer value wrapper class object when the object to be serialized is an integer value wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value. The encoding module is used to encode the integer value based on the target encoding length to obtain an encoded byte array.

[0022] An object deserialization apparatus according to a fourth aspect of this application includes: The length determination module is used to read the length marker bit in the encoded byte array and determine the target encoding length based on the value of the length marker bit; The parsing module is used to parse the sign bit and data bit in the encoded byte array based on the target encoding length; The reconstruction module is used to reconstruct the original integer value based on the sign bit and the data bit, so as to restore the integer value wrapper class object based on the original integer value.

[0023] An electronic device according to a fifth aspect of this application includes a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the computer program, implements an object serialization method or an object deserialization method as described in any of the preceding claims.

[0024] A non-transitory computer-readable storage medium according to a sixth aspect of this application stores a computer program thereon, characterized in that, when executed by a processor, the computer program implements an object serialization method or an object deserialization method as described in any of the preceding claims.

[0025] A computer program product according to a seventh aspect of this application includes a computer program, characterized in that, when the computer program is executed by a processor, it implements the object serialization method or the object deserialization method as described in any of the preceding claims.

[0026] The above-described one or more technical solutions in the embodiments of this application have at least one of the following technical effects: Compared to most existing object serialization schemes that do not optimize for numeric types, resulting in fixed storage space usage, low storage efficiency, and wasted resources, this solution addresses the issue of integer wrapper class objects by employing adaptive variable-length integer encoding technology. This technology automatically selects the target encoding length from a variable-length encoding rule table based on the number of significant digits of the integer value. Encoding the integer value based on this adaptively selected target encoding length allows smaller values ​​to occupy fewer bytes, reducing storage space usage and thus improving serialization storage efficiency. This solution is suitable for efficient storage and transmission of objects in Java applications.

[0027] By reconstructing the original integer value by parsing the length marker, sign bit, and data bits of the encoded byte array, object deserialization is achieved, avoiding complex conditional judgments and redundant operations. The decoding logic is clear and efficient, improving deserialization efficiency. Furthermore, after serializing the integer wrapper class object by performing variable-length integer encoding based on the significant number of digits of the integer value, the corresponding deserialization process is executed to restore the integer wrapper class object, which together reduces the data size, saves storage space and network bandwidth, and is suitable for efficient storage and transmission of objects in Java applications.

[0028] Additional aspects and advantages of this application will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of this application. Attached Figure Description

[0029] To more clearly illustrate the technical solutions in the embodiments or related technologies of this application, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0030] Figure 1 This is one of the flowcharts illustrating the object serialization method provided in the embodiments of this application.

[0031] Figure 2 This is a schematic diagram of the variable-length encoding format for the Integer type provided in the embodiments of this application.

[0032] Figure 3 This is a schematic diagram of the NullFlagList bitmap structure provided in the embodiments of this application.

[0033] Figure 4 This is a schematic diagram of the object serialization system architecture provided in the embodiments of this application.

[0034] Figure 5 This is a flowchart illustrating the deserialization method provided in the embodiments of this application.

[0035] Figure 6 This is the second flowchart of the object serialization method provided in the embodiments of this application.

[0036] Figure 7 This is a schematic diagram of the structure of the object serialization device provided in the embodiments of this application.

[0037] Figure 8 This is a schematic diagram of the structure of the object serialization device provided in the embodiments of this application.

[0038] Figure 9 This is a schematic diagram of the physical structure of the electronic device provided in the embodiments of this application. Detailed Implementation

[0039] The embodiments of this application will be described in further detail below with reference to the accompanying drawings and examples. The following examples are used to illustrate this application, but should not be used to limit the scope of this application.

[0040] In the description of the embodiments of this application, it should be noted that the terms "center," "longitudinal," "lateral," "upper," "lower," "front," "rear," "left," "right," "vertical," "horizontal," "top," "bottom," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing the embodiments of this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on the embodiments of this application. In addition, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.

[0041] In the description of the embodiments of this application, it should be noted that, unless otherwise explicitly specified and limited, the terms "connected" and "linked" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium. Those skilled in the art can understand the specific meaning of the above terms in the embodiments of this application based on the specific circumstances.

[0042] In the embodiments of this application, unless otherwise expressly specified and limited, "above" or "below" the second feature can mean that the first feature is in direct contact with the second feature, or that the first feature is in indirect contact with the second feature through an intermediate medium. Furthermore, "above," "on top of," and "over" the second feature can mean that the first feature is directly above or diagonally above the second feature, or simply that the first feature is at a higher horizontal level than the second feature. "Below," "below," and "under" the second feature can mean that the first feature is directly below or diagonally below the second feature, or simply that the first feature is at a lower horizontal level than the second feature.

[0043] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the embodiments of this application. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0044] Figure 1 This is one of the flowcharts illustrating the object serialization method provided in this application. (Refer to...) Figure 1 This application provides an object serialization method, which may specifically include the following steps: Step 101: Obtain the object to be serialized.

[0045] It should be noted that the execution subject of the object serialization method provided in this application embodiment can be an electronic device, a component in an electronic device, an integrated circuit, or a chip. The electronic device can be a mobile electronic device or a non-mobile electronic device. For example, a mobile electronic device can be a mobile phone, tablet computer, laptop computer, PDA, wearable device, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), etc., while a non-mobile electronic device can be a server, network attached storage (NAS), personal computer (PC), television (TV), ATM, or self-service machine, etc. This application embodiment does not specifically limit these. The following description uses a server as the execution subject.

[0046] The object to be serialized can be a data object that supports serialization operations and needs to be converted into a byte sequence. In the Java platform, the object to be serialized can be a Java object instance that implements the Serializable interface.

[0047] In some embodiments, before serializing an object, it can be checked whether the object to be serialized is null. If it is null, NULL_OBJECT is returned and the object serialization process is not executed; if it is not null, the object serialization process is executed. Embodiments of this application can use an empty byte array (NULL_OBJECT) as a uniform marker for null values ​​to achieve unified null value handling.

[0048] Step 102: When the object to be serialized is an integer wrapper class object, select the target encoding length from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent an integer value.

[0049] The serializer corresponding to the integer wrapper class encapsulates the serialization logic for the integer wrapper class object. After obtaining the object to be serialized and determining that the object is not null, if the object type is determined to be an integer wrapper class, the serializer corresponding to the integer wrapper class can be called to execute the serialization logic for the integer wrapper class object (i.e., based on the significant number of digits of the integer value encapsulated inside the integer wrapper class object, selecting the target encoding length from the variable-length encoding rule table, and encoding the integer value based on the target encoding length to obtain the encoded byte array), thereby achieving serialization of the integer wrapper class object. It should be noted that different object types can each correspond to different serializers. In this embodiment, the corresponding serializer can also be called to execute the corresponding serialization logic for different object types.

[0050] Integer wrapper classes are classes used to encapsulate basic integer types. These can include classes like Byte, Short, Integer, and Long. Internally, an integer wrapper class object can contain fields for storing integer values. For example, the Integer class encapsulates a private final int value field, and the Long class encapsulates a private final long value field.

[0051] In this embodiment of the application, after obtaining the object to be serialized, if the object to be serialized is an integer wrapper class object, the integer wrapper class field in the object can be identified and located, the integer value encapsulated inside can be extracted, and the target encoding length can be selected from the variable length encoding rule table based on the effective number of digits of the integer value to encode the integer value, thereby writing the encoded byte array as the serialization result of the field into the final output stream.

[0052] The variable-length encoding rule table can be a predefined mapping data structure used to record the correspondence between the significant number of digits of an integer value and the encoding length (i.e., encoding format) during serialization output. The encoding length can refer to the number of bytes dynamically allocated based on the significant number of digits of the integer value. In this embodiment, the encoding length corresponding to the significant number of digits can be automatically selected from the variable-length encoding rule table as the target encoding length based on the significant number of digits of the integer value, thereby implementing adaptive variable-length integer encoding for integer values ​​in integer wrapper class objects.

[0053] Different integer wrapper classes can support different variable-length encoding lengths. For example, the Integer type can support 1-5 byte encodings, while the Long type can support 1-9 byte encodings.

[0054] Figure 2 This is a schematic diagram of the variable-length encoding format for the Integer type provided in an embodiment of this application. (Refer to...) Figure 2 In one example, for the Integer type, when the number of significant digits is less than or equal to 4, the corresponding variable-length encoding format can be 1 byte; when the number of significant digits is 5-12, the corresponding variable-length encoding format can be 2 bytes; when the number of significant digits is 13-20, the corresponding variable-length encoding format can be 3 bytes; when the number of significant digits is 21-27, the corresponding variable-length encoding format can be 4 bytes; and when the number of significant digits is 28-32, the corresponding variable-length encoding format can be 5 bytes.

[0055] In one alternative embodiment, in addition to variable-length integer encoding schemes, dictionary-based compression encoding schemes or statistical Huffman coding can also be used to encode integer numerical schemes.

[0056] Specifically, for a large number of recurring integer values, a dictionary-based compression encoding scheme can be used, where dictionary indices are used instead of the actual values.

[0057] For example, taking [33566731, 33566731, 3, 913, 33566731, 31, 33566731, 31, 3, 33566731] as an example, in scenarios with a large number of duplicate values, the values ​​can be classified and encoded first (e.g., generating a dictionary 0:33566731, 1:3, 2, 913, 4:31). Then, during data transmission, the dictionary (0:33566731, 1:3, 2, 913, 4:31) is transmitted first, followed by the value index (0, 0, 1, 2, 0, 3, 0, 3, 1, 0), which can effectively save bandwidth.

[0058] Specifically, a statistical Huffman coding scheme can be used, which optimizes the encoding based on the frequency of numerical values. Similar to dictionary compression, statistical Huffman coding allows the creation of a Huffman tree based on the frequency of numerical values, serving as the dictionary data source and further compressing the data at the bit level.

[0059] Step 103: Encode the integer value based on the target encoding length to obtain the encoded byte array.

[0060] In this embodiment, integer values ​​can be encoded based on a selected target encoding length, and the encoded byte array is written as the serialization result of the field into the final output stream. Compared to a fixed storage space method, this embodiment dynamically selects the encoding length based on the effective number of bits of the integer value to adaptively encode the integer value. This allows the encoding length to be dynamically determined by the actual effective size of the integer value, which helps reduce storage space usage.

[0061] Compared to most existing object serialization schemes that do not optimize for numeric types, resulting in fixed storage space usage, low storage efficiency, and wasted resources, this application's embodiment addresses integer numeric wrapper class objects by employing adaptive variable-length integer encoding technology. This technology automatically selects the target encoding length from a variable-length encoding rule table based on the effective number of digits in the integer value. Encoding the integer value based on the adaptively selected target encoding length reduces the number of bytes occupied by smaller values, thereby decreasing storage space usage and improving serialization storage efficiency. This approach is suitable for efficient storage and transmission of objects in Java applications.

[0062] Based on any of the above embodiments, the number of significant bits of an integer value can be determined in the following way: determine the absolute value of the integer value; use bitwise operations to calculate the minimum number of bits required to represent the absolute value of the integer value, and obtain the number of significant bits of the integer value.

[0063] In some embodiments, a sign bit extraction operation can be performed on an integer value to obtain the sign identifier of the integer value. When the sign identifier indicates a negative value, the integer value can be inverted bitwise and incremented by one to obtain the absolute value of the integer value; when the sign identifier indicates a non-negative value (i.e., a positive value or zero), the integer value can be directly used as the absolute value.

[0064] In some embodiments, bitwise operations can be used to remove the same padding bits in the higher bits (i.e., remove redundant 0 bits in the higher bits, which are usually only used to pad a fixed width, such as 32 bits, and do not carry actual numerical information), thereby determining the minimum number of bits required for the absolute value of the integer value, obtaining the effective number of bits of the integer value, and then selecting the target encoding length from the variable-length encoding rule table based on the effective number of bits.

[0065] Specifically, a bit counting instruction can be used to calculate the number of consecutive zeros in the high-order bits of the absolute value binary representation, thus obtaining the number of effective bits.

[0066] For example, taking the value 5 as an example, the value field encapsulated inside the Integer object can store the integer value in 32-bit binary two's complement form. After removing the same padding bits in the high bits, the number of valid bits is 3 (that is, 101 has three valid data bits, so the number of valid bits is 3).

[0067] The embodiments of this application calculate the absolute value of an integer value and use bitwise operations to calculate the effective number of bits (removing the same padding bits in the high bits). The calculated effective number of bits (i.e. the minimum number of bits) can provide an accurate basis for the subsequent dynamic allocation of storage bytes, which is beneficial for the subsequent implementation of variable-length integer encoding, occupying fewer bytes and reducing the storage space occupied.

[0068] Based on any of the above embodiments, the integer value is encoded based on the target encoding length to obtain an encoded byte array. Specifically, this may include: determining the sign bit of the integer value; and performing variable-length encoding on the valid data bits of the integer value based on the target encoding length to obtain a variable-length encoding result. A length marker and a sign bit are added to the high bits of the variable-length encoding result to obtain the encoded byte array; the value of the length marker is determined based on the target encoding length.

[0069] In this embodiment, a sign bit extraction operation can be performed on the integer value to obtain the sign identifier (positive / negative) of the integer value. After performing variable-length encoding on the effective data bits of the integer value based on the target encoding length to obtain the variable-length encoding result, a length marker bit and a sign bit can be added to the high bits of the variable-length encoding result to obtain the encoded byte array.

[0070] In some embodiments, the variable-length encoding rule table may also record the length flag bit format and data bit format corresponding to each encoding format, so that the value of the length flag bit can be determined based on the selected target encoding length.

[0071] Continue to refer to Figure 2 In one example, for the Integer type, the 1-byte format may include 4 bits of data and a length marker bit 00, the 2-byte format may include 12 bits of data and a length marker bit 01, the 3-byte format may include 20 bits of data and a length marker bit 10, the 4-byte format may include 27 bits of data and a length marker bit 110, and the 5-byte format may include the following 4 bytes of complete data and a length marker bit 111.

[0072] This application embodiment uses a selected target encoding length to perform variable-length encoding on the effective data bits of an integer value, resulting in a variable-length encoding result. The length marker bit and sign bit are embedded in the high bits of the variable-length encoding result, which can achieve compact storage of effective data bits and compact byte representation of metadata, thus improving serialization storage efficiency.

[0073] Based on any of the above embodiments, when the object to be serialized is a wrapper class object and the basic type value encapsulated inside the wrapper class object is zero, the method further includes: performing bitmap marking on the zero-value field in the wrapper class object to obtain marked data; and serializing the marked data and non-zero value values ​​into a byte array.

[0074] In the embodiments of this application, during serialization, for numeric types (byte, short, int, long, float, double), the value 0 can be treated as null, and the field with the value 0 is marked with a bitmap. Therefore, during the data storage stage, the value of the field marked as zero will not be written to the final byte array. The byte array only contains the field type, the marked data, and the values ​​of the non-zero value fields.

[0075] This application embodiment optimizes the numerical zero value by treating the numerical value 0 as a null value during serialization, thereby reducing the storage overhead of zero value data.

[0076] Based on any of the above embodiments, when the object to be serialized is an array type object, the method further includes: determining the required number of bitmap bytes based on the length of the array, and initializing the bitmap array; each bit of the bitmap array is used to indicate whether the element at the corresponding index position in the array is null; traversing the array, setting the bit corresponding to the null element to the first flag value, and setting the bit corresponding to the non-null element to the second flag value to obtain the bitmap data; and serializing the array length, the bitmap data, and the non-null elements into a byte array.

[0077] In this embodiment, the array type may include arrays of primitive types and arrays of object types. An object type array is an array whose elements are object references or null values. Primitive type arrays are used to store numeric data; the elements of a primitive type array can be Java primitive numbers (byte, short, int, long, float, double) or zero values.

[0078] During serialization, for arrays of object types, a bitmap can be used to mark whether each element in the array is null. Every 8 elements occupy 1 byte, thus achieving array null mark optimization through bitmap marking technology (NullFlagList, a null mark list that uses a bitmap to mark the position of null elements in the array); for arrays of primitive types, a bitmap can be used to mark whether each element in the primitive type array is zero (treating zero as null), achieving zero value optimization.

[0079] A bitmap is a data structure that uses binary bits to mark states. A bitmap array is a byte array structure, where each bit can serve as a state flag corresponding to an empty state at an index position in the array. In some embodiments, the required number of bitmap bytes can be calculated based on the array length: ceil(length / 8).

[0080] For arrays of object types, a null element can be null in the array, not pointing to any object instance in heap memory; a non-null element can be a reference type element in the array that points to an actual object instance. For arrays of primitive types, a null element can be 0 in the array, and a non-null element can be a non-zero value in the array.

[0081] The first and second flag values ​​can be binary 1 and 0, respectively, used to identify null and non-null states. During serialization, null elements can be identified as compressible redundant elements and skipped from serialization via bitmap marking; non-null elements can be identified as payloads and participate in the serialization process.

[0082] In some embodiments, the array can be iterated over, and the `setNull()` method can be used to mark null elements, thus implementing the null value marking operation for empty elements. It should be noted that for arrays of primitive types, the value 0 can be regarded as a null element and marked as null in the NullFlagList bitmap.

[0083] In this embodiment, the bits corresponding to null elements can be set to 1, and the bits corresponding to non-null elements can be set to 0 to obtain bitmap data, thereby serializing the array length, bitmap data, and non-null elements into a byte array.

[0084] Figure 3 This is a schematic diagram of the NullFlagList bitmap structure provided in an embodiment of this application. (Refer to...) Figure 3In one example, assuming the array is [null, A, null, B, C, null, null, D, E], the bitmap size can be calculated to be 2 bytes. By setting the bit corresponding to null to 1 and the bits corresponding to A, B, C, D, and E to 0 through bitmap marking, the first byte represents 10100110 and the second byte represents 0xxxxxxx. The actual data stored is only non-null elements.

[0085] To address the issues of insufficient optimization in handling null elements in arrays or collections, inefficient null value handling, and wasted storage space in existing Java serialization schemes, this application uses bitmaps to mark whether each element in the array is null. Every 8 elements occupy 1 byte. By using bitmap marking, null elements in the array occupy almost no extra space, optimizing the marking and storage of null elements in arrays / collections, reducing storage space usage, and making it suitable for efficient storage and transmission of objects in Java applications.

[0086] In an alternative embodiment, in addition to using bitmap markers to mark null values, a sparse array storage scheme and a run-length encoding scheme can also be used.

[0087] Specifically, in a sparse array storage scheme, only the indices and values ​​of non-null elements can be stored. For scenarios where there are many null values ​​in array or object properties (e.g., less than 1 / 8 of the properties have values), the index of each non-null element can be recorded separately instead of using bitmap markers.

[0088] For example, for the array [null,null,null,null,null,null,null,null,null, "abc",null, ...], if the array has 64 elements and only the 10th element is a non-null value, then it can be encoded as 10: "abc", thus saving storage space more effectively.

[0089] Specifically, in run-length encoding schemes, consecutive null elements can be compressed. In scenarios with consecutive null elements, the array becomes very sparse. In such cases, the number of consecutive null elements can be recorded using an enhanced bitmap marker, thereby saving space.

[0090] For example, the array [null,null,null,“abc”,null,null,null,…(100 nulls),“aaa”,null,…(20 nulls),“eee”] can be encoded as: 3 nulls + “abc” + 100 nulls + “aaa” + 20 nulls + eee, thus effectively saving storage space.

[0091] Based on any of the above embodiments, after obtaining the object to be serialized, the method further includes: obtaining all getter methods of the object to be serialized; the getter methods correspond one-to-one with the object properties; sorting the property names corresponding to each getter method to obtain the property sorting order; traversing and calling each getter method based on the property sorting order to obtain the current property value; marking whether the current property value is null or zero to obtain the marked data; and serializing the marked data and the property values ​​that are not null or zero into a byte array.

[0092] Getter methods and object properties can establish a deterministic one-to-one mapping relationship through naming conventions: the property `name` corresponds to the method `getName()`. In some embodiments, all getter methods of the object to be serialized can be obtained through reflection, with each getter method corresponding to an object property, used to retrieve the current property value. This application embodiment dynamically obtains all getter methods of an object through reflection, applicable to objects to be serialized that conform to the specifications, avoiding the need for a predefined fixed property list, and improving versatility and flexibility.

[0093] In this embodiment, the property names corresponding to each getter method can be extracted, the property names corresponding to each getter method can be sorted to obtain the property sorting order, and the getter methods of each property can be traversed and called sequentially based on the property sorting order to obtain the current property value of each property, thereby ensuring the consistency of the serialization order.

[0094] In some embodiments, NullFlagList can be used to mark whether an attribute value is null (empty value) or zero, obtaining marked data, and then serializing the marked data and the attribute values ​​that are not null or zero into a byte array. In this way, the serialized data does not contain attribute names and type information, but only saves the marked data and the actual valid data (i.e., attribute values ​​that are not null or zero), thereby compressing the size of the serialized data and reducing the storage space occupied.

[0095] This application embodiment employs a getter / setter-based reflection mechanism, sorts by attribute name, and does not save attribute name and type information. This ensures the consistency of the serialization order while reducing the storage volume and transmission bandwidth of the serialized data.

[0096] Based on any of the above embodiments, when the object to be serialized is a Boolean type object, the method further includes: if the Boolean type object is null, then return an empty byte array; if the Boolean value of the Boolean type object is true, then return a byte array containing a first preset byte representation; if the Boolean value of the Boolean type object is false, then return a byte array containing a second preset byte representation; wherein the first preset byte representation is different from the second preset byte representation.

[0097] Boolean objects can refer to instances of the wrapper class Boolean in Java. Boolean objects may be null or may contain the values ​​true or false.

[0098] For Boolean objects, a null value indicates that the field has not been assigned a value or has been explicitly set to null. In this embodiment, we can first check whether the Boolean object is null. If it is null, we can return an empty byte array (0 bytes in length, which does not occupy any data space in the serialization result).

[0099] The first preset byte representation can be one or more predefined bytes used to uniquely identify the true value; for example, it can be a single byte 0x01 (1 byte in length). The second preset byte representation can also be one or more predefined bytes used to uniquely identify the false value; for example, it can be a single byte 0x00 (1 byte in length).

[0100] In this embodiment of the application, during Boolean serialization, the three possible states of the Boolean object are mapped to different byte sequences, and a 1-byte encoding scheme is adopted. For null values, no bytes are occupied, and for true and false, only a very small 1-byte representation is used, eliminating all metadata and object header overhead. It is suitable for serialization scenarios with a large number of Boolean values; the processing logic is simple, which helps to improve serialization efficiency; it can be embedded into object serialization schemes and has strong versatility.

[0101] Based on any of the above embodiments, if it is determined that the object to be serialized is not null, the method further includes: obtaining the object type of the object to be serialized, and obtaining the serializer corresponding to the object type from the serializer factory; if the serializer corresponding to the object type does not exist in the cache, then creating a new serializer corresponding to the object type based on the object type, calling the new serializer corresponding to the object type to convert the object to be serialized into a byte array, and storing the new serializer corresponding to the object type in the cache; if the serializer corresponding to the object type exists in the cache, then calling the serializer in the cache to convert the object to be serialized into a byte array.

[0102] An object type can be used to identify the actual type of an object. The Java types supported in this application's embodiments may include: primitive types (byte, char, short, int, long, float, double, boolean), wrapper types (Byte, Character, Short, Integer, Long, Float, Double, Boolean), complex types (String, BigDecimal, Date), array types (all primitive type arrays, object arrays), collection types (List and its subclasses ArrayList, LinkedList, Stack, Vector, etc., Map), enumeration types (all enumeration classes), and custom object types (supporting arbitrary custom objects through getter / setter reflection).

[0103] Figure 4 This is a schematic diagram of the object serialization system architecture provided in an embodiment of this application. (Refer to...) Figure 4 In some embodiments, a modular design can be adopted, and object serialization can be achieved through core components such as the serialization interface (VSerializable), serializer factory (SerializerFactory), data sequence (DataSequence), different types of serializers, and dynamic code generation tool (JavassistTool).

[0104] The serialization interface defines a unified interface for serialization and deserialization methods, and all types of serializers need to implement this interface.

[0105] The SerializerFactory maintains a mapping between object types and serializers, and is responsible for creating and managing various serializer instances. It can employ a singleton pattern and a caching mechanism. After determining the object type, the SerializerFactory can return the corresponding available serializer.

[0106] Caching can be used to save already created serializers, avoiding duplicate creation. Introducing a caching mechanism can reduce the number of serializer creations and lower overhead.

[0107] Data sequences can provide sequential reading functionality for byte arrays, including methods such as hasNext(), next(), and next(intlength).

[0108] A serializer is a component (or function) responsible for converting objects of a specific type into byte arrays. It typically encapsulates the serialization logic for that type, such as how to read object fields, how to encode primitive types, and how to handle nested objects. Different object types can correspond to different serializers. Specialized serializers can be implemented for different Java types, including: primitive type serializers (ByteSerializer, ShortSerializer, IntegerSerializer, LongSerializer, FloatSerializer, DoubleSerializer, CharSerializer, BooleanSerializer), wrapper type serializers (corresponding to the wrapper types of primitive types), complex type serializers (StringSerializer, BigDecimalSerializer, DateSerializer), array type serializers (ByteArraySerializer, ShortArraySerializer, IntArraySerializer, LongArraySerializer, FloatArraySerializer, DoubleArraySerializer, CharArraySerializer, BooleanArraySerializer, ObjectArraySerializer), collection type serializers (ListSerializer, MapSerializer), and enum type serializers (EnumSerializer).

[0109] Dynamic code generation tools are used to dynamically generate array and list creation utility classes at runtime using the Javassist bytecode manipulation library: NewArrayTool (array creation utility interface, providing newArray and copy methods): supports the creation and copying of arrays of primitive types and object types; NewListTool (list creation utility interface, providing newList and copy methods): supports the creation and copying of List subclasses such as ArrayList, LinkedList, Stack, and Vector.

[0110] In one alternative embodiment, in addition to Javassist, the following dynamic compilation schemes can also be used: ASM bytecode manipulation library (ASM is a Java bytecode manipulation framework), CGLIB dynamic proxy (CGLIB is an open-source code generation library based on Java), and JavaCompiler API (Application Programming Interface).

[0111] In this embodiment, if the serializer corresponding to the object type does not exist in the cache (i.e., this is the first time the type is being processed), a new serializer can be dynamically created based on the object type. The newly created serializer is then called to convert the object to be serialized into a byte array, and the newly created serializer is stored in the cache for reuse by subsequent objects of the same type. If the serializer corresponding to the object type already exists in the cache, it can be directly retrieved from the cache and called to complete the conversion from object to byte array.

[0112] This application embodiment achieves dynamic type serializer creation by using the Javassist bytecode manipulation library to dynamically generate serializers at runtime.

[0113] To address the performance issues arising from the numerous reflection calls and object creations during serialization / deserialization in existing Java serialization schemes, this application's embodiments employ a factory pattern and a caching mechanism to manage the serializer, avoiding the overhead of repeatedly creating objects of the same type and thus improving serialization performance.

[0114] To address the limited support of existing Java serialization schemes for complex types (such as generic collections, enumerations, and custom objects), this application adopts a factory pattern and a caching mechanism to manage serializers, making it easy to add serializers of new types without modifying the calling code, thus providing strong extensibility.

[0115] Figure 5 This is a flowchart illustrating the deserialization method provided in an embodiment of this application. (Refer to...) Figure 5 This application provides an object deserialization method, which may specifically include the following steps: Step 501: Read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit.

[0116] It should be noted that the execution subject of the object serialization method provided in this application embodiment can be an electronic device, a component in an electronic device, an integrated circuit, or a chip. The electronic device can be a mobile electronic device or a non-mobile electronic device. For example, a mobile electronic device can be a mobile phone, tablet computer, laptop computer, PDA, wearable device, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), etc., while a non-mobile electronic device can be a server, network attached storage (NAS), personal computer (PC), television (TV), ATM, or self-service machine, etc. This application embodiment does not specifically limit these. The following description uses a server as the execution subject.

[0117] The encoded byte array can refer to the byte sequence corresponding to the integer value after serialization, and can contain information such as length marker, sign and data.

[0118] The length flag is a flag used in encoding to indicate the actual number of bytes or data bits occupied by an integer value. In variable-length integer encoding, the length flag can be read to determine how many more bytes need to be read to fully reconstruct the integer value.

[0119] In some embodiments, during object deserialization, length marker bits can be read from the encoded byte array, and the target encoding length (i.e., the total number of bytes or bits occupied) of the integer value during encoding can be determined based on the value of the length marker bits. The length marker bits can be one or more bits used to indicate the number of bytes or bits occupied by subsequent data.

[0120] Step 502: Based on the target encoding length, parse the sign bit and data bit in the encoded byte array.

[0121] The sign bit can be a bit used to indicate whether a number is positive or negative. For example, 0 can represent a positive number, and 1 can represent a negative number.

[0122] Data bits can refer to the bits used in encoding to store the absolute value of an integer value. In the embodiments of this application, during object serialization, bitwise operations are used to calculate the minimum number of bits required to represent the absolute value of the integer value. After obtaining the effective number of bits of the integer value, a target encoding length can be selected from the variable-length encoding rule table based on the effective number of bits of the integer value. Variable-length encoding is then performed on the effective data bits of the integer value based on the target encoding length to obtain the variable-length encoding result. Finally, the value of the variable-length encoding result is written into the data bits (i.e., the data information of the data bits is the variable-length encoding result).

[0123] Step 503: Reconstruct the original integer value based on the sign bit and data bits, and restore the integer wrapper class object based on the original integer value.

[0124] In this embodiment, the original integer value can be calculated based on the sign bit and data bits according to a predetermined numerical reconstruction rule. The original integer value can be a basic type value such as int or long. Then, based on the original integer value, the corresponding integer wrapper class object (such as Integer, Long, etc.) can be reconstructed, and the object is returned as the deserialization result.

[0125] This application's embodiments are adapted to variable-length integer encoding schemes. By parsing the length marker, sign bit, and data bits of the encoded byte array, the original integer value is reconstructed, enabling object deserialization. This avoids complex conditional judgments and redundant operations, resulting in clear and efficient decoding logic and improved deserialization efficiency. Furthermore, after serializing an integer wrapper class object using variable-length integer encoding based on the significant number of digits of the integer value, this application's embodiments restore the integer wrapper class object by executing the corresponding deserialization process. This collectively reduces data size, saves storage space and network bandwidth, and is suitable for efficient storage and transmission of objects in Java applications.

[0126] Based on any of the above embodiments, the method further includes: reading the array length and bitmap data in the encoded byte array; determining whether the element at the corresponding index position in the array is null based on the value of each bit in the bitmap data, and obtaining a null value determination result; and restoring the array type object based on the array length and the null value determination result.

[0127] In this embodiment, the required number of bitmap bytes (ceil(length / 8)) is first calculated based on the array length, and the bitmap array is initialized. During serialization, the array is traversed, and the setNull() method is used to mark null elements, serializing the array length and bitmap data into a byte array. During deserialization, the array length and bitmap data can be read from the byte data stream (i.e., the encoded byte array), and the isNull() method is used to determine whether each element at the corresponding index position in the array is null (empty value), obtaining the null value judgment result (i.e., which elements are null elements and which elements are non-null elements). Finally, the array type object can be restored based on the array length, the null value judgment result, and the non-null elements.

[0128] In this embodiment of the application, the bit corresponding to a null element can be 1, and the bit corresponding to a non-null element can be 0.

[0129] To address the shortcomings of existing Java serialization schemes in handling null elements in arrays or collections, which suffer from inefficient null value processing and wasted storage space, this application's embodiment uses a bitmap to mark whether each element in the array is null during serialization. Every 8 elements occupy 1 byte. During deserialization, the array length and bitmap data can be read to determine whether each element is null. By utilizing bitmap marking, null elements in the array occupy almost no extra space, optimizing the marking and storage of null elements in arrays / collections, reducing storage space usage, and making it suitable for efficient storage and transmission of objects in Java applications, thus improving the performance of serialization and deserialization.

[0130] Based on any of the above embodiments, it further includes: filling null positions and zero positions with zero values ​​based on the labeled data.

[0131] In this embodiment, during serialization, for numeric types (byte, short, int, long, float, double), the value 0 can be treated as null, and fields with 0 values ​​are marked with a bitmap. Therefore, during the data storage stage, the values ​​of fields marked as zero are not written to the final byte array. During deserialization, null or zero value positions can be filled with the default value 0 according to the bitmap marking.

[0132] This application's embodiments optimize for zero values ​​by treating the value 0 as a null value during serialization and filling null or zero value positions with the default value 0 during deserialization. This reduces the storage overhead of zero value data and improves the performance of serialization and deserialization.

[0133] Based on any of the above embodiments, it further includes: restoring the object state by invoking the setter method via reflection.

[0134] In this embodiment, during serialization, the property names corresponding to each getter method can be extracted, and these property names can be sorted to obtain the property sorting order. Based on the property sorting order, the getter methods of each property are traversed and called sequentially to obtain the current property value, thereby ensuring the consistency of the serialization order. During deserialization, the object state can be restored by calling the setter methods through reflection.

[0135] This application's embodiments employ a reflection mechanism based on getter / setter (getter / setter are standard access methods for JavaBeans, used to obtain and set object properties), sorting by property name and not storing property name and type information. This ensures the consistency of the serialization order while reducing the storage volume and transmission bandwidth of serialized data, and improving the performance of serialization and deserialization.

[0136] Based on any of the above embodiments, it further includes: if the byte data in the encoded byte array is empty, then return a null value; if the first byte of the byte data is represented by a first preset byte, then return true; if the first byte of the byte data is represented by a second preset byte, then return false; if the first byte of the byte data is represented by other bytes, then throw an exception.

[0137] In this embodiment, during the serialization of a Boolean type, it can first check whether the Boolean type object is null. If it is null, an empty byte array (0 bytes) is returned; if it is true, a byte array containing 0x01 (1 byte) is returned; if it is false, a byte array containing 0x00 (1 byte) is returned. During deserialization, it can check whether the byte data is empty. If it is empty, null is returned. If it is not empty, the first byte can be read. If the first byte is a first preset byte representation (0x01), true is returned; if the first byte is a second preset byte representation (0x00), false is returned; if the first byte is a value other than the first or second preset byte representation, an exception is thrown.

[0138] In this embodiment of the application, during Boolean serialization, the three possible states of the Boolean object are mapped to different byte sequences, and a 1-byte encoding scheme is adopted. For null values, no bytes are occupied, and for true and false, only a very small 1-byte representation is used, eliminating all metadata and object header overhead. It is suitable for serialization scenarios with a large number of Boolean values; the processing logic is simple, which is beneficial to improving the performance of serialization and deserialization.

[0139] To enable those skilled in the art to better understand the embodiments of this application, the embodiments of this application are described below through a specific example.

[0140] Existing Java serialization schemes mainly include: 1) Java native serialization: Implemented by implementing the Serializable interface, but it has the following disadvantages: the serialized data is large in size and occupies a lot of storage space; the serialization / deserialization performance is low; cross-language compatibility is poor; and there are security risks.

[0141] 2) Third-party serialization frameworks: JSON format (such as Jackson, Gson): good readability, but large data volume and slow parsing speed; Protocol Buffers: a binary serialization protocol developed by Google, with good performance but requires defining .proto files; Kryo: a high-performance Java serialization framework, but with complex configuration and limited support for certain types.

[0142] Therefore, the existing technology has the following problems: 1) Low storage efficiency: Most serialization schemes are not optimized for numeric types, occupy fixed storage space, and cause resource waste.

[0143] 2) Performance bottleneck: The serialization / deserialization process involves a large number of reflection calls and object creations, which affects performance.

[0144] 3) Limited type support: The support for complex types (such as generic collections, enumerations, and custom objects) is not perfect.

[0145] 4) Inefficient handling of null values: The handling of null elements in arrays or collections is not optimized enough, wasting storage space.

[0146] Therefore, embodiments of this application provide an efficient, flexible, and type-safe object serialization scheme.

[0147] Figure 6 This is the second flowchart illustrating the object serialization method provided in this application embodiment. (Refer to...) Figure 6 In one specific embodiment, it can first be determined whether the object to be serialized is null. If the object to be serialized is null, then NULL_OBJECT (i.e., an empty byte array) is returned.

[0148] If the object to be serialized is not null, then the object type is obtained, and the serializer corresponding to the object type is retrieved from the serialization factory. It is then checked whether a serializer corresponding to the object type exists in the cache. If a serializer corresponding to the object type exists in the cache, it is used to perform the serialization operation, and a byte array is returned.

[0149] If the serializer corresponding to the object type does not exist in the cache, a new serializer corresponding to the object type is created. If the object type is an enumeration type, an enumeration type serializer, `EnumSerializer`, is created, and the serialization operation is performed by calling the newly created enumeration type serializer, returning a byte array. If the object type is an array type, an array type serializer, `ArraySerializer`, is created, and the serialization operation is performed by calling the newly created array type serializer, returning a byte array. If the object type is a List type, a List type serializer, `ListSerializer`, is created, and the serialization operation is performed by calling the newly created List type serializer, returning a byte array. If the object type is another type, a serializer of that other type, `ClassSerializer`, is created, and the serialization operation is performed by calling the newly created serializer corresponding to that other type, returning a byte array.

[0150] Specifically, object serialization / deserialization can be implemented based on variable-length integer encoding, that is, for the Integer type, an adaptive variable-length encoding scheme can be used.

[0151] 1. Serialization process: (1) Check if the object is null. If it is null, return NULL_OBJECT.

[0152] (2) Calculate the absolute value and sign bit of the integer.

[0153] (3) Calculate the number of effective bits by bitwise operations (remove the padding bits that are the same in the high bits).

[0154] (4) Select the encoding length (1-5 bytes) based on the number of valid bits.

[0155] (5) Add a length marker bit and a sign bit to the high bits of the encoding result.

[0156] (6) Return the encoded byte array.

[0157] 2. Deserialization process: (1) Read the first byte and determine the encoding format based on the high-order length marker bit.

[0158] (2) Parse the sign bit and data bit according to different encoding formats.

[0159] (3) Reconstruct the original integer and return it.

[0160] Specifically, object serialization / deserialization can be implemented based on a NullFlagList bitmap.

[0161] 1. Construction process: Calculate the number of bitmap bytes required based on the array length (ceil(length / 8)) and initialize the bitmap array.

[0162] 2. Serialization process: Traverse the array, use the setNull() method to mark null elements, and serialize the array length and bitmap data into a byte array.

[0163] 3. Deserialization process: Read the array length from the data stream, read the bitmap data, and use the isNull() method to determine whether each element is null.

[0164] Bitmap operations can include: setNull(int index) sets the specified bit to 1 (indicating null), and isNull(int index) checks if the specified bit is 1.

[0165] Specifically, object serialization / deserialization schemes can employ numerical zero-value optimization.

[0166] 1. During serialization, for numeric types (byte, short, int, long, float, double), the value 0 can be treated as null; it is marked as null in the NullFlagList bitmap.

[0167] 2. During deserialization, based on the bitmap markers, null or zero-value positions are filled with the default value 0.

[0168] Specifically, object serialization can be implemented as follows: 1. Serialization process: (1) Obtain all getter methods of an object through reflection; (2) Sort by attribute name to ensure consistency of serialization order; (3) Use NullFlagList to mark whether the attribute value is null or zero; Serialized data does not contain attribute names and type information; it only saves the data itself. 2. Deserialization process: During deserialization, the setter method is called via reflection to restore the object's state.

[0169] Specifically, object serialization / deserialization can be implemented based on Boolean type serialization.

[0170] 1. Serialization process: (1) Check if the object is null. If it is null, return an empty byte array (0 bytes).

[0171] (2) If true, return a byte array (1 byte) containing 0x01.

[0172] (3) If false, return a byte array (1 byte) containing 0x00.

[0173] 2. Deserialization process: (1) Check if the data is empty. If it is empty, return null.

[0174] (2) Read the first byte.

[0175] (3) If the value is 0x00, return false.

[0176] (4) If it is 0x01, return true.

[0177] (5) Other values ​​throw an exception.

[0178] In summary, compared with the prior art, the embodiments of this application have the following advantages: (1) High storage efficiency: Variable length encoding technology makes small values ​​occupy fewer bytes, saving an average of 30-70% of storage space.

[0179] (2) Excellent performance: The caching mechanism and optimization algorithm improve the serialization / deserialization speed by more than 50%, and the overall performance of serialization / deserialization is improved.

[0180] (3) Type safety: Generic support and type checking ensure compile-time type safety.

[0181] (4) High scalability: The modular design makes it easy to add new types of serializers.

[0182] (5) Null value optimization: Bitmap marking makes null elements in the array take up almost no extra space.

[0183] (6) Good compatibility: Pure Java implementation, no external dependencies, easy to integrate.

[0184] Figure 7 This is a schematic diagram of the object serialization apparatus provided in an embodiment of this application. (Refer to...) Figure 7 This application provides an object serialization apparatus, which may specifically include the following modules: Module 710 is used to obtain the object to be serialized; The length selection module 720 is used to select the target encoding length from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer value wrapper class object when the object to be serialized is an integer value wrapper class object; the effective number of bits refers to the minimum number of bits required to represent an integer value. Encoding module 730 is used to encode integer values ​​based on the target encoding length to obtain an encoded byte array.

[0185] Compared to most existing object serialization schemes that do not optimize for numeric types, resulting in fixed storage space usage, low storage efficiency, and wasted resources, this application's embodiment addresses integer numeric wrapper class objects by employing adaptive variable-length integer encoding technology. This technology automatically selects the target encoding length from a variable-length encoding rule table based on the effective number of digits in the integer value. Encoding the integer value based on the adaptively selected target encoding length reduces the number of bytes occupied by smaller values, thereby decreasing storage space usage and improving serialization storage efficiency. This approach is suitable for efficient storage and transmission of objects in Java applications.

[0186] Figure 8 This is a schematic diagram of the object serialization apparatus provided in an embodiment of this application. (Refer to...) Figure 8 This application provides an object deserialization apparatus, which may specifically include the following modules: The length determination module 810 is used to read the length marker bit in the encoded byte array and determine the target encoding length based on the value of the length marker bit; Parsing module 820 is used to parse the sign bit and data bit in the encoded byte array based on the target encoding length; Reconstruction module 830 is used to reconstruct the original integer value based on the sign bit and data bits, so as to restore the integer wrapper class object based on the original integer value.

[0187] This application's embodiments are adapted to variable-length integer encoding schemes. By parsing the length marker, sign bit, and data bits of the encoded byte array, the original integer value is reconstructed, enabling object deserialization. This avoids complex conditional judgments and redundant operations, resulting in clear and efficient decoding logic and improved deserialization efficiency. Furthermore, after serializing an integer wrapper class object using variable-length integer encoding based on the significant number of digits of the integer value, this application's embodiments restore the integer wrapper class object by executing the corresponding deserialization process. This collectively reduces data size, saves storage space and network bandwidth, and is suitable for efficient storage and transmission of objects in Java applications.

[0188] Figure 9 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 9As shown, the electronic device may include a processor 910, a communication interface 920, a memory 930, and a communication bus 940. The processor 910, communication interface 920, and memory 930 communicate with each other via the communication bus 940. The processor 910 can call logical instructions in the memory 930 to execute the following methods: obtaining the object to be serialized; if the object to be serialized is an integer wrapper class object, selecting a target encoding length from a variable-length encoding rule table based on the effective number of bits of the integer value encapsulated within the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value; and encoding the integer value based on the target encoding length to obtain an encoded byte array. Alternatively, read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit; based on the target encoded length, parse the sign bit and data bit in the encoded byte array; based on the sign bit and data bit, reconstruct the original integer value, and then recover the integer wrapper class object based on the original integer value.

[0189] Furthermore, the logical instructions in the aforementioned memory 830 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to related technologies, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0190] On the other hand, this application discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions, and when the program instructions are executed by a computer, the computer can execute the methods provided in the above-described method embodiments, such as: obtaining an object to be serialized; when the object to be serialized is an integer wrapper class object, selecting a target encoding length from a variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value; and encoding the integer value based on the target encoding length to obtain an encoded byte array. Alternatively, read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit; based on the target encoded length, parse the sign bit and data bit in the encoded byte array; based on the sign bit and data bit, reconstruct the original integer value, and then recover the integer wrapper class object based on the original integer value.

[0191] In another aspect, embodiments of this application also provide a non-transitory computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program is implemented to perform the transmission methods provided in the above embodiments, including, for example,: obtaining an object to be serialized; if the object to be serialized is an integer wrapper class object, selecting a target encoding length from a variable-length encoding rule table based on the effective number of bits of the integer value encapsulated within the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent an integer value; and encoding the integer value based on the target encoding length to obtain an encoded byte array. Alternatively, read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit; based on the target encoded length, parse the sign bit and data bit in the encoded byte array; based on the sign bit and data bit, reconstruct the original integer value, and then recover the integer wrapper class object based on the original integer value.

[0192] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0193] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the parts that contribute to the related technology, can be embodied in the form of software products. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0194] Finally, it should be noted that the above embodiments are only used to illustrate this application and are not intended to limit this application. Although this application has been described in detail with reference to the embodiments, those skilled in the art should understand that various combinations, modifications, or equivalent substitutions of the technical solutions of this application do not depart from the spirit and scope of the technical solutions of this application and should be covered within the scope of the claims of this application.

Claims

1. An object serialization method, characterized in that, include: Get the object to be serialized; When the object to be serialized is an integer wrapper class object, the target encoding length is selected from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value. Based on the target encoding length, the integer value is encoded to obtain an encoded byte array.

2. The object serialization method according to claim 1, characterized in that, The significant digits of the integer value are determined in the following way: Determine the absolute value of the integer value; Bitwise operations are used to calculate the minimum number of bits required to represent the absolute value of the integer value, thus obtaining the effective number of bits of the integer value.

3. The object serialization method according to claim 1, characterized in that, The process of encoding the integer value based on the target encoding length to obtain the encoded byte array includes: Determine the sign bit of the integer value; Based on the target encoding length, the effective data bits of the integer value are subjected to variable-length encoding to obtain a variable-length encoding result; A length marker bit and the sign bit are added to the high bits of the variable-length encoding result to obtain the encoded byte array; the value of the length marker bit is determined based on the target encoding length.

4. The object serialization method according to claim 1, characterized in that, If the object to be serialized is a wrapper class object, and the primitive type value encapsulated within the wrapper class object is zero, the method further includes: Bitmap markers are applied to zero-value fields in wrapper class objects to obtain marked data. The marked data and non-zero value values ​​are serialized into byte arrays.

5. The object serialization method according to claim 1, characterized in that, When the object to be serialized is an array type object, the method further includes: Based on the length of the array, determine the required number of bitmap bytes and initialize the bitmap array; each bit of the bitmap array is used to indicate whether the element at the corresponding index position in the array is null; Traverse the array, set the bit corresponding to the null element to the first flag value, and set the bit corresponding to the non-null element to the second flag value to obtain the bitmap data; The length of the array, the bitmap data, and the non-empty elements are serialized into a byte array.

6. The object serialization method according to claim 1, characterized in that, After obtaining the object to be serialized, the process also includes: Obtain all getter methods of the object to be serialized; each getter method corresponds one-to-one with an object property. Sort the property names corresponding to each getter method to obtain the property sorting order; Based on the attribute sorting order, iterate through and call each getter method to obtain the current attribute value; Mark whether the current attribute value is empty or zero to obtain the marked data; The marked data and attribute values ​​that are not empty or zero are serialized into byte arrays.

7. The object serialization method according to claim 1, characterized in that, When the object to be serialized is a boolean type object, the method further includes: If the boolean object is null, an empty byte array is returned; If the boolean value of the boolean type object is true, then a byte array containing the first preset byte representation is returned; If the boolean value of the boolean type object is false, then a byte array containing the second preset byte representation is returned; The first preset byte representation is different from the second preset byte representation.

8. The object serialization method according to claim 1, characterized in that, If it is determined that the object to be serialized is not null, the method further includes: Obtain the object type of the object to be serialized, and obtain the serializer corresponding to the object type from the serializer factory; If the serializer corresponding to the object type does not exist in the cache, a new serializer corresponding to the object type is created based on the object type, the new serializer corresponding to the object type is called, the object to be serialized is converted into a byte array, and the new serializer corresponding to the object type is stored in the cache. If the serializer corresponding to the object type exists in the cache, then the serializer in the cache is called to convert the object to be serialized into a byte array.

9. An object deserialization method, characterized in that, include: Read the length marker bit in the encoded byte array and determine the target encoded length based on the value of the length marker bit; Based on the target encoding length, parse the sign bit and data bit in the encoded byte array; Based on the sign bit and the data bit, the original integer value is reconstructed, and the integer wrapper class object is recovered based on the original integer value.

10. The object deserialization method according to claim 9, characterized in that, The method further includes: Read the array length and bitmap data from the encoded byte array; Based on the value of each bit in the bitmap data, determine whether the element at the corresponding index position in the array is empty, and obtain the empty value judgment result; Based on the array length and the result of the null value determination, the array type object is restored.

11. The object deserialization method according to claim 9, characterized in that, The method further includes: Based on the labeled data, empty and zero-value positions are filled with zero values.

12. The object deserialization method according to claim 9, characterized in that, The method further includes: The object's state is restored by invoking the setter method using reflection.

13. The object deserialization method according to claim 9, characterized in that, The method further includes: If the encoded byte array contains empty bytes, return null. If the first byte of the byte data is represented by a first preset byte, then return true; If the first byte of the byte data is represented by a second preset byte, then return false; If the first byte of the byte data is represented by other bytes, an exception is thrown.

14. An object serialization apparatus, characterized in that, include: The acquisition module is used to acquire the object to be serialized; The length selection module is used to select a target encoding length from the variable-length encoding rule table based on the effective number of bits of the integer value encapsulated inside the integer value wrapper class object when the object to be serialized is an integer value wrapper class object; the effective number of bits refers to the minimum number of bits required to represent the integer value. The encoding module is used to encode the integer value based on the target encoding length to obtain an encoded byte array.

15. An object deserialization apparatus, characterized in that, include: The length determination module is used to read the length marker bit in the encoded byte array and determine the target encoding length based on the value of the length marker bit; The parsing module is used to parse the sign bit and data bit in the encoded byte array based on the target encoding length; The reconstruction module is used to reconstruct the original integer value based on the sign bit and the data bit, so as to restore the integer value wrapper class object based on the original integer value.

16. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the object serialization method as described in any one of claims 1 to 8, or the object deserialization method as described in any one of claims 9 to 13.

17. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the computer program implements the object serialization method as described in any one of claims 1 to 8, or the object deserialization method as described in any one of claims 9 to 13.

18. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the object serialization method as described in any one of claims 1 to 8, or the object deserialization method as described in any one of claims 9 to 13.