CN112286594B - Object serialization and deserialization method and device, electronic device and medium - Google Patents
Object serialization and deserialization method and device, electronic device and medium Download PDFInfo
- Publication number
- CN112286594B CN112286594B CN202011080680.4A CN202011080680A CN112286594B CN 112286594 B CN112286594 B CN 112286594B CN 202011080680 A CN202011080680 A CN 202011080680A CN 112286594 B CN112286594 B CN 112286594B
- Authority
- CN
- China
- Prior art keywords
- class
- attribute
- data structure
- serialization
- current class
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000000034 method Methods 0.000 title claims abstract description 290
- 238000004590 computer program Methods 0.000 claims abstract description 9
- 230000003068 static effect Effects 0.000 claims description 27
- 230000004044 response Effects 0.000 claims description 16
- 230000015654 memory Effects 0.000 claims description 12
- 230000002441 reversible effect Effects 0.000 claims description 11
- 238000004458 analytical method Methods 0.000 claims description 8
- 230000002159 abnormal effect Effects 0.000 claims description 3
- 238000010586 diagram Methods 0.000 description 7
- 238000004891 communication Methods 0.000 description 6
- 230000005540 biological transmission Effects 0.000 description 4
- 230000003936 working memory Effects 0.000 description 4
- 238000010276 construction Methods 0.000 description 3
- 230000003287 optical effect Effects 0.000 description 3
- 230000002085 persistent effect Effects 0.000 description 3
- 238000012795 verification Methods 0.000 description 3
- 238000003491 array Methods 0.000 description 2
- 238000007667 floating Methods 0.000 description 2
- 238000013507 mapping Methods 0.000 description 2
- 238000012545 processing Methods 0.000 description 2
- 230000006399 behavior Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004364 calculation method Methods 0.000 description 1
- 230000010267 cellular communication Effects 0.000 description 1
- 238000006243 chemical reaction Methods 0.000 description 1
- 230000001351 cycling effect Effects 0.000 description 1
- 238000013500 data storage Methods 0.000 description 1
- 230000001419 dependent effect Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 238000003780 insertion Methods 0.000 description 1
- 230000037431 insertion Effects 0.000 description 1
- 238000007689 inspection Methods 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000001028 reflection method Methods 0.000 description 1
- 239000007787 solid Substances 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/448—Execution paradigms, e.g. implementations of programming paradigms
- G06F9/4488—Object-oriented
- G06F9/4493—Object persistence
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/30—Arrangements for executing machine instructions, e.g. instruction decode
- G06F9/30003—Arrangements for executing specific machine instructions
- G06F9/30076—Arrangements for executing specific machine instructions to perform miscellaneous control operations, e.g. NOP
- G06F9/30087—Synchronisation or serialisation instructions
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Stored Programmes (AREA)
Abstract
The disclosure provides an object serialization and deserialization method and device, electronic equipment and medium, and relates to the technical field of cloud platforms and computer program design. According to one aspect of the disclosure, the object serialization method includes: defining a first method in a class corresponding to the object, wherein the first method comprises the following steps: traversing the object attribute of the current class, and calling a related serialization method to serialize the attribute in sequence, wherein the current class of the first method is the class in which the first method is defined; and invoking a first method defined in the class to which the object corresponds to perform a serialization operation of the object.
Description
Technical Field
The disclosure relates to the technical field of cloud platforms and computer program designs, in particular to methods and equipment for serializing and deserializing objects, electronic equipment and media.
Background
Serialization (Serialization) is the process of converting the state information of an object into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent storage area. Later, the object may be recreated by reading or de-serializing the state of the object from the storage area.
In many existing serialization schemes, the serialization solutions are found from the external view of the object, including reflection methods, object-based attribute access methods, and the like. The reflective scheme has strong expansibility but low efficiency. The object-based attribute access method has higher requirements on the object and breaks the tightness although the efficiency is improved; although reflections are avoided, there is still a loss in efficiency as it still involves the invocation of multiple methods.
Moreover, these methods generally fail to verify whether fields of the attributes are empty. While an associated adapter may be implemented to address the verification of whether fields are nullable based on the properties of the object, this also places high demands on the object itself and breaks the seal.
Thus, looking for a serialized solution from an external perspective of the object, there is some efficiency loss no matter what way it takes; meanwhile, the requirement on the object is high, the original tightness of the object is generally required to be destroyed, and the verification of whether the attribute field can be empty cannot be directly realized.
Disclosure of Invention
According to one aspect of the present disclosure, there is provided an object serialization method including: defining a first method in a class corresponding to an object, wherein the first method comprises the following steps: traversing the object attribute of the current class, and calling a related serialization method to serialize the attribute in turn, wherein the current class of the first method refers to the class in which the first method is defined; and calling a first method defined in the class corresponding to the object to execute the serialization operation of the object.
According to another aspect of the present disclosure, there is provided an object deserialization method including: defining a second method in a class corresponding to the object, wherein the second method comprises the following steps: creating a first data structure; traversing the attribute of the object of the current class to store the serialized serial number corresponding to the attribute and the type of the attribute into the first data structure, wherein the current class of the second method is the class in which the second method is defined; defining a third method in the class corresponding to the object, wherein the third method comprises the following steps: creating a second data structure for storing the serialized sequence number and the serialized sequence value; traversing the attribute of the object of the current class to acquire a corresponding sequence value from the second data structure according to the sequence number of the attribute, wherein the current class of the third method is the class in which the third method is defined; and invoking a second method and a third method defined in the class corresponding to the object to realize the inverse serialization operation of the object.
According to yet another aspect of the present disclosure, there is provided an object serialization apparatus including: a definition unit configured to define a first method in a class corresponding to an object, the first method including: traversing the object attribute of the current class, and calling a related serialization method to serialize the attribute in turn, wherein the current class of the first method refers to the class in which the first method is defined; and a serialization unit configured to call a first method defined in a class corresponding to the object, so as to execute a serialization operation of the object.
According to yet another aspect of the present disclosure, there is provided an object deserialization apparatus including: a first defining unit configured to define a second method in a class corresponding to the object, the second method comprising: creating a first data structure; traversing the attribute of the object of the current class to store the serialized serial number corresponding to the attribute and the type of the attribute into the first data structure, wherein the current class of the second method is the class in which the second method is defined; a second definition unit configured to define a third method in a class corresponding to the object, the third method including: creating a second data structure for storing the serialized sequence number and the serialized sequence value; traversing the attribute of the object of the current class to acquire a corresponding sequence value from the second data structure according to the sequence number of the attribute, wherein the current class of the third method is the class in which the third method is defined; and the reverse serialization unit is configured to call a second method and a third method defined in the class corresponding to the object so as to realize the reverse serialization operation of the object.
According to still another aspect of the present disclosure, there is provided an electronic device including: a processor; and a memory storing a program comprising instructions that when executed by the processor cause the processor to perform the object serialization method described in this disclosure.
According to yet another aspect of the present disclosure, there is provided a computer readable storage medium storing a program comprising instructions that, when executed by a processor of an electronic device, cause the electronic device to perform the object serialization method described in the present disclosure.
According to yet another aspect of the present disclosure, there is provided a computer program product comprising a computer program which, when executed by a processor, implements the object serialization method described in the present disclosure.
According to still another aspect of the present disclosure, there is provided an electronic device including: a processor; and a memory storing a program comprising instructions that when executed by the processor cause the processor to perform the object deserialization method described in this disclosure.
According to yet another aspect of the present disclosure, there is provided a computer readable storage medium storing a program comprising instructions that, when executed by a processor of an electronic device, cause the electronic device to perform the object de-serialization method described in the present disclosure.
According to yet another aspect of the present disclosure, there is provided a computer program product comprising a computer program which, when executed by a processor, implements the object de-serialization method described in the present disclosure.
According to one aspect of the present disclosure, the object serialization method is from an object internal perspective such that the object itself provides relevant serialization capabilities.
These and other aspects of the disclosure will be apparent from and elucidated with reference to the embodiments described hereinafter.
Drawings
The accompanying drawings illustrate exemplary embodiments and, together with the description, serve to explain exemplary implementations of the embodiments. The illustrated embodiments are for exemplary purposes only and do not limit the scope of the claims. Throughout the drawings, identical reference numerals designate similar, but not necessarily identical, elements.
FIG. 1 is a schematic diagram of an application scenario illustrating one object serialization and anti-serialization of an exemplary embodiment;
FIG. 2 is a flow chart illustrating a method of object serialization of an exemplary embodiment;
FIG. 3 is a schematic diagram illustrating an object serialization process of an exemplary embodiment;
FIG. 4 is a flow chart illustrating an object deserialization method of an exemplary embodiment;
FIG. 5 is a schematic diagram illustrating an object deserialization and null check process of an exemplary embodiment;
FIG. 6 is a schematic diagram illustrating an object serialization apparatus of an exemplary embodiment;
FIG. 7 is a schematic diagram illustrating an object deserialization apparatus of an exemplary embodiment; and
FIG. 8 is a block diagram illustrating an exemplary computing device that may be used in connection with the exemplary embodiments.
Detailed Description
In the present disclosure, the use of the terms "first," "second," and the like to describe various elements is not intended to limit the positional relationship, timing relationship, or importance relationship of the elements, unless otherwise indicated, and such terms are merely used to distinguish one element from another. In some examples, a first element and a second element may refer to the same instance of the element, and in some cases, they may also refer to different instances based on the description of the context.
The terminology used in the description of the various illustrated examples in this disclosure is for the purpose of describing particular examples only and is not intended to be limiting. Unless the context clearly indicates otherwise, the elements may be one or more if the number of the elements is not specifically limited. Furthermore, the term "and/or" as used in this disclosure encompasses any and all possible combinations of the listed items.
The technical scheme of the disclosure can be applied to the distributed system environment for data transmission or storage between systems, network communication between a server and a client, and the like, and is not limited herein. As shown in fig. 1, fig. 1 is a schematic diagram of an application scenario illustrating object serialization and deserialization of an exemplary embodiment. In this example, the serialization end is taken as a service end, and the anti-serialization end is taken as a user end for instance. The server side can serialize the data object to be transmitted to obtain serialized data, the serialized data is transmitted to the user side, and the user side performs deserialization operation on the serialized data to obtain a reconstructed data object. Of course, the application scenario is not limited to the listed application scenario, for example, the serialization end is a client end, the deserialization end is a server end, and the client end and the server end may have serialization and deserialization operations, for example, serializing an object into a character string to be stored in a local file, starting to read the character string for the next time, deserializing the character string, and so on. In the existing system or application program, the method provided by the specification can be adopted as long as the serialization and deserialization methods can be executed, and corresponding beneficial effects are obtained.
Before describing the specific steps of the present embodiment in detail, the object serialization method and the deserialization method related to the present technical solution will be briefly described.
Object serialization is a technique of converting object data into another form of persistent data, i.e., converting state information of an object into a form of storage or transmission. During serialization, an object writes its current state to a temporary or persistent storage area, after which the object may be recreated by reading or de-serializing the state of the object from the storage area. Such conversion may be reversible in order to enable the stored and transferred data to be changed back to the object in memory again. The object serialization method and the inverse serialization method are widely used in data transmission and application between networks, and generally need to convert information into a data stream to realize the data transmission and application.
The data object is a mapping of entities such as people, things, objects and the like existing in the objective world in the computer logic. For example, when the object is a book, the attributes of the object include a book number, an author, a publisher, a remark, and the like, after the book is serialized, a binary data stream corresponding to the book can be generated, where the binary data stream has attribute values corresponding to the attributes of the book, so that entities such as people, things, objects, and the like existing in the objective world are represented by a computer language. The data object may be any entity, such as a person, thing, object, etc., that exists in the objective world, for example, the data object may be a User, i.e., user. The object includes at least one attribute, a name of the attribute may be referred to as an attribute name, a value corresponding to the attribute may be referred to as an attribute value, and a type corresponding to the attribute may be referred to as an attribute type. For example, the attribute type may be int (integer type), boolean (long integer type), double (double floating point type), float (single floating point type), short (short integer type), string (string type), and the like.
A class may be a cohesive package of certain metadata that describes the behavior rules of objects that are instances of the class. For example, a User is a class representing a User, and the User is an object of a User class, that is, an instance of the User class, where the User has an attribute userID, an attribute value of 0001, and the attribute value of the User can be converted into serialized data through serialization. The parent class may be a class inherited in a class having an inheritance relationship, the child class may be a class inherited in a class having an inheritance relationship, or the child class may be a class inherited from the parent class.
The existing object serialization schemes all look at the problem of serialization from an external view, and propose some related solutions, which lead to some unnecessary performance loss, and lack empty inspection of fields, so that the robustness is poor.
Thus, according to one aspect of the present disclosure, there is provided a method of object serialization, as shown in fig. 2, the method comprising: defining a first method in a class corresponding to the object, wherein the first method comprises the following steps: defining a first method in a class corresponding to an object, wherein the first method comprises the following steps: traversing the object attribute of the current class, and calling a related serialization method to serialize the attribute in turn, wherein the current class of the first method refers to the class in which the first method is defined (step 210); and invoking a first method defined in the class to which the object corresponds to perform a serialization operation of the object (step 220).
According to one aspect of the present disclosure, the object serialization method is from an object internal perspective such that the object itself provides relevant serialization capabilities.
In some examples, if the class has a parent, the first method is defined in its parent, including the parent of its parent, and so on. If the class has a subclass, then the first method need not be defined in its subclass.
According to some embodiments, the serialization method according to the present disclosure further comprises: and responding to the existence of a parent class of the class corresponding to the object, defining a first method in the parent class, and calling the first method defined in the parent class of the class corresponding to the object before calling the first method defined in the class corresponding to the object.
According to some embodiments, the first method is a non-static method. The non-static method is also called an instantiation method, a static keyword is not added, memory is not occupied when a class is defined, and the memory is allocated only when the class is instantiated into an object by the object calling the method. Non-static methods may invoke both static members or methods and other non-static members or methods.
In an object serialization method according to the present disclosure, which is designed based on Gson and Java under the Json serialization standard, taking the object corresponding class a as an example, a non-static method writeJson (Gson, jsonWriter), i.e., the first method, is first added to the class a. If class A has a parent, then add static methods writeJson in all its parents (Gson, jsonWriter). writeJson are all internal logic: 1) If the current class has a parent class, calling a homonymy method writeJson of the parent class, and firstly completing serialization operation of the parent class; 2) Traversing the attribute of the object in the current class, and calling JsonWriter the related serialization method to perform serialization operation on the attribute, wherein the current class refers to the class in which the writeJson method is defined.
In an embodiment of an object serialization process according to the present disclosure designed under the Json serialization standard and based on Gson and Java, a JsonAdapter adapter is first constructed. As shown in fig. 3, in step 1, the adapter calls beginObject method JsonWritter to complete the initialization operation of serialization; in step 2, the writeJson method of class a is invoked to complete the serialization of its internal properties in turn, the internal logic of the writeJson method is as described above, the process corresponds to steps 3 to 11 in fig. 3, in step 3, for example, the writeXX (filedA, keyA) method is invoked to write the property name and the property value in step 4, and in step 5, the writing operation is further performed; in step 6, for example, call writeXX (filedB, keyB) the method to write the attribute name and attribute value in step 7, further performing the write operation in step 8; steps 9-11, etc. repeat the above steps to further perform write operations on other attributes of the object; in step 12, jsonAdapter the adapter invokes the endObject method of JsonWritter to complete the serialized ending job. The writeJson method is an object's own method, so no reflection and attribute-dependent access methods are required. It is because without these additional operations that serialization according to the methods of the present disclosure is more efficient; meanwhile, the tightness of the original attribute field is not required, and the application range is wider than FastJson, moshi and the like.
According to another aspect of the present disclosure, there is provided an object deserializing method, as shown in fig. 4, comprising: defining a second method in a class corresponding to the object, wherein the second method comprises the following steps: creating a first data structure; and traversing the attributes of the object of the current class to store the serialized sequence number corresponding to the attribute and the type of the attribute in the first data structure, wherein the current class of the second method is the class in which the second method is defined (step 410); defining a third method in the class corresponding to the object, wherein the third method comprises the following steps: creating a second data structure for storing the serialized sequence number and the serialized sequence value; and traversing the attributes of the objects of the current class to obtain corresponding sequence values from the second data structure according to the sequence numbers of the attributes, wherein the current class of the third method refers to the class in which the third method is defined (step 420); and invoking a second method and a third method defined in the class to which the object corresponds to achieve a reverse serialization operation of the object (step 430).
In some examples, if the class has a parent, the second method and the third method are defined in its parent, including the parent of its parent, and so on. If the class has a subclass, then the second method and the third method need not be defined in its subclass.
In some examples, the first data structure is a map object. The map object is used to save key-value pairs and can remember the original insertion order of the keys. Any value (object or original value) may be used as a key or a value.
According to some embodiments, the anti-serialization method according to the present disclosure further comprises: in response to the class corresponding to the object having a parent class, defining a second method and a third method in the parent class, wherein the second method further comprises: in response to the existence of a parent class in a current class, before traversing the attribute of an object of the current class, calling a second method defined in the parent class of the current class to acquire information in a first data structure corresponding to the parent class, and storing the information in the first data structure created by calling the second method in the current class; and the third method further comprises: in response to the current class having a parent class, invoking a third method defined in the parent class of the current class before invoking the third method defined in the current class.
According to some embodiments, the third method is a non-static method and the second method is a static method. Static methods are methods that add static keywords, which have been loaded and assigned at the time of class definition. Only static members or methods can be called in the static method, and non-static methods or non-static members cannot be called; while non-static methods may invoke both static members or methods and other non-static members or methods. Static methods, like static variables, belong to the class itself, and not to an object of that class. The memory space of the static method is fixed, which is relatively more resource-saving.
According to some embodiments, the second method further comprises: traversing the attribute of the object of the current class, storing the nullable information corresponding to the attribute into the first data structure, and checking whether the attribute is nullable in the reverse serialization process based on the nullable information.
According to some embodiments, the second data structure is a hash table (HashMap). HashMap is implemented based on the Map interface of a hash table, each element being a value-Key pair, i.e. a data structure accessed directly from a Key value. It accesses records by mapping key values to a location in the table to speed up the lookup.
According to some embodiments, invoking the second method and the third method defined in the class to which the object corresponds to implement the inverse serialization operation of the object comprises: acquiring a serial number of the object in the class after serialization; calling the second method to obtain a first data structure returned by the second method; acquiring corresponding information stored in the first data structure according to the serial number to analyze; sequentially obtaining the sequence value of the object after serialization according to the analysis result; calling the third method, and storing the serial number and the serial value into the second data structure; and after all the serialized serial numbers and serial values of the objects are obtained, invoking the third method to perform deserialization operation according to the second data structure.
According to some embodiments, sequentially obtaining the serialized sequence values of the object according to the parsing result includes: and responding to the analysis that the attribute cannot be null but the acquired sequence value is null, terminating the deserialization operation, and throwing out the abnormal information.
In some examples, in an object deserialization method according to the present disclosure that is designed based on Gson and Java with the Json serialization standard, taking the object corresponding class a as an example, the static method Map < String, field > getEfficiencyJsonFields (), the second method, is added in the class a. If class ClassA has a parent, then the static method Map < String, field > getEfficiencyJsonFields () is added to all its parents. The internal logic of the Map < String, field > getEfficiencyJsonFields () method is: 1) Creating a Map < String, field > object, storing information of attribute type, whether attribute Field can be empty, etc. in the file, and providing relevant json parsing capability, 2) if there is a parent class currently, calling getEfficiencyJsonFields () of the parent class of the current class to obtain Map < String, field > information corresponding to the parent class, and storing the Map object created in the first step (namely, map object created in the current class); 3) Traversing the attribute of the current class, obtaining the serialization key (namely the serial number after serialization) corresponding to the attribute, the attribute type and whether the attribute can be empty or not, and storing the information into the Map object created in the first step; 4) Returning to the Map object created in the first step, wherein the current class refers to a class in which the Map < String, field > getEfficiencyJsonFields () method is defined. And, adding a non-static method ClassA (Gson, hashMap < String, value >) to class ClassA, namely a third method. If class ClassA has parents, then add non-static method ClassA to all its parents (Gson, hashMap < String, value >). The internal logic of ClassA (Gson, hashMap < String, value >) is: 1) If the current class has a parent class, invoking a constructor of the parent class to transmit Gson parameters, namely HashMap < String, value > and firstly completing the construction of the parent class; 2) Traversing the attribute of the object of the current class, acquiring a corresponding value (namely a serialized sequence value) from the HashMap according to the serialized key of the attribute, wherein the value represents the value corresponding to the key, and ensuring that the assigned attribute is of an empty type safety according to whether the attribute can be empty for performing related safety check before assigning the value to the related attribute; 3) After all attribute initialization is completed, if there is no exception, the construction of the object is completed, wherein the current class refers to the class in which the class a (Gson, hashMap < String, value >) method is defined.
In an embodiment of an object deserialization process according to the present disclosure designed under the Json serialization standard and based on Gson and Java, a JsonAdapter adapter is first constructed. As shown in fig. 5, in step 1, the adapter call JsonReader completes beginObject the initialization operation of deserialization; in step 2, the adapter calls nextName method JsonReader to obtain a serialized key; in step 3, acquiring relevant fields from the Map returned by the object getEfficiencyJsonFields method according to the serialization key; in step 4, analyzing the Field, calling a corresponding method according to the analyzed attribute type to obtain a corresponding serialization value in step 5, and storing the serialization key and the serialization value in the HashMap; cycling the steps 3 to 5 until all the attribute analysis is completed; in step 6, the adapter calls endObject method JsonReader to complete the deserialization ending; in step 7, a ClassA (Gson, hashMap < String, value >) constructor is invoked, which receives a HashMap set, the method specific logic is as described above, wherein initialization of the attributes and null security check of the attributes can be completed; the verification of whether the serialization value can be empty or not can be performed according to the empty information obtained through analysis; in step 8, the Parser Parser is invoked to read the attribute values in turn and further check whether it can be empty.
The core step of the whole deserialization is to call a constructor of the object for receiving the HashMap set, and the constructor directly completes the assignment operation and the null check logic of the attribute. The access method of the internal assignment of the object constructor without reflection and dependency attribute has higher efficiency; the internal private attribute of the object constructor has no problem of inaccessible, so the tightness of the object is not required, and the application range of the adapter is wider than that of FastJson and Moshi; and the internal object of the constructor knows whether the fields can be empty or not, so that relevant empty judgment can be made before the attribute is assigned, if abnormal, the construction process is terminated, and the stability of the system is improved.
In some examples, after receiving the HashMap set, the serialization key and the serialization value are obtained, and the serialization value is assigned to the correlation attribute to complete the object deserialization operation. In the assignment process, whether the attribute can be empty or not can be checked by whether the serialization value can be empty or not.
According to the object serialization and deserialization method, the stability and the robustness of the application are greatly improved, and the throughput of a system is improved; the waiting time of the user is reduced through improving the efficiency, and the user experience is improved.
According to yet another aspect of the present disclosure, as shown in fig. 6, there is provided an object serialization apparatus 600 including: a defining unit 610, configured to define a first method in a class corresponding to an object, where the first method includes: traversing the object attribute of the current class, and calling a related serialization method to serialize the attribute in turn, wherein the current class of the first method refers to the class in which the first method is defined; and a serialization unit 620 configured to invoke a first method defined in a class corresponding to the object, to perform a serialization operation of the object.
According to some embodiments, serialization apparatus 600 according to the present disclosure further includes a response unit configured to define a first method in a parent class of a class corresponding to the object in response to the existence of the parent class, and wherein the first method defined in the parent class of the class corresponding to the object is invoked before the first method defined in the class corresponding to the object is invoked.
Here, the operations of the above units 610 to 620 of the object serialization apparatus 600 are similar to those of the steps 210 to 220 described above, respectively, and are not repeated here.
According to yet another aspect of the present disclosure, as shown in fig. 7, an object deserialization apparatus 700 includes: the first defining unit 710 is configured to define a second method in a class corresponding to the object, where the second method includes: creating a first data structure; traversing the attribute of the object of the current class to store the serialized serial number corresponding to the attribute and the type of the attribute into the first data structure, wherein the current class of the second method is the class in which the second method is defined; a second defining unit 720, configured to define a third method in the class corresponding to the object, where the third method includes: creating a second data structure for storing the serialized sequence number and the serialized sequence value; traversing the attribute of the object of the current class to acquire a corresponding sequence value from the second data structure according to the sequence number of the attribute, wherein the current class of the third method is the class in which the third method is defined; and a reverse serialization unit 730 configured to invoke the second method and the third method defined in the class corresponding to the object, so as to implement the reverse serialization operation of the object.
According to some embodiments, the anti-serialization apparatus 700 according to the present disclosure further includes a response unit configured to define a second method and a third method in a parent class in response to the class corresponding to the object existing in the parent class, wherein the second method further includes: in response to the existence of a parent class in a current class, before traversing the attribute of an object of the current class, calling a second method defined in the parent class of the current class to acquire information in a first data structure corresponding to the parent class, and storing the information in the first data structure created by calling the second method in the current class; and the third method further comprises: in response to the current class having a parent class, invoking a third method defined in the parent class of the current class before invoking the third method defined in the current class.
According to some embodiments, the second method further comprises: traversing the attribute of the object of the current class, storing the nullable information corresponding to the attribute into the first data structure, and checking whether the attribute is nullable in the reverse serialization process based on the nullable information.
According to some embodiments, the anti-serialization unit is configured to perform the following operations: acquiring a serial number of the object in the class after serialization; calling the second method to obtain a first data structure returned by the second method; acquiring corresponding information stored in the first data structure according to the serial number to analyze; sequentially obtaining the sequence value of the object after serialization according to the analysis result; calling the third method, and storing the serial number and the serial value into the second data structure; and after all the serialized serial numbers and serial values of the objects are obtained, invoking the third method to perform deserialization operation according to the second data structure.
Here, the operations of the above units 710 to 730 of the object deserializing apparatus 700 are similar to the operations of the steps 410 to 430 described above, respectively, and are not repeated here.
According to still another aspect of the present disclosure, there is provided an electronic device including: a processor; and a memory storing a program comprising instructions that when executed by the processor cause the processor to perform the object serialization method described in this disclosure.
According to yet another aspect of the present disclosure, there is provided a computer readable storage medium storing a program comprising instructions that, when executed by a processor of an electronic device, cause the electronic device to perform the object serialization method described in the present disclosure.
According to still another aspect of the present disclosure, there is provided an electronic device including: a processor; and a memory storing a program comprising instructions that when executed by the processor cause the processor to perform the object deserialization method described in this disclosure.
According to yet another aspect of the present disclosure, there is provided a computer readable storage medium storing a program comprising instructions that, when executed by a processor of an electronic device, cause the electronic device to perform the object de-serialization method described in the present disclosure.
With reference to fig. 8, a computing device 2000 will now be described, which is an example of a hardware device (electronic device) that may be applied to aspects of the present disclosure. The computing device 2000 may be any machine configured to perform processes and/or calculations and may be, but is not limited to, a workstation, a server, a desktop computer, a laptop computer, a tablet computer, a personal digital assistant, a robot, a smart phone, an on-board computer, or any combination thereof. The above-described object serialization method and/or object deserialization method may each be implemented, in whole or at least in part, by computing device 2000 or a similar device or system.
The computing device 2000 may include elements that are connected to the bus 2002 (possibly via one or more interfaces) or that communicate with the bus 2002. For example, computing device 2000 may include a bus 2002, one or more processors 2004, one or more input devices 2006, and one or more output devices 2008. The one or more processors 2004 may be any type of processor and may include, but are not limited to, one or more general purpose processors and/or one or more special purpose processors (e.g., special processing chips). Input device 2006 may be any type of device capable of inputting information to computing device 2000 and may include, but is not limited to, a mouse, a keyboard, a touch screen, a microphone, and/or a remote control. The output device 2008 may be any type of device capable of presenting information and may include, but is not limited to, a display, speakers, video/audio output terminals, vibrators, and/or printers. Computing device 2000 may also include a non-transitory storage device 2010, or any storage device that is connected to non-transitory storage device 2010, which may be non-transitory and that may enable data storage, and may include, but is not limited to, a magnetic disk drive, an optical storage device, a solid state memory, a floppy disk, a flexible disk, a hard disk, a magnetic tape, or any other magnetic medium, an optical disk or any other optical medium, a ROM (read only memory), a RAM (random access memory), a cache memory, and/or any other memory chip or cartridge, and/or any other medium from which a computer may read data, instructions, and/or code. The non-transitory storage device 2010 may be detached from the interface. The non-transitory storage device 2010 may have data/program (including instructions)/code for implementing the methods and steps described above. Computing device 2000 may also include a communication device 2012. The communication device 2012 may be any type of device or system that enables communication with external devices and/or with a network, and may include, but is not limited to, modems, network cards, infrared communication devices, wireless communication devices, and/or chipsets, such as bluetooth (TM) devices, 1302.11 devices, wiFi devices, wiMax devices, cellular communication devices, and/or the like.
Computing device 2000 may also include a working memory 2014, which may be any type of working memory that may store programs (including instructions) and/or data useful for the operation of processor 2004 and may include, but is not limited to, random access memory and/or read-only memory devices.
Software elements (programs) may reside in the working memory 2014 including, but not limited to, an operating system 2016, one or more application programs 2018, drivers, and/or other data and code. Instructions for performing the above-described methods and steps may be included in one or more applications 2018, and the above-described object serialization method and/or object deserialization method may each be implemented by reading and executing instructions of one or more applications 2018 by the processor 2004. More specifically, for example, in the above-described object serialization method, steps 210 to 220 may be implemented, for example, by the processor 2004 executing the application 2018 having the instructions of steps 210 to 220. Further, other steps in the above-described object serialization method and/or object deserialization method may be implemented, for example, by the processor 2004 executing an application 2018 having instructions to perform the corresponding steps. Executable code or source code of instructions of software elements (programs) may be stored in a non-transitory computer readable storage medium (such as storage device 2010 described above) and, when executed, may be stored (possibly compiled and/or installed) in working memory 2014. Executable code or source code for instructions of software elements (programs) may also be downloaded from a remote location.
It should also be understood that various modifications may be made according to specific requirements. For example, custom hardware may also be used, and/or particular elements may be implemented in hardware, software, firmware, middleware, microcode, hardware description languages, or any combination thereof. For example, some or all of the disclosed methods and apparatus may be implemented by programming hardware (e.g., programmable logic circuits including Field Programmable Gate Arrays (FPGAs) and/or Programmable Logic Arrays (PLAs)) in an assembly language or hardware programming language such as VERILOG, VHDL, c++ using logic and algorithms according to the present disclosure.
It should also be appreciated that the components of computing device 2000 may be distributed over a network. For example, some processes may be performed using one processor while other processes may be performed by another processor remote from the one processor. Other components of computing system 2000 may also be similarly distributed. As such, computing device 2000 may be construed as a distributed computing system that performs processing in multiple locations.
Although embodiments or examples of the present disclosure have been described with reference to the accompanying drawings, it is to be understood that the foregoing methods, systems, and apparatus are merely exemplary embodiments or examples, and that the scope of the present invention is not limited by these embodiments or examples but only by the claims following the grant and their equivalents. Various elements of the embodiments or examples may be omitted or replaced with equivalent elements thereof. Furthermore, the steps may be performed in a different order than described in the present disclosure. Further, various elements of the embodiments or examples may be combined in various ways. It is important that as technology evolves, many of the elements described herein may be replaced by equivalent elements that appear after the disclosure.
Claims (12)
1. A method of deserializing an object, comprising:
Defining a second method in a class corresponding to the object to be de-sequenced, wherein the second method comprises the following steps: creating a first data structure; traversing the attribute of the object of the current class to store the serialized serial number corresponding to the attribute and the type of the attribute into the first data structure, wherein the current class of the second method is the class in which the second method is defined;
Defining a third method in a class corresponding to the object, wherein the third method comprises the following steps: creating a second data structure for storing the serialized sequence number and the serialized sequence value; traversing the attribute of the object of the current class to acquire a corresponding sequence value from the second data structure according to the sequence number of the attribute, wherein the current class of the third method is the class in which the third method is defined; and
Invoking the second method and the third method defined in the class corresponding to the object to realize the reverse serialization operation of the object, including:
Acquiring a serial number of the object in the class after serialization;
calling the second method to obtain a first data structure returned by the second method;
acquiring corresponding information stored in the first data structure according to the serial number to analyze;
sequentially obtaining the sequence value of the object after serialization according to the analysis result;
Calling the third method, and storing the serial number and the serial value into the second data structure; and
And after all the serialized serial numbers and serial values of the object are obtained, invoking the third method to perform deserialization operation according to the second data structure.
2. The method of claim 1, further comprising: in response to the class corresponding to the object itself having a parent class, defining a second method and a third method in the parent class, wherein,
The second method further comprises: in response to the existence of a parent class in a current class, before traversing the attribute of an object of the current class, calling a second method defined in the parent class of the current class to acquire information in a first data structure corresponding to the parent class, and storing the information in the first data structure created by calling the second method in the current class; and
The third method further comprises: in response to the current class having a parent class, invoking a third method defined in the parent class of the current class before invoking the third method defined in the current class.
3. The method of claim 1, wherein the third method is a non-static method and the second method is a static method.
4. The method of claim 1, the second method further comprising: traversing the attribute of the object of the current class, storing the nullable information corresponding to the attribute into the first data structure, and checking whether the attribute is nullable in the reverse serialization process based on the nullable information.
5. The method of claim 1, wherein the second data structure is a hash table.
6. The method of claim 1, wherein sequentially obtaining the serialized sequence values of the object according to the parsing result comprises:
and responding to the analysis that the attribute cannot be null but the acquired sequence value is null, terminating the deserialization operation, and throwing out the abnormal information.
7. An object deserialization apparatus comprising:
A first defining unit configured to define a second method in a class corresponding to an object itself to be deserialized, the second method comprising: creating a first data structure; traversing the attribute of the object of the current class to store the serialized serial number corresponding to the attribute and the type of the attribute into the first data structure, wherein the current class of the second method is the class in which the second method is defined;
A second definition unit configured to define a third method in a class corresponding to the object itself, the third method including: creating a second data structure for storing the serialized sequence number and the serialized sequence value; traversing the attribute of the object of the current class to acquire a corresponding sequence value from the second data structure according to the sequence number of the attribute, wherein the current class of the third method is the class in which the third method is defined; and
The deserializing unit is configured to call the second method and the third method defined in the class corresponding to the object to realize the deserializing operation of the object, and comprises the following steps:
Acquiring a serial number of the object in the class after serialization;
calling the second method to obtain a first data structure returned by the second method;
acquiring corresponding information stored in the first data structure according to the serial number to analyze;
sequentially obtaining the sequence value of the object after serialization according to the analysis result;
Calling the third method, and storing the serial number and the serial value into the second data structure; and
And after all the serialized serial numbers and serial values of the object are obtained, invoking the third method to perform deserialization operation according to the second data structure.
8. The apparatus of claim 7, further comprising a response unit configured to define a second method and a third method in a parent class in response to the class corresponding to the object itself being present in the parent class, wherein,
The second method further comprises: in response to the existence of a parent class in a current class, before traversing the attribute of an object of the current class, calling a second method defined in the parent class of the current class to acquire information in a first data structure corresponding to the parent class, and storing the information in the first data structure created by calling the second method in the current class; and
The third method further comprises: in response to the current class having a parent class, invoking a third method defined in the parent class of the current class before invoking the third method defined in the current class.
9. The apparatus of claim 7, the second method further comprising: traversing the attribute of the object of the current class, storing the nullable information corresponding to the attribute into the first data structure, and checking whether the attribute is nullable in the reverse serialization process based on the nullable information.
10. An electronic device, comprising:
A processor; and
A memory storing a program comprising instructions that when executed by the processor cause the processor to perform the method of any one of claims 1-6.
11. A computer readable storage medium storing a program, the program comprising instructions which, when executed by a processor of an electronic device, cause the electronic device to perform the method of any one of claims 1-6.
12. A computer program product comprising a computer program, wherein the computer program, when executed by a processor, implements the method of any of claims 1-6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011080680.4A CN112286594B (en) | 2020-10-10 | 2020-10-10 | Object serialization and deserialization method and device, electronic device and medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011080680.4A CN112286594B (en) | 2020-10-10 | 2020-10-10 | Object serialization and deserialization method and device, electronic device and medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112286594A CN112286594A (en) | 2021-01-29 |
CN112286594B true CN112286594B (en) | 2024-07-12 |
Family
ID=74421889
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202011080680.4A Active CN112286594B (en) | 2020-10-10 | 2020-10-10 | Object serialization and deserialization method and device, electronic device and medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112286594B (en) |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113138755A (en) * | 2021-04-19 | 2021-07-20 | 上海逸动医学科技有限公司 | JSON serialization and deserialization optimization method and system |
CN113553033B (en) * | 2021-06-24 | 2022-04-19 | 厦门正航软件科技有限公司 | Object serialization and deserialization method, device and computer readable storage medium |
Family Cites Families (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101661391B (en) * | 2009-09-24 | 2013-08-28 | 金蝶软件(中国)有限公司 | Object serializing method, object deserializing method, device and system |
CN107038022B (en) * | 2015-07-13 | 2021-01-29 | 阿里巴巴集团控股有限公司 | Deserialization method and deserialization device |
CN105573758A (en) * | 2015-12-16 | 2016-05-11 | 福建星网智慧科技股份有限公司 | Dynamic byte-code compilation technology-based codejson frame |
CN107451237B (en) * | 2017-07-26 | 2020-09-15 | 阿里巴巴集团控股有限公司 | Serialization and deserialization method, device and equipment |
US10467321B2 (en) * | 2017-09-07 | 2019-11-05 | Mastercard International Incorporated | Systems and methods for dynamic C# serialization |
CN109388402A (en) * | 2018-09-26 | 2019-02-26 | 杭州米加科技股份有限公司 | Lightweight Josn format analytical tool based on Android platform |
CN110490724A (en) * | 2019-07-03 | 2019-11-22 | 平安证券股份有限公司 | The storage method and device of account data |
CN111752547A (en) * | 2020-06-30 | 2020-10-09 | 武汉虹信技术服务有限责任公司 | Method for modifying Java serialized object serial number and application thereof |
-
2020
- 2020-10-10 CN CN202011080680.4A patent/CN112286594B/en active Active
Also Published As
Publication number | Publication date |
---|---|
CN112286594A (en) | 2021-01-29 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN108572818A (en) | A kind of user interface rendering intent and device | |
US9378237B2 (en) | Methods for in-place access of serialized data | |
US10983803B2 (en) | Annotating dynamic libraries for multi-OS applications | |
CN112286594B (en) | Object serialization and deserialization method and device, electronic device and medium | |
US11941078B2 (en) | Set operations using multi-core processing unit | |
CN109597825B (en) | Rule engine calling method, device, equipment and computer readable storage medium | |
CN109033456B (en) | Condition query method and device, electronic equipment and storage medium | |
CN110888972A (en) | Sensitive content identification method and device based on Spark Streaming | |
US8539512B2 (en) | Transactional environments for event and data binding handlers | |
KR101392046B1 (en) | Method for simplifying interfaces having dynamic libraries | |
CN116541336B (en) | Software running method of multi-core chip and coprocessor | |
CN111797278A (en) | Method for mapping associated object and relation | |
WO2023143545A1 (en) | Resource processing method and apparatus, electronic device, and computer-readable storage medium | |
CN114564241B (en) | Method and device for accessing hardware device, computer device and storage medium | |
CN113626001B (en) | API dynamic arrangement method and device based on script | |
CN116225450A (en) | Text conversion method and apparatus, computing device and readable storage medium | |
CN111857662B (en) | Programming method for describing specific constitution of object based on MAP and interface | |
CN116414689A (en) | Interface parameter verification method and system based on reflection mechanism | |
CN114911541A (en) | Configuration information processing method and device, electronic equipment and storage medium | |
CN114138507B (en) | Python program service method, device and computer readable storage medium | |
CN112256455A (en) | Method, medium and electronic device for optimizing transaction remote procedure call | |
US7430487B2 (en) | System and method for implementing a programmable DMA master with data checking utilizing a drone system controller | |
CN116185371B (en) | Hardware device registration method, device, equipment and storage medium | |
CN118427199B (en) | Multi-language document self-adaptive adaptation method and device | |
CN112068814A (en) | Method, device, system and medium for generating executable file |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |