A data serialization method, device, equipment, and storage medium
By defining the serialization function in the preset base class and calling the variable storage function to obtain the variable name and value of member variables, the associated storage and serialization processing are performed to solve the low efficiency and easy development efficiency of data serialization methods in existing technologies. The problem of errors achieves efficient and concise data serialization.
Patent Information
- Application Number
- CN202110624032.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-04
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2041-06-04
AI Technical Summary
In the prior art, data serialization methods are inefficient in development, error-prone, and difficult to maintain. Especially in the front-end and back-end separation architecture in the field of financial technology, repeated code development in serialized formats accounts for a lot of work.
By define serialization functions in the preset base class, and call the variable storage function in this serialization function to obtain the variable name and value of member variables, perform associated storage and serialized processing to achieve data serialization.
Simplify the development process of data serialization, improve development efficiency, reduce error rate and maintain difficulty, and reduce the writing of duplicate code.
Smart Images

Figure CN113326032B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to but are not limited to the field of computer technology, and in particular to a data serialization method and apparatus, device, and storage medium. Background Art
[0002] With the development of computer technology, more and more technologies are applied in the financial field. The traditional financial industry is gradually transforming to financial technology (Fintech). However, due to the security and real-time requirements of the financial industry, financial technology also puts forward higher requirements for technology. In the field of financial technology, the application system in the relevant technology usually adopts a front-end and back-end separation architecture, and the serialized format is used for data interaction between the front-end and the back-end. In addition, different back-end services often need to call service interfaces. In the process of calling the interface for data transmission, the serialized format is usually used for data transmission. Therefore, in the process of application system development, a large part of the work needs to be spent on developing and debugging interfaces based on the serialized format. In the data serialization method in the related technology, a lot of duplicate code needs to be written in the process of developing each interface based on the serialized format, which has low development efficiency, is prone to errors, and is difficult to maintain. Summary of the invention
[0003] In view of this, the embodiments of the present application provide a data serialization method and apparatus, device, and storage medium to solve at least one problem existing in the related art.
[0004] The technical solution of the embodiment of the present application is implemented as follows:
[0005] On the one hand, an embodiment of the present application provides a data serialization method, the method comprising:
[0006] Get at least one member variable of an instance of a preset class;
[0007] The serialization function defined in the base class of the preset class is called by using the instance; wherein the following steps are performed during the calling of the serialization function:
[0008] Calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0009] Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data.
[0010] On the other hand, an embodiment of the present application provides a data serialization device, the device comprising:
[0011] An acquisition module, used to acquire at least one member variable of an instance of a preset class;
[0012] A serialization module is used to use the instance to call a serialization function defined in a base class of the preset class; wherein the following steps are performed during the calling of the serialization function: calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and to store the variable name and value of each member variable in an associated manner, wherein the variable name of each member variable is determined based on a custom reflection mechanism; and serialization processing is performed based on the associated stored variable name and value of each member variable to obtain serialized data.
[0013] On the other hand, an embodiment of the present application provides a data serialization device, including a memory and a processor, wherein the memory stores a computer program that can be executed on the processor, and the processor implements the steps in the above method when executing the program.
[0014] On the other hand, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and the computer program implements the steps in the above method when executed by a processor.
[0015] In an embodiment of the present application, on the one hand, based on a custom reflection mechanism, the variable name of at least one member variable in a preset class can be determined, and then the variable name and value of each member variable can be stored in association, and the variable name and value of each member variable stored in association are serialized to obtain the serialized data; on the other hand, a serialization function for implementing data serialization is defined in the base class of the preset class, and the variable storage function declared in the base class and defined in the preset class is called in the serialization function to obtain the variable name and value of at least one member variable of the instance of the preset class, and the variable name and value of each member variable are stored in association, and then the variable name and value of each member variable stored in association are serialized in the serialization function to obtain the serialized data. In this way, since the variable storage function can be declared in the base class and defined in the preset class inherited from the base class, the associated storage of the variable name and value of the corresponding member variable can be realized in the base class according to different subclasses, and then the serialization of the instance can be realized simply and quickly by using the instance of the preset class inherited from the base class to call the serialization function defined in the base class. In addition, when developing a preset class, you only need to focus on obtaining the values of each member variable in the instance of the preset class. There is no need to develop a large amount of repetitive and error-prone code such as assignment and serialization of serialized objects. This can improve the development efficiency of the first service, reduce the error rate in the development process, and reduce the difficulty of maintaining the application system. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1A A schematic diagram of the interface development process in the relevant technology;
[0017] Figure 1B A schematic diagram of an implementation flow of a data serialization method provided in an embodiment of the present application;
[0018] Figure 2 A schematic diagram of an implementation flow of a data serialization method provided in an embodiment of the present application;
[0019] Figure 3 A schematic diagram of an implementation flow of a data serialization method provided in an embodiment of the present application;
[0020] Figure 4A A schematic diagram of an implementation framework of a data interaction interface provided in an embodiment of the present application;
[0021] Figure 4B A schematic diagram of a memory structure corresponding to m_vmapData data provided in an embodiment of the present application;
[0022] Figure 4CA schematic diagram of an implementation flow of obtaining data from a data container and calling a JSON serialization method provided in an embodiment of the present application;
[0023] Figure 4D A schematic diagram of an implementation flow of obtaining data from m_vmapData and calling a JSON serialization method provided in an embodiment of the present application;
[0024] Figure 4E A schematic diagram of an implementation process of obtaining data from m_vJsonData and calling a JSON serialization method provided in an embodiment of the present application;
[0025] Figure 5 This is a schematic diagram of the structure of the data serialization device according to an embodiment of the present application;
[0026] Figure 6 A hardware entity diagram of a data serialization device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0027] In order to make the purpose, technical solutions and advantages of the present application clearer, the technical solutions of the present application are further elaborated in detail below in conjunction with the drawings and embodiments. The described embodiments should not be regarded as limiting the present application. All other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.
[0028] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0029] If similar descriptions of "first / second" appear in the application documents, the following instructions are added. In the following description, the terms "first\second\third" involved are merely used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged in a specific order or sequence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0030] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0031] The technical solution of the present application is further described in detail below with reference to the accompanying drawings and embodiments.
[0032] Before further describing the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are explained. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.
[0033] 1) Macro: In computer science, a macro is an abstract pattern that replaces text according to a set of predefined rules, such as the #define function defined in the C++ language. The interpreter or compiler will automatically replace the pattern when encountering a macro. For compiled languages, during the compilation phase, the preprocessor will expand the macro references that appear in the source file into corresponding macro definitions;
[0034] 2) Base class: In object-oriented design, a base class is defined as the class type that contains the commonalities of all entities;
[0035] 3) Derived class: Using the inheritance mechanism, a new class can be derived from an existing class. The derived new class is called a derived class. A derived class can inherit from a base class and have all the common properties of the base class. A derived class is also called a subclass;
[0036] 4) Pure virtual function: A pure virtual function is a virtual function declared in a base class. Any subclass of the base class must implement the function to achieve polymorphism.
[0037] 5) Polymorphism: It is a major feature of object-oriented programming languages. The core concept of polymorphism is to access functions defined in subclasses through base classes. There are many ways to implement polymorphism, such as pure virtual functions.
[0038] 6) Reflection mechanism: It mainly refers to the ability of a program to access, detect and modify its own state or behavior. The reflection mechanism has many capabilities, such as the ability of a base class to obtain subclass member variables and member methods;
[0039] 7) JSON (JavaScript Object Notation): It is a lightweight data exchange format that is easy for humans to read and write, and easy for machines to parse and generate. It can effectively improve network transmission efficiency.
[0040] 8) key / value: key-value pair. key is the keyword and value is the value;
[0041] 9) Serialization: refers to converting data with a certain structure into a form that can be accessed or transmitted, such as into a character stream. After serialization, the data can be transmitted in various logical modules of the system or in the network.
[0042] In order to better understand the data serialization method provided in the embodiment of the present application, the data serialization scheme in the related art is first described below.
[0043] Take the implementation of the JSON-based interface between two backend services as an example. Figure 1A In the related art, the development of each interface includes an offline process 10 and a program development process 20, wherein the developer can define the interface fields according to the interface requirements in the offline process 10; in the program development process 20, the interface program can be developed based on the interface fields defined in the offline process 10. When the obtained interface program is executed, the following steps can be implemented:
[0044] Step S21, system initialization;
[0045] Step S22, obtaining the values of all fields;
[0046] Here, if it is a service requester, it is necessary to obtain the value of each request field according to the defined request field. If it is a service responder, it is necessary to obtain the value of each response field according to the defined response field.
[0047] Step S23, calling the JSON object assignment statement: by calling the JSON object assignment statement, the interface fields can be added to the JSON object in sequence as the fields of the JSON object, and each field in the JSON object is assigned a corresponding field value;
[0048] Step S24, calling the JSON serialization method: by calling the JSON serialization method, the JSON object can be serialized into a JSON string;
[0049] Step S25, request / respond to the other party's service: send the JSON string to the other party's service through the service request interface provided by the other party's service, or return the JSON string to the other party's service when responding to the request of the other party's service.
[0050] The data serialization scheme of the above-mentioned related technology has the following problems: 1) Low development efficiency. Since the implementation of each interface is independent, the above steps need to be implemented in each interface. Therefore, when developing multiple interfaces, a large amount of repetitive code needs to be written. For a small or medium-sized system, the number of interfaces provided is generally dozens, and the average number of fields in each interface is dozens. For larger systems, the number of interfaces often reaches hundreds. Therefore, the scheme has a large amount of repetitive work, resulting in low development efficiency; 2) It is error-prone and difficult to maintain. When calling JSON object assignment, developers need to manually match the field name of the interface with the variable used to store the interface field value in the interface class, which is error-prone and difficult to detect. In addition, subsequent modifications to the interface fields also need to be performed simultaneously in multiple places, making the interface program difficult to maintain.
[0051] The embodiment of the present application provides a data serialization method, which can be executed by a first service running on a computer device. The computer device may refer to a server, a laptop, a tablet computer, a desktop computer, a smart TV, a set-top box, a mobile device (such as a mobile phone, a portable video player, a personal digital assistant, a dedicated messaging device, a portable gaming device), or other device with data processing capabilities. Figure 1B The following is a schematic diagram of the implementation process of the data serialization method of the present application embodiment. Figure 1B As shown, the method includes:
[0052] Step S101, obtaining at least one member variable of an instance of a preset class;
[0053] Here, the preset class can be any suitable class in an object-oriented programming language, and the instance of the preset class is the instance that currently needs to be serialized, and the instance has one or more member variables. For an instance of a preset class, each member variable of the instance has a corresponding value. During implementation, the value of each member variable of the instance can be obtained in any suitable manner, which is not limited here. For example, the value of each member variable can be a set constant, or it can be calculated through a set calculation process, or it can be obtained by querying a database. In some embodiments, each member variable of the instance can directly correspond to each interface field that the first service needs to interact with other services.
[0054] The at least one member variable may be all member variables of the instance, or may be part of the member variables of the instance that need to be serialized, which is not limited here.
[0055] Step S102, using the instance to call a serialization function defined in a base class of the preset class; wherein the following steps are performed during the calling of the serialization function:
[0056] Calling a variable storage function declared in the base class and defined in the preset class, wherein the variable storage function is used to obtain a variable name and a value of each member variable, and associate and store the variable name and the value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0057] Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data.
[0058] Here, the serialization function may be a function or method defined in the base class that can be called by a subclass. Since the preset class inherits from the base class, the instance of the preset class can call the serialization function. For example, when the programming language used by the first service is C++, the serialization function may be a function of public or protected permissions in the base class.
[0059] The serialization function can obtain the variable name and value of each member variable that needs to be serialized in the instance by calling the variable storage function, and store the variable name and value of each member variable in an associated manner, and then perform serialization processing based on the variable name and value of each member variable stored in an associated manner to achieve serialization of the instance. During implementation, the variable storage function can be a pure virtual function declared in the base class, and each subclass of the base class needs to define the implementation process of the variable storage function. Due to the polymorphism in object-oriented programming languages, when the serialization function is called using an instance of a preset class, the variable storage function called in the serialization function is implemented based on the definition in the preset class.
[0060] The variable name of each member variable can be determined based on a custom reflection mechanism. In some embodiments, a custom reflection mechanism can be implemented using macros defined in a base class. For example, assuming that the variable name of a member variable x of an instance is to be obtained, a macro can be defined in the base class: #define varName(x)#x, and varName(x) is introduced at the code where the variable name of the member variable needs to be obtained in the implementation of the variable storage function defined in the preset class. In this way, since the function of # in the C++ macro is to stringify the macro parameter behind it, the varName(x) in the preset class can be automatically replaced with the string "x" by expanding the macro during the compilation phase of the application of the first service, so that the variable storage function can be called in the base class to obtain the variable name of each member variable that needs to be serialized in the instance, and then the variable name and value of each member variable can be obtained.
[0061] In some embodiments, a registration method may be provided by the base class, and the instance of the preset class may register the member variable name of the instance to the base class by calling the registration method, thereby implementing a custom reflection mechanism. For example, the base class may provide a registration method register(), and in the code where the variable name of the member variable needs to be obtained in the implementation of the variable storage function defined in the preset class, the registration method may be called by register("x", x), thereby registering the variable name of the member variable x to the base class.
[0062] Since the value of each member variable of the instance is stored in the memory when a value is assigned, the value of each member variable can be obtained through address reference, pointer addressing, etc., which is not limited here.
[0063] After obtaining the variable name and value of each member variable, the variable name and value of each member variable can be matched one by one through associative storage. The variable name and value of each member variable can be stored in an address accessible to the base class, such as a member variable of the base class, a set file or a database, which is not limited here. During implementation, the variable name and value of each member variable can be associated and stored in any suitable manner, which is not limited here. For example, the variable name and value of each member variable can be associated and stored in the form of a key-value pair. For each member variable, the variable name of the member variable can be used as the keyword of the key-value pair, and the value of the member variable can be used as the value of the key-value pair; the variable name and value of each member variable can also be associated and stored in the form of a string. For each member variable, the variable name and value of the member variable can be concatenated into a string in a suitable manner, and the string corresponding to each member variable is stored.
[0064] Since there is a one-to-one correspondence between the variable name and the value of each member variable in the associated storage, serialization processing can be performed based on each variable name and value pair. During implementation, those skilled in the art can use a suitable serialization format to serialize the variable name and value of each member variable in the associated storage according to actual needs to obtain data in the corresponding serialization format. The serialization format used may include, but is not limited to, JSON format, eXtensible Markup Language (XML) format, etc.
[0065] In some embodiments, the customized reflection mechanism is implemented by using macro instructions defined in the base class, and the variable name of each member variable is obtained by expanding the macro instructions during the compilation phase of the application of the first service.
[0066] In an embodiment of the present application, on the one hand, based on a custom reflection mechanism, the variable name of at least one member variable in a preset class can be determined, and then the variable name and value of each member variable can be associated and stored, and serialization processing is performed based on the variable name and value of each member variable stored in association, so as to obtain serialized data; on the other hand, a serialization function for implementing data serialization is defined in a base class of the preset class, and a variable storage function declared in the base class and defined in the preset class is called in the serialization function to obtain the variable name and value of at least one member variable of an instance of the preset class, and the variable name and value of each member variable are associated and stored, and then serialization processing is performed in the serialization function based on the variable name and value of each member variable stored in association, so as to obtain serialized data. In this way, since the variable storage function can be declared in the base class and defined in the preset class inherited from the base class, the variable name and value of the corresponding member variable can be automatically associated and stored according to different subclasses in the base class, and then the serialization of the instance can be realized simply and quickly by using the instance of the preset class inherited from the base class to call the serialization function defined in the base class. In addition, when developing a preset class, you only need to pay attention to the acquisition of the value of each member variable in the instance of the preset class, and there is no need to develop a large amount of repetitive and error-prone code such as assignment and serialization of serialized objects, thereby improving the development efficiency of the first service, reducing the error rate in the development process and the difficulty of maintaining the application system.
[0067] The present application embodiment provides a data serialization method. Figure 2 The following is a schematic diagram of the implementation process of the data serialization method of the present application embodiment. Figure 2 As shown, the method is applied to the first service, including:
[0068] Step S201, when it is determined that an instance of a preset class needs to be sent to a second service, obtaining at least one member variable of the instance;
[0069] Here, the second service is any suitable service that needs to perform data interaction with the first service. The first service and the second service interact based on data in a serialized format.
[0070] An instance of a preset class can be request data that needs to be carried when the first service sends a data request to the second service, or it can be response data that needs to be returned to the second service when the first service responds to the data request of the second service, which is not limited here. Each member variable of the instance can correspond to each interface field that the first service needs to interact with the second service. The value of each member variable can be a set constant, or it can be calculated through a set calculation process, or it can be obtained by querying a database, which is not limited here.
[0071] During implementation, it can be determined whether it is necessary to send an instance of the preset class to the second service by monitoring whether the set function is called or whether the set parameters are received. For example, when the first service sends a data request to the second service, or when the first service responds to the data request of the second service, the serialization of the instance of the preset class can be triggered by calling the set function to obtain the serialized data. When it is monitored that the function is called, it can be determined that it is necessary to send the instance of the preset class to the second service. For another example, when the first service sends a data request to the second service, or when the first service responds to the data request of the second service, it can be triggered by calling the set function of the target instance and transmitting to the function the parameters that represent the need for data serialization. When it is monitored that the parameters are received, it can be determined that it is necessary to send the instance of the preset class to the second service.
[0072] Step S202, using the instance to call a serialization function defined in a base class of the preset class; wherein the following steps are performed during the calling of the serialization function:
[0073] Calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0074] Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data;
[0075] The serialized data is sent to the second service.
[0076] Here, the serialized data may be the interaction data carried when the first service interacts with the second service. The serialized data may be the request data that the first service needs to carry when initiating a data request to the second service. The first service may send the serialized data to the second service when initiating a data request to the second service. The serialized data may also be the response data that the first service needs to return to the second service when responding to the data request of the second service. The first service may send the serialized data to the second service when responding to the data request of the second service.
[0077] During implementation, developers can predefine the format of each field carried in the interactive data when the first service interacts with the second service. Each field in the interactive data can correspond to a member variable in the preset class. The variable name of the member variable is the field name of the corresponding field, and the value of the member variable is the value of the corresponding field. For example, the format of each request field carried in the request data when the first service initiates a data request to the second service can be predefined, and the field name of each request field can be directly used as the variable name of the member variable in the preset class; or the field name of each response field carried in the response data when the first service responds to the data request of the second service, and the field name of each response field can be directly used as the variable name of the member variable in the preset class.
[0078] In an embodiment of the present application, when it is determined that an instance of a preset class needs to be sent to a second service, at least one member variable of the instance is obtained, and the serialized data is sent to the second service in the serialization function. In this way, the member variable name of the instance can be directly used as the field name of the field carried in the interactive data when the first service interacts with the second service. Since the variable name of each member variable of the instance is automatically associated with the value in the base class, the field name of each field in the interactive data can be automatically associated with the value. There is no need for developers to assign the values of the member variables of the instance to the fields in the interactive data by writing code, thereby reducing the situation where code errors lead to interaction errors between the first service and the second service. In addition, sending the serialized data to the second interface during the process of being called in the serialization function defined in the base class can further reduce the workload of writing a large amount of repetitive code in each subclass of the base class, thereby further improving the development efficiency of the first service.
[0079] The present application embodiment provides a data serialization method. Figure 3 The following is a schematic diagram of the implementation process of the data serialization method of the present application embodiment. Figure 3 As shown, the method includes:
[0080] Step S301, when a data sending function of an instance of a preset class is called, determining that the instance needs to be sent to a second service; wherein the data sending function of the instance is used to send the instance to the second service in a serialized format;
[0081] Here, the data sending function may be any suitable function defined in the preset class, and may be used to send an instance of the preset class to the second service in a serialized format. When the data sending function is called, it may be determined that the instance needs to be sent to the second service. In implementation, any suitable serialization format may be used when sending the instance, and the serialization format may be user-defined or system-default, which is not limited here.
[0082] In some embodiments, the serialization format includes at least one of the following: JSON format, XML format.
[0083] Step S302, in response to a call operation on a data sending function of the instance, obtaining at least one member variable of the instance;
[0084] Here, calling the data sending function of the instance can trigger the process of sending the instance in a serialized format to the second service. Therefore, after the data sending function is called, at least one member variable of the instance can be obtained in response to the calling operation of the data sending function of the instance.
[0085] Step S303, using the instance to call the serialization function defined in the base class of the preset class; wherein the following steps are performed during the calling of the serialization function:
[0086] Calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0087] Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data;
[0088] The serialized data is sent to the second service.
[0089] In the embodiment of the present application, when the data sending function of an instance of a preset class is called, it is determined that the instance needs to be sent to the second service, and in response to the calling operation of the data sending function of the instance, at least one member variable of the instance is obtained, and the serialization function defined in the base class of the preset class is called using the instance. In this way, the process of sending the instance to the second service in a serialized format can be triggered by calling the data sending function of the instance.
[0090] The present invention provides a data serialization method, which includes:
[0091] Step S401, obtaining at least one member variable of an instance of a preset class;
[0092] Step S402, using the instance to call the serialization function defined in the base class of the preset class; wherein the following steps S421 to S422 are performed during the calling of the serialization function:
[0093] Step S421, calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0094] Step S422, serialization processing is performed based on the variable name and value of each member variable stored in association to obtain serialized data.
[0095] During implementation, the above step S422 may include:
[0096] Step S422a, determining a serialization object that matches the set serialization format based on the variable name and value of each member variable stored in association;
[0097] Here, the serialization format set may be set by a developer or user according to actual needs, for example, may include but is not limited to JSON format, XML format, etc., which is not limited here.
[0098] The serialized object is an object that matches the serialization format. For example, when the serialization format is set to JSON format, the serialized object that matches the serialization format may be a JSON object. For another example, when the serialization format is set to XML format, the serialized object that matches the serialization format may be an XML object.
[0099] There is a one-to-one correspondence between the variable name and value of each member variable in the associated storage. Based on the set serialization format, each set of corresponding variable names and values can be used to assign values to the initialized serialized object. For example, when the serialized object is a JSON object, each set of corresponding variable names and values can be added to the JSON object as a key-value pair.
[0100] Step S422b: serialize the serialized object based on the serialization format to obtain serialized data.
[0101] Here, in the case of determining the serialization format used for formatting, the serialized object can be serialized based on the serialization format. During implementation, a third-party serialization function that matches the serialization format can be called to process the serialized object to obtain serialized data, or a custom serialization method can be used to serialize the serialized object based on the serialization format, which is not limited here. For example, for a JSON object, a third-party JSON serialization function can be used to convert the JSON object into a corresponding JSON string, and the obtained JSON string is the serialized data.
[0102] In the embodiment of the present application, based on the variable name and value of each member variable stored in association, a serialized object matching the set serialization format is determined, and based on the serialization format, the serialized object is serialized to obtain the serialized data. In this way, the serialized data can be obtained simply and quickly based on the variable name and value of each member variable stored in association.
[0103] The present invention provides a data serialization method, which includes:
[0104] Step S501, obtaining at least one member variable of an instance of a preset class;
[0105] Step S502, using the instance to call the serialization function defined in the base class of the preset class; wherein the following steps S521 to S522 are performed during the calling of the serialization function:
[0106] Step S521, calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism;
[0107] Step S522, serialization processing is performed based on the variable name and value of each member variable stored in association to obtain serialized data.
[0108] During implementation, the following steps may be performed during the process of calling the variable storage function:
[0109] Step S521a, obtaining the variable name and value of each member variable;
[0110] Step S521b, storing the variable name and value of each member variable in the form of a key-value pair in a data container.
[0111] Here, the variable name and value of each member variable can be associated and stored in the form of a key-value pair. For each member variable, the variable name of the member variable can be used as the keyword of the key-value pair, and the value of the member variable can be used as the value of the key-value pair to obtain a key-value pair.
[0112] The data container may be any suitable container that can store multiple sets of data, including but not limited to lists, arrays, vectors, etc. In implementation, the data container may be a container that supports storing a single data type, or a container that supports storing different data types, which is not limited here.
[0113] Each set of data stored in the data container may be a key-value pair set including at least one key-value pair, or may be a serialized object, each serialized object including at least one key-value pair. Those skilled in the art may use a suitable data structure to store the variable name and value of each member variable in the data container in the form of a key-value pair according to actual conditions, which is not limited here.
[0114] In some embodiments, the at least one member variable includes multiple groups of member variables, and the above step S521b includes: storing the variable name and value of each member variable in a data container in the form of a key-value pair; wherein each element in the data container includes a group of member variable names and values. Here, the variable name and value of each group of member variables can be stored in a key-value pair set or in a serialized object, which is not limited here.
[0115] In some embodiments, the variable storage function includes a first variable storage function, the data container includes a first data container, each element in the first data container is a key-value pair set, and the following steps can be performed during the calling of the first variable storage function: step S531, obtaining the variable name and value of each member variable; step S532, storing the variable name and value of each member variable in the form of a key-value pair in the key-value pair set; step S533, storing the key-value pair set in the first data container.
[0116] In some embodiments, the following steps are further performed during the calling of the first variable storage function: Step S534, setting the instance type identifier to the first type. Here, the instance type identifier is an identifier that characterizes the value type of the member variables that need to be serialized in the instance. The first type can characterize that the value type of the member variables that need to be serialized in the instance is a string, that is, the variable name and value of each group of member variables can be stored in a key-value pair set, and the variable name and value of each member variable can be obtained from the first data container.
[0117] The above step S522 may include:
[0118] Step S541, when it is determined that the instance type identifier is the first type, traverse the elements in the first data container to obtain at least one key-value pair set;
[0119] Step S542, for each of the key-value pair sets, based on the variable name and value of each of the member variables in the key-value pair set, determining a serialization object that matches the set serialization format;
[0120] Here, for each key-value pair in the key-value pair set, a serialized object initialized with a variable name and a value in each key-value pair can be assigned a value based on a set serialization format. For example, if the serialized object is a JSON object, each key-value pair can be added to the JSON object.
[0121] Step S543: Based on the serialization format, serialization processing is performed on each of the serialization objects to obtain serialized data.
[0122] In some embodiments, the variable storage function includes a second variable storage function, the data container includes a second data container, each element in the second data container is a serialized object matching a set serialization format, and the following steps may be performed during the calling of the second variable storage function:
[0123] Step S551, obtaining the variable name and value of each member variable;
[0124] Step S552, obtaining a serialized object matching the serialization format;
[0125] Here, a new serialized object may be created based on the serialization format, or an existing serialized object may be initialized to obtain a serialized object that matches the serialization format, which is not limited here.
[0126] Step S553, storing the variable name and value of each member variable in the form of a key-value pair in the serialized object;
[0127] Step S554: store the serialized object in the second data container.
[0128] In some embodiments, the following steps are further performed during the calling of the second variable storage function: Step S555, setting the instance type identifier to the second type. Here, the second type may indicate that the value type of the member variables to be serialized in the instance is a serialized object, that is, the variable name and value of each group of member variables may be stored in the form of a serialized object, and the variable name and value of each member variable may be obtained from the second data container.
[0129] The above-mentioned step S522 may include: step S561, when it is determined that the instance type identifier is the second type, traversing the elements in the second data container to obtain at least one serialized object that matches the serialization format; step S562, based on the serialization format, serializing each of the serialized objects to obtain serialized data.
[0130] In some embodiments, the serialization format includes at least one of the following: JSON format, XML format.
[0131] In the embodiment of the present application, the variable name and value of each member variable are obtained during the process of the variable storage function being called, and the variable name and value of each member variable are stored in the data container in the form of a key-value pair. In this way, the variable name and value of each member variable can be associated and stored simply and quickly.
[0132] The following is an example of using the C++ programming language to implement an interface for data interaction based on the JSON format to further illustrate the embodiment of the present application. The embodiment of the present application provides a data serialization method, which is applied to the first service and can be used to implement a data interaction interface. The interface implements a certain degree of reflection mechanism at the bottom of the framework, so that the bottom of the framework can obtain all member variables of the upper caller. The bottom of the framework also implements the JSON object assignment, JSON object serialization, request / response second service and other processes in the data JSON serialization process. Based on this data serialization method, in the process of developing any interface class, the interface class can inherit from the base class of the bottom of the framework. In the interface class, you only need to pay attention to how to obtain all the field values of this interface, and then call the macro instruction to implement a pure virtual function of the base class, so that the entire interface development can be completed quickly, which is convenient and fast, and there will be no errors in field names and variable binding.
[0133] Figure 4A A schematic diagram of an implementation framework of a data interaction interface provided in an embodiment of the present application, such as Figure 4A As shown, the implementation framework of the data interaction interface includes a base class 61 and an interface subclass 62. The base class 61 provides a header file containing specific macro instructions, based on which a reflection mechanism can be implemented. The interface subclass 62 inherits from the base class 61 and implements the pure virtual function genDataMap of the base class 61. The reflection mechanism implemented by the macro instructions defined in the header file of the base class is described below.
[0134] The base class defines the following variables in the header file:
[0135] A map whose key and value are both string types, denoted as m_mData. Map is an associative container of STL (Chinese Standard Template Library), which can provide one-to-one key-value pair hashing. The key in each key-value pair can only appear once in the map.
[0136] A vector of type map, denoted as m_vmapData, where the key and value of the map are both string types. A vector is a sequential container that encapsulates a dynamically sized array and can store objects of various types;
[0137] A vector of JSON objects, denoted as m_vJsonData;
[0138] A JSON object, denoted as m_jData.
[0139] For general interface subclasses, all fields for data interaction are of string type. Therefore, the following macros can be defined in the header file of the base class:
[0140] #define varName(x)#x, used to obtain the stringified value of variable x. In C++ macros, the function of "#" is to perform string operations on the macro variable behind it;
[0141] #define StoreMap(x)m_mData[varName(x)]=x, used to add the variable name and value of variable x to m_mData;
[0142] #define StoreMap2(x,y)do{x=y;m_mData[varName(x)]=x;}while(0), used to assign the value of variable y to x, and add the variable name and value of variable x to m_mData;
[0143] #define PushMap do{if(m_mData.size()>0){m_dataFormat=MAP_FORMAT;m_vmapData.push_back(m_mData);m_mData.clear();}else{Log("m_mData empty");return sERROR;}}while(0), used to add m_mData to m_vmapData, where m_dataFormat is used by the base class to determine what format of interface the interface subclass calls to distinguish whether the field type of the subclass is a string or a complex sequence of nested JSON, and MAP_FORMAT indicates that the interface subclass calls PushMap to store variable names and values.
[0144] For more complex interface subclasses, there are nested JSON object types in the fields for data interaction. Therefore, the following macro instructions can also be defined in the header file of the base class:
[0145] #define StoreJson(x)m_jData[varName(x)]=x, used to add the variable name and value of variable x to m_jData;
[0146] #define PushJson do{if(!m_jData.empty()){m_dataFormat=JSON_FORMAT;m_vJsonData.push_back(m_jData);m_jData.clear();}else{Log("m_jData empty");return sERROR;}}while(0), used to add m_jData to m_vJsonData, JSON_FORMAT indicates that the interface subclass calls PushJson to store variable names and values.
[0147] Since the C++ programming language itself does not support reflection functions and does not store variable names, variable names will not appear in the program code after compilation, so the base class cannot obtain member variables of the subclass in C++. The embodiment of the present application uses the above macro instructions in combination, and can implement the pure virtual function genDataMap of the base class by calling macro instructions such as StoreMap and PushMap in the interface subclass. In this way, since the compiler will expand the macro instructions during the compilation stage, in the process of macro instruction expansion, the macro #define varName(x)#x is expanded, and the variable name of the variable x can be obtained, and then the variable name and variable value of the interface subclass can be obtained and stored. Finally, the variable name and variable value of the interface subclass will be stored in the base class m_mData in the form of key / value pairs, or stored in the base class m_jData in the form of JSON objects.
[0148] The interface subclass can implement the genDataMap pure virtual function of the base class based on the macro instructions defined in the above base class according to actual needs. For example, the following statement can be used to reference the macro instructions in the base class to implement the genDataMap pure virtual function:
[0149] StoreMap2(v1,getKey1());
[0150] StoreMap2(v2,getKey2());
[0151] …
[0152] StoreMap2(vn,getKeyn());
[0153] Here, member variables v1, v2, ... vn need to be defined in the interface subclass, and the variable names of member variables v1, v2, ... vn need to be consistent with the field names of the interface fields required for data interaction with the interface. During implementation, the field names of the JSON data defined in the interface can be directly used as the variable names of the interface class. If there is a type of nested JSON object in the field of interface data interaction, the macro instruction StoreJson can be called to implement the genDataMap pure virtual function. Since the interface subclass inherits from the base class and implements the pure virtual function genDataMap of the base class, all interface subclasses form polymorphism when the main process function calls genDataMap, and automatically call the genDataMap implemented by the corresponding interface subclass, thereby passing the variables obtained by the subclass to the base class, and then the base class calls the subsequent steps in the main process function to complete the entire interface call process.
[0154] For interface subclasses that need to interact with batch data, there will be multiple groups of interface fields that need to be serialized. The variable name and value of the instance corresponding to each group of field interfaces will be stored in the form of a map array in m_vmapData, or in the form of a JSON object array in m_vJsonData. Figure 4B This is a schematic diagram of the memory structure corresponding to m_vmapData, such as Figure 4B As shown, m_vmapData may include multiple maps (Map1, Map2, Mapn are exemplarily shown in the figure), and each map may include multiple key-value pairs (Key1 / Value1, Key2 / Value2, Keyn / Valuen are exemplarily shown in the figure). For interface subclasses that need to perform batch data interaction, during the process of the base class's genDataMap pure virtual function, the base class's PushMap or PushJson macro instructions can be called as needed to push multiple map data into the base class's m_vmapData, or push multiple JSON objects into the base class's m_vJsonData.
[0155] Continue to see Figure 4A The base class 61 also implements a main process function, which includes the following steps S611 to S614:
[0156] Step S611, initializing the system and interface;
[0157] Step S612, calling the pure virtual function genDataMap;
[0158] Step S613, obtaining data from the data container and calling the JSON serialization method;
[0159] Here, data can be obtained from different data containers according to whether there is a nested JSON object type in the field for data interaction of the interface subclass, so as to correspond the variable name and value of each variable one by one, that is, to bind the interface field and the field value, and call the JSON serialization method to serialize the variable name and value of each variable to obtain the serialized data.
[0160] Figure 4C A schematic diagram of an implementation flow of obtaining data from a data container and calling a JSON serialization method provided in an embodiment of the present application is shown in FIG. Figure 4C As shown, the process includes the following steps S621 to S623:
[0161] Step S621, determine the type of the interface subclass; if the interface subclass is a simple JSON type, proceed to step S622; if the interface subclass is a nested JSON type, proceed to step S623;
[0162] Here, the interface subclasses can be divided into simple JSON types and nested JSON types according to whether there are nested JSON object types in the fields for data interaction of the interface subclasses. The simple JSON type interface subclasses do not have nested JSON object types in the fields for data interaction, and the nested JSON type interface subclasses have nested JSON object types in the fields for data interaction.
[0163] During implementation, the type of the interface subclass can be determined based on the macro instruction called by the interface subclass. If the interface subclass calls the macro instruction PushMap, the interface subclass is determined to be a simple JSON type; if the interface subclass calls the macro instruction PushJson, the interface subclass is determined to be a nested JSON type. During implementation, since the m_dataFormat = MAP_FORMAT statement is executed in the PushMap instruction and the m_dataFormat = JSON_FORMAT statement is executed in the PushJson instruction, the type of the interface subclass can be determined based on the value of m_dataFormat. When the value of m_dataFormat is MAP_FORMAT, the interface subclass can be determined to be a simple JSON type; when the value of m_dataFormat is JSON_FORMAT, the interface subclass can be determined to be a nested JSON type.
[0164] Step S622, obtaining data from m_vmapData and calling the JSON serialization method;
[0165] Here, when the interface subclass is a simple JSON type, data is obtained from m_vmapData and the JSON serialization method is called. In implementation, m_vmapData can be looped through, each set of maps can be taken out from m_vmapData, and then each key / value pair in the map can be obtained by looping through the map, and then the key in the key / value pair is used as the key of the JSON object, and the value is used as the value of the JSON object, thereby obtaining at least one assigned JSON object, and finally the JSON serialization method is called to serialize each JSON object.
[0166] Figure 4D A schematic diagram of an implementation process of obtaining data from m_vmapData and calling a JSON serialization method provided in an embodiment of the present application, the process includes the following steps S631 to S636:
[0167] Step S631, taking out a map from m_vmapData;
[0168] Step S632, taking a key / value pair from the current map;
[0169] Step S633, JSON object [key] = value;
[0170] Step S634, determine whether there are any keys in the current map; if so, return to step S632; if not, go to step S635;
[0171] Step S635, serializing the JSON object;
[0172] Step S636, determine whether there is still map in m_vmapData; if so, return to step S631; if not, end.
[0173] Step S623, obtain data from m_vJsonData and call the JSON serialization method.
[0174] Here, when the interface subclass is a nested JSON type, data is obtained from m_vJsonData and the JSON serialization method is called. During implementation, each JSON object can be taken out from m_vJsonData by looping, and then the JSON serialization method is called to serialize each JSON object.
[0175] Figure 4E A schematic diagram of an implementation process of obtaining data from m_vJsonData and calling a JSON serialization method provided in an embodiment of the present application, the process includes the following steps S641 to S643:
[0176] Step S641, extract the JSON object from m_vJsonData;
[0177] Step S642, serializing the JSON object;
[0178] Step S643, determine whether there is any JSON object in m_vJsonData; if so, return to step S641; if not, end.
[0179] Since the macro instruction varName(x) can obtain the string corresponding to the variable name, the design of map, JSON object and vector can use this string as the key and the value of the corresponding variable as the value. Thus, the pure virtual function genDataMap is called, and the process of obtaining data from the data container automatically realizes the binding of the interface field name and the variable value.
[0180] Step S614, request / respond to the second service.
[0181] Here, requesting / responding to the second service is the process of communication between the first service and the second service. During implementation, a pre-agreed communication method can be adopted according to the actual scenario. In the embodiment of the present application, the process of requesting / responding to the second service is implemented in the base class, so that each interface subclass does not need to separately implement a communication module for communicating with the second service.
[0182] Continue to see Figure 4A The interface subclass 62 can also obtain the values of all interface fields according to the actual situation, and call the main process function of the base class 61 to realize the serialization of the instance of the interface subclass 62. Assuming that n fields need to be obtained, the values of the n fields can be obtained by implementing the value-taking methods of n fields. For example, n variables can be defined in the interface subclass to store the values of the n fields, which are respectively recorded as v1, v2, ... vn. The values of v1, v2, ... vn can be obtained by implementing the value-taking methods of each field, getKey1(), getKey2(), ... getKeyn().
[0183] In some embodiments, the data interaction interface can be implemented in a manner other than the base class + interface subclass framework, as long as the ideas involved in the embodiments of the present application are implemented: using macro definitions to achieve a certain degree of reflection, binding variable values and field names one by one through reflection, and encapsulating the implementation steps into functions for calling, which can also reduce the amount of repetitive work in the interface development process to a certain extent and simplify the development steps.
[0184] In some embodiments, methods other than macro instructions can be used to implement the base class automatically obtaining member variables of the interface subclass. For example, the base class can provide a registration method to register all member variable names of the interface subclass to the base class, and bind the variable name and variable value at the same time of registration, so that the base class can automatically obtain the variable name and variable value of the member variable of the interface subclass.
[0185] The embodiments of the present application have at least the following beneficial effects: 1) A reflection mechanism is implemented in C++ through specific macro instructions, so that the base class can easily obtain all member variables of the interface subclass, and the intrusion on the normal development process of the interface subclass is relatively low. The interface subclass only needs to focus on obtaining the values of all interface fields, and then call the macro instruction to implement a pure virtual function of the base class to quickly complete the entire interface subclass development; 2) Based on the implemented reflection mechanism, the base class automatically implements the binding between the interface field key and the interface subclass member variable value; 3) All JSON object assignments and JSON serialization processes are uniformly implemented by the base class, so that when developing a new interface subclass, you only need to focus on how to obtain the interface field values, and there is no need to perform repetitive and error-prone tasks such as JSON object assignment and JSON serialization.
[0186] Based on the foregoing embodiments, an embodiment of the present application provides a data serialization device, which includes the modules included and the sub-modules included in each module, and can be implemented by a processor in a data serialization device; of course, it can also be implemented by a specific logic circuit; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA), etc.
[0187] Figure 5 A schematic diagram of the composition structure of a data serialization device provided in an embodiment of the present application is shown in the figure. As shown in the figure, the device 700 includes an acquisition module 701 and a serialization module 702, wherein:
[0188] An acquisition module 701 is used to acquire at least one member variable of an instance of a preset class;
[0189] The serialization module 702 is used to use the instance to call the serialization function defined in the base class of the preset class; wherein the following steps are performed during the calling of the serialization function: calling the variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and to store the variable name and value of each member variable in an associated manner, wherein the variable name of each member variable is determined based on a custom reflection mechanism; and serialization processing is performed based on the variable name and value of each member variable stored in an associated manner to obtain serialized data.
[0190] In some embodiments, the customized reflection mechanism is implemented by using macro instructions defined in the base class, and the variable name of each member variable is obtained by expanding the macro instructions during the compilation phase of the application of the first service.
[0191] In some embodiments, the device is applied to a first service, and the acquisition module is also used to: when it is determined that the instance needs to be sent to a second service, obtain at least one member variable of the instance; when the serialization function is called, the following steps are also performed: sending the serialized data to the second service.
[0192] In some embodiments, the device also includes: a determination module, used to determine that the instance needs to be sent to a second service when the data sending function of the instance is called; wherein the data sending function of the instance is used to send the instance to the second service in a serialized format; the acquisition module is also used to: in response to a calling operation on the data sending function of the instance, obtain at least one member variable of the instance.
[0193] In some embodiments, the serialization module is also used to: perform the following steps in the process of calling the serialization function: determine a serialization object that matches the set serialization format based on the variable name and value of each member variable stored in association; and serialize the serialization object based on the serialization format to obtain serialized data.
[0194] In some embodiments, the serialization module is also used to: perform the following steps in the process of calling the serialization function: obtain the variable name and value of each member variable; store the variable name and value of each member variable in the form of a key-value pair in a data container.
[0195] In some embodiments, the at least one member variable includes multiple groups of member variables, and the serialization module is also used to: perform the following steps in the process of calling the serialization function: store the variable name and value of each member variable in a data container in groups in the form of key-value pairs; wherein each element in the data container includes the variable name and value of a group of member variables.
[0196] In some embodiments, the variable storage function includes a first variable storage function, the data container includes a first data container, and each element in the first data container is a key-value pair set; the serialization module is also used to: call the first variable storage function, and the first variable storage function performs the following steps during the calling process: obtain the variable name and value of each member variable; store the variable name and value of each member variable in the form of a key-value pair in a key-value pair set; and store the key-value pair set in the first data container.
[0197] In some embodiments, the serialization module is also used to: perform the following steps in the process of calling the first variable storage function: set the instance type identifier to the first type; perform the following steps in the process of calling the serialization function: when it is determined that the instance type identifier is the first type, traverse the elements in the first data container to obtain at least one key-value pair set; for each of the key-value pair sets, based on the variable name and value of each member variable in the key-value pair set, determine a serialization object that matches the set serialization format; based on the serialization format, serialize each of the serialization objects to obtain serialized data.
[0198] In some embodiments, the variable storage function includes a second variable storage function, the data container includes a second data container, and each element in the second data container is a serialized object that matches the set serialization format; the serialization module is also used to: call the second variable storage function, and the following steps are performed during the calling of the second variable storage function: obtain the variable name and value of each member variable; obtain a serialized object that matches the serialization format; store the variable name and value of each member variable in the form of a key-value pair in the serialized object; and store the serialized object in the second data container.
[0199] In some embodiments, the serialization module is also used to: perform the following steps in the process of calling the second variable storage function: set the instance type identifier to the second type; perform the following steps in the process of calling the serialization function: when it is determined that the instance type identifier is the second type, traverse the elements in the second data container to obtain at least one serialized object that matches the serialization format; based on the serialization format, serialize each of the serialized objects to obtain serialized data.
[0200] In some embodiments, the serialization format includes at least one of the following: JSON format, XML format.
[0201] The description of the above device embodiment is similar to the description of the above method embodiment, and has similar beneficial effects as the method embodiment. For technical details not disclosed in the device embodiment of the present application, please refer to the description of the method embodiment of the present application for understanding.
[0202] It should be noted that in the embodiment of the present application, if the above-mentioned data serialization method is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a data serialization device (which can be a server, or a network device, etc.) to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a disk or an optical disk. In this way, the embodiment of the present application is not limited to any specific combination of hardware and software.
[0203] Correspondingly, an embodiment of the present application provides a data serialization device, including a memory and a processor. The memory stores a computer program that can be run on the processor, and the processor implements the steps in the above method when executing the program.
[0204] Correspondingly, an embodiment of the present application provides a computer-readable storage medium on which a computer program is stored, and when the computer program is executed by a processor, the steps in the above method are implemented.
[0205] It should be noted here that the description of the above storage medium and device embodiments is similar to the description of the above method embodiments, and has similar beneficial effects as the method embodiments. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the description of the method embodiments of this application for understanding.
[0206] It should be noted that Figure 6 A schematic diagram of a hardware entity of a data serialization device in an embodiment of the present application is shown in FIG. Figure 6 As shown, the hardware entity of the data serialization device 800 includes: a processor 801, a communication interface 802 and a memory 803, wherein
[0207] The processor 801 generally controls the overall operation of the data serialization device 800 .
[0208] The communication interface 802 enables the data serialization device to communicate with other terminals or servers through a network.
[0209] The memory 803 is configured to store instructions and applications executable by the processor 801, and can also cache data to be processed or processed by the processor 801 and various modules in the data serialization device 80 (for example, image data, audio data, voice communication data, and video communication data), which can be implemented through flash memory (FLASH) or random access memory (Random Access Memory, RAM).
[0210] It should be understood that "one embodiment" or "an embodiment" mentioned throughout the specification means that specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. It should be understood that in various embodiments of the present application, the size of the sequence number of the above-mentioned processes does not mean the order of execution, and the execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application. The above-mentioned sequence numbers of the embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments.
[0211] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0212] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.
[0213] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units; they may be located in one place or distributed on multiple network units; some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0214] In addition, all functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may be a separate unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0215] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiment; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM), magnetic disks or optical disks, etc., various media that can store program codes.
[0216] Alternatively, if the above-mentioned integrated unit of the present application is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present application can be essentially or partly embodied in the form of a software product that contributes to the relevant technology. The computer software product is stored in a storage medium, including several instructions to enable a data serialization device (which can be a server, a personal computer, a mobile terminal, or a network device, etc.) to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks, or optical disks.
[0217] The above is only an implementation method of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A data serialization method, characterized in that: Applied in the field of financial technology, the method includes: Obtain at least one member variable of an instance of a preset class; the at least one member variable includes some member variables in the instance that need to be serialized; The serialization function defined in the base class of the preset class is called by using the instance; wherein the following steps are performed during the calling of the serialization function: Calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used to obtain the variable name and value of each member variable, and associate and store the variable name and value of each member variable, and the variable name of each member variable is determined based on a custom reflection mechanism; the variable storage function is declared in the base class and defined in the preset class inherited from the base class, and the serialization function defined in the base class is called by using the instance of the preset class inherited from the base class; only the acquisition of the value of each member variable in the instance of the preset class is needed, and there is no need to assign and serialize a large number of serialized objects; The following steps are performed during the calling of the variable storage function: obtaining the variable name and value of each member variable; storing the variable name and value of each member variable in a data container in the form of a key-value pair; the variable storage function includes a first variable storage function, the data container includes a first data container, and each element in the first data container is a key-value pair set, or the variable storage function includes a second variable storage function, the data container includes a second data container, and each element in the second data container is a serialized object matching a set serialization format; Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data; Wherein, in the first service, the step of obtaining at least one member variable of an instance of a preset class includes: In case it is determined that the instance needs to be sent to the second service, obtaining at least one member variable of the instance; Determine whether to send the instance of the preset class to the second service by monitoring whether the set function is called or whether the set parameters are received; the second service is any suitable service that needs to interact with the first service for data; the first service and the second service interact based on data in serialized format; The following steps are also performed during the calling of the serialization function: Send the serialized data to the second service; the serialized data is the request data that the first service needs to carry when sending a data request to the second service, or is the response data that the first service needs to return to the second service when responding to the data request of the second service, and each member variable of the instance corresponds to each interface field that the first service needs to interact with the second service; predefine the format of each field carried in the interaction data when the first service interacts with the second service, each field in the interaction data corresponds to a member variable in a preset class, the variable name of the member variable is the field name of the corresponding field, and the value of the member variable is the value of the corresponding field; Wherein, the method further comprises: When a data sending function of the instance is called, it is determined that the instance needs to be sent to a second service; wherein the data sending function of the instance is used to send the instance to the second service in a serialized format; the data sending function is any suitable function defined in the preset class, and when the data sending function is called, it is determined that the instance needs to be sent to the second service; The step of obtaining at least one member variable of the instance when it is determined that the instance needs to be sent to the second service includes: In response to a calling operation on a data sending function of the instance, at least one member variable of the instance is obtained.
2. The method according to claim 1, characterized in that The customized reflection mechanism is implemented by using the macro instruction defined in the base class, and the variable name of each member variable is obtained by expanding the macro instruction during the compilation phase of the application program of the first service.
3. The method according to claim 1, characterized in that The variable name and value of each member variable stored based on association are serialized to obtain serialized data, including: Determine a serialization object that matches a set serialization format based on the variable name and value of each member variable stored in association; Based on the serialization format, the serialization object is serialized to obtain serialized data.
4. The method according to claim 1, characterized in that The at least one member variable includes a plurality of groups of member variables, and the variable name and value of each member variable are stored in the data container in the form of a key-value pair, including: The variable name and value of each member variable are stored in a data container in groups in the form of key-value pairs; wherein each element in the data container includes a group of variable names and values of the member variables.
5. The method according to claim 1, characterized in that The following steps are performed during the calling of the first variable storage function: Obtain the variable name and value of each member variable; The variable name and value of each member variable are stored in a key-value pair set in the form of a key-value pair; The key-value pair set is stored in the first data container.
6. The method according to claim 5, characterized in that The following steps are also performed during the calling of the first variable storage function: Set the instance type identifier to the first type; The variable name and value of each member variable stored based on association are serialized to obtain serialized data, including: When it is determined that the instance type identifier is the first type, traverse the elements in the first data container to obtain at least one key-value pair set; For each of the key-value pair sets, based on the variable name and value of each of the member variables in the key-value pair set, determine a serialization object that matches the set serialization format; Based on the serialization format, serialization processing is performed on each of the serialization objects to obtain serialized data.
7. The method according to claim 1, characterized in that The second variable storage function is called during the execution of the following steps: Obtain the variable name and value of each member variable; Get a serialized object that matches the serialization format; The variable name and value of each member variable are stored in the serialized object in the form of a key-value pair; The serialized object is stored in the second data container.
8. The method according to claim 7, characterized in that The following steps are further performed during the calling of the second variable storage function: Setting the instance type identifier to the second type; The variable name and value of each member variable stored based on association are serialized to obtain serialized data, including: In the case where it is determined that the instance type identifier is the second type, traversing the elements in the second data container to obtain at least one serialized object matching the serialization format; Based on the serialization format, serialization processing is performed on each of the serialization objects to obtain serialized data.
9. The method according to any one of claims 5 to 7, characterized in that The serialization format includes at least one of the following: JSON format and XML format.
10. A data serialization device, characterized in that: Applied in the field of financial technology, the device comprises: An acquisition module, used to acquire at least one member variable of an instance of a preset class; the at least one member variable includes some member variables in the instance that need to be serialized; A serialization module, used for calling a serialization function defined in a base class of the preset class by using the instance; wherein the following steps are performed during the calling of the serialization function: calling a variable storage function declared in the base class and defined in the preset class; wherein the variable storage function is used for obtaining a variable name and a value of each member variable, and storing the variable name and the value of each member variable in association, and the variable name of each member variable is determined based on a custom reflection mechanism; the variable storage function is declared in a base class and defined in a preset class inherited from the base class, and the serialization function defined in the base class is called by using an instance of the preset class inherited from the base class; only the acquisition of the values of each member variable in the instance of the preset class needs to be paid attention to, and there is no need to assign and serialize a large number of serialized objects; The following steps are performed during the calling of the variable storage function: obtaining the variable name and value of each member variable; storing the variable name and value of each member variable in a data container in the form of a key-value pair; the variable storage function includes a first variable storage function, the data container includes a first data container, and each element in the first data container is a key-value pair set, or the variable storage function includes a second variable storage function, the data container includes a second data container, and each element in the second data container is a serialized object matching a set serialization format; Based on the variable name and value of each member variable in the associated storage, serialization processing is performed to obtain serialized data; Wherein, in the first service, the step of obtaining at least one member variable of an instance of a preset class includes: In case it is determined that the instance needs to be sent to the second service, obtaining at least one member variable of the instance; Determine whether to send the instance of the preset class to the second service by monitoring whether the set function is called or whether the set parameters are received; the second service is any suitable service that needs to interact with the first service for data; the first service and the second service interact based on data in serialized format; In case it is determined that the instance needs to be sent to the second service, obtaining at least one member variable of the instance; The following steps are also performed during the calling of the serialization function: Send the serialized data to the second service; the serialized data is the request data that the first service needs to carry when sending a data request to the second service, or is the response data that the first service needs to return to the second service when responding to the data request of the second service, and each member variable of the instance corresponds to each interface field that the first service needs to interact with the second service; predefine the format of each field carried in the interaction data when the first service interacts with the second service, each field in the interaction data corresponds to a member variable in a preset class, the variable name of the member variable is the field name of the corresponding field, and the value of the member variable is the value of the corresponding field; The device is also specifically used for: When a data sending function of the instance is called, it is determined that the instance needs to be sent to a second service; wherein the data sending function of the instance is used to send the instance to the second service in a serialized format; the data sending function is any suitable function defined in the preset class, and when the data sending function is called, it is determined that the instance needs to be sent to the second service; The step of obtaining at least one member variable of the instance when it is determined that the instance needs to be sent to the second service includes: In response to a calling operation on a data sending function of the instance, at least one member variable of the instance is obtained.
11. A data serialization device, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor executes the program, the steps in the method according to any one of claims 1 to 9 are implemented.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps in the method according to any one of claims 1 to 9 are implemented.
Citation Information
Patent Citations
Ground feature attribute data processing method
CN101944105A
Data processing method and device
CN110275789A