WO2015100652A1 - Method and apparatus for performing conversion among serialized messages - Google Patents

Method and apparatus for performing conversion among serialized messages Download PDF

Info

Publication number
WO2015100652A1
WO2015100652A1 PCT/CN2013/091193 CN2013091193W WO2015100652A1 WO 2015100652 A1 WO2015100652 A1 WO 2015100652A1 CN 2013091193 W CN2013091193 W CN 2013091193W WO 2015100652 A1 WO2015100652 A1 WO 2015100652A1
Authority
WO
WIPO (PCT)
Prior art keywords
data
description item
serialized
item
xml
Prior art date
Application number
PCT/CN2013/091193
Other languages
French (fr)
Chinese (zh)
Inventor
吕明伟
Original Assignee
北京新媒传信科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 北京新媒传信科技有限公司 filed Critical 北京新媒传信科技有限公司
Priority to CN201380081227.7A priority Critical patent/CN105793842B/en
Priority to PCT/CN2013/091193 priority patent/WO2015100652A1/en
Publication of WO2015100652A1 publication Critical patent/WO2015100652A1/en

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor

Definitions

  • the present invention relates to the field of data processing technologies, and in particular, to a method and apparatus for converting between serialized messages.
  • Message serialization is an important part of distributed systems. It is responsible for message delivery between service clusters like blood.
  • Serialization protocols commonly found in message serialization include self-explanatory serialization protocols such as XML (Extensible Markup Language) protocol, binary-based serialization protocol ProtocolBuffer (Protocol Cache, PB for short), and JSON (JavaScript). Object Notation, JavaScript object tagging, etc.
  • a server generally supports only one format of a serialization protocol.
  • a WEB server only supports an XML format or a JS0N format protocol
  • a service server generally only supports a PB format protocol.
  • the message transfer mode between the servers is limited, for example, the message can only be transmitted in the XML format or can only be transmitted in the PB format, and the message body of the XML format message is large.
  • the operation efficiency is poor, and the message in the PB format is poorly readable.
  • the message delivery mode of the server is inflexible and the message transmission performance is low.
  • the embodiments of the present invention provide a conversion method between serialized messages and a corresponding conversion device between serialized messages, so as to solve the inflexible manner of message transmission between servers in the existing solution. Low performance issues, etc.
  • an embodiment of the present invention provides a method for converting between serialized messages, including:
  • the serialized data structure by using the set serialization description item; wherein, the first serialized message Having a first format, the second serialized message has a second format; the serialized description item includes a data structure description item and a field description item set in the data structure description item, a structure of each data structure description item, and a data structure description item The structure of each field description item is consistent with the first structure; for the serialized message to be converted in the first format conforming to the first structure, the serialized message is parsed according to the serialized data structure, and the parsed data is written.
  • serialized data structure In the serialized data structure, the serialized data structure after the data is written is obtained; the data in the serialized data structure after the data is written is extracted; and the extracted data is converted into the data according to the correspondence recorded in the serialized data structure.
  • an embodiment of the present invention provides a device for converting between serialized messages, including:
  • a data structure generating unit configured to generate a serialized data structure by using the set serialized description item according to the first structure of the first serialized message and the correspondence between the first structure and the second structure of the second serialized message;
  • the first serialized message has a first format
  • the second serialized message has a second format.
  • the serialized description item includes a data structure description item and a field description item set in the data structure description item, and each data structure description item The structure of each field description item in the structure and data structure description item is consistent with the first structure;
  • the parsing unit is configured to parse the serialized message to be converted in the first format conforming to the first structure, parse the serialized message according to the serialized data structure, and write the parsed data into the serialized data structure to obtain Serialized data structure after data is written;
  • a data extracting unit configured to extract data in the serialized data structure after the data is written
  • a converting unit configured to convert the extracted data into a serialized message of a second format having the second structure according to the correspondence recorded in the serialized data structure.
  • the embodiment of the present invention generates a serialized data structure by using the set serialization description item, and writes the serialized data message to be converted in the first format into the serialized data structure, and then serializes the data after the data is written.
  • the technical means for extracting data into a serialized message of the second format in the structure can enable the server to conveniently and quickly serialize messages of different formats through a serialized data structure compatible with serialized messages of multiple formats.
  • Conversion according to actual needs, select a serialization protocol of a specific format to transmit serialized messages, for example, use XML format to transmit messages outside the server, to inherit the extensive application of the XML format in the system, and transmit the message in the PB format inside the server. In order to inherit the efficiency of the PB format, thereby improving the flexibility of the server to deliver messages and optimizing the message transmission performance.
  • FIG. 1 shows a flow chart of a method for converting between serialized messages according to an embodiment of the present invention
  • FIG. 2 is a schematic diagram showing a method for parsing an XML serialized message according to a serialized data structure according to still another embodiment of the present invention
  • FIG. 3 illustrates a flow diagram of outputting a PB serialized message from a serialized data structure written by data, in accordance with yet another embodiment of the present invention
  • FIG. 4 is a schematic diagram showing a method of parsing a PB message according to a serialized data structure according to still another embodiment of the present invention
  • FIG. 5 is a diagram showing a serialized data structure written from data according to still another embodiment of the present invention. Flowchart for outputting an XML serialized message;
  • FIG. 6 is a block diagram showing the structure of a device for converting between serialized messages according to still another embodiment of the present invention.
  • An embodiment of the present invention provides a method for converting between serialized messages.
  • the method includes the following steps: S100: following a first structure of a first serialized message, and the first structure and the second sequence
  • the serialized data structure is generated using the set serialization description item, wherein the first serialized message has a first format and the second serialized message has a second format.
  • the first format and the second format respectively correspond to a data format of a serialization protocol, such as an XML (Extensible Markup Language) format, a PB (Protocol Cache, abbreviated as PB) format, or a JSON (JavaScript Object Notation, JavaScript object tag) format, etc., where the first format is different from the second format.
  • a serialization protocol such as an XML (Extensible Markup Language) format, a PB (Protocol Cache, abbreviated as PB) format, or a JSON (JavaScript Object Notation, JavaScript object tag) format, etc.
  • a serialized data structure is a bridge between serialized messages, which contains several serialized description items.
  • a serialized description item represents a type or field that can be serialized to describe the rules at the time of serialization.
  • the serialization description item includes a data structure description item and a field description item.
  • a data structure description item is a set of field description items.
  • a data structure description item may include multiple field description items, and another data structure may be nested in the data structure description item, that is, set in the data structure description item.
  • Subdata structure description items support complex data types.
  • the sub-data structure description item is basically the same as the data structure description item.
  • the main difference is that the sub-data structure description item also needs to include a relationship information, and the sub-data structure description item located in the same data structure description item is established by the relationship information.
  • the field describes the relationship of the items.
  • each data structure description item and the structure of each field description item and sub-data description item (if present) in the data structure description item need to be consistent with the first structure. The following is explained in conjunction with the examples.
  • first serialized message XML serialized message
  • the root node name is "config”
  • the root node includes node elements named "name” and "address”
  • the first structure further includes a nested substructure, substructure
  • the child root node has the name “childs” and the child root node includes the node attribute named "childName” and the node element named "childAddress”.
  • PB serialized message having a second structure when the second format is the PB format is as follows:
  • Optional string name 1; ⁇ indicates a data item with sequence number 1
  • Optional string address 2; ⁇ indicates a data item with sequence number 2
  • Optional string name 1; ⁇ indicates a data item with sequence number 1
  • Optional string address 2; ⁇ indicates a data item with sequence number 2 ⁇
  • Optional childs childsEntity 3; ⁇ indicates a substructure of sequence number 3
  • the second structure of the PB serialization message includes a data item whose sequence number is 1, a data type is a string (string), and a data item whose data type is a character string.
  • the second structure further includes a nested sub-structure of sequence number 3.
  • the sub-structure includes a data item whose sequence number is 1, a data type is a character string, and a data item whose serial number is 2. and the data type is a character string.
  • the serialized data structure is generated by using the set serialization description item, which can be expressed as follows:
  • Structure name "config” ⁇ indicates the structure name
  • Data structure ⁇ ⁇ indicates sub-data structure description item
  • Structure name "childs" ⁇ indicates substructure name
  • the serialized data structure includes a data structure description item, and the data structure description item includes a structure name, a field description item, and a sub data structure description item.
  • the structure name is the root node name "config" in the first structure above.
  • Field description items include data type (type), sequence number, node type, and name, where the node type is the default node type (such as a node element) and can be omitted.
  • the sequence number in the field description item corresponds to the sequence number in the PB format, and the sequence number in the field description item is the sequence number of the data in the field description item in the PB serialization message having the second structure, and the field description item includes the “sequence number.
  • the node type in the node type field description item includes the node element and the node attribute, and the default node type is the node element, Omitting or not;
  • the name in the field description item corresponds to the name of the node element or node attribute in the XML format.
  • the sub-data structure description item includes a sequence number, a sub-structure name, and a sub-field description item.
  • the substructure name in the subdata structure description item corresponds to the name of the subroot node in the XML format.
  • the subfield description item includes the data type, sequence number, node type, and name.
  • the information in the subfield description item has the same effect as the information in the field description item.
  • the first structure and the PB sequence of the XML serialized message are established by the structure name in the data structure description item, the substructure name in the subdata structure description item, and the sequence number and the name in the field description item and the sub field description item.
  • Corresponding relationship between the second structure of the message for the scenario where the nested structure does not exist, the first structure and the PB of the XML serialization message are established by the structure name and the sequence number and the name in the field description item in the data structure description item.
  • the correspondence between the second structures of the serialized messages that is, the correspondence is set in the serialized data structure by the information in the data structure description item and the information in the field description item. In a variant, the correspondence may also be set in the serialized data structure only by the information in the field description item.
  • the information in the data structure description item, the sub-data structure description item, the field description item, and the sub-field description item are changed according to the format and structure of the serialization message, and are not strictly limited herein.
  • step S101 After the serialization data structure is created by the above operation, the process proceeds to step S101.
  • S101 parsing the to-be-converted serialized message according to the serialized data structure, and parsing the parsed data into the serialized data structure to obtain data Serialized data structure after writing.
  • S103 Convert the extracted data into a serialized message of a second format having a second structure according to the correspondence recorded in the serialized data structure.
  • a scenario in which the first format is an XML format and the second format is a PB format is taken as an example, and the foregoing step S 101 is performed.
  • the operation in the description is described in which the serialized message in the XML format has the first structure, and the serialized message in the PB format has the second structure; other contents of the conversion method between the serialized messages are referred to other embodiments of the present invention.
  • step S100 After the serialized data structure is obtained through the above step S100, all the XML serialized messages conforming to the first structure can be parsed as the serialized data structure to be converted, and the parsing process is shown in FIG. 2, which shows The process of parsing an XML serialized message according to a serialized data structure includes the following steps:
  • S200 The server (or other device) loads the XML serialization message to be converted.
  • S201 The server corrects the root node according to the XML serialization message to be converted.
  • the server uses the stream analysis method to read the XML elements in the XML serialization message to be converted according to the serialized data structure, and processes the XML elements in the read XML serialization message to be processed one by one.
  • the name in the read XML element is the same as the structure name in the data structure description item, it is determined that the XML node corresponding to the XML element is the root node.
  • the nodes of the XML and the corresponding field description items in the serialized data structure can be made consistent.
  • step S202 The server parses the node attribute in the current node, and writes the data of the XML element as the node attribute into the corresponding field description item in the serialized data structure, and proceeds to step S203.
  • the current node is an XML node that is currently being processed or indirectly dependent on the root node, and the XML element in the current node is dependent or indirectly dependent on the root node.
  • the data of the XML element in the current node is parsed one by one, and the parsed data is written into the serialized data structure.
  • the parsed data of the XML element is written in the field description item.
  • the type of the XML element includes a node attribute and a node element.
  • the parsing of the node attribute in the currently processed current node is performed, and then the parsing of the node element is performed.
  • this embodiment does not apply to the node.
  • the order in which attributes and node elements are parsed is strictly limited.
  • step S203 The server parses the node element in the current node, and proceeds to step S204.
  • step S204 The server determines whether there is a nested structure, and if yes, returns to step S201, and if no, proceeds to step S205 to determine whether there is a nested structure according to the name of the XML element, and the name and sub-data structure description item of the read XML element When the substructure names in the same are the same, confirm that there is a nested structure.
  • the currently processed XML element is used as a child root node (replace the root node with the child root node), and each XML element in the child root node is read, and the child root is read.
  • the name of the XML element in the node will be the same as the name in the subfield description item (substituting the field description item with the subfield description item) and the type of the XML element is the same as the node type in the subfield description item.
  • the data of the XML element is written in the subfield description item.
  • step S205 The server writes the data of the XML element as the node element into the corresponding field descriptor, and the name of the XML element read in step S206 is the same as the name in the field descriptor, and the node type in the field descriptor When it is a node element, it is confirmed that the XML element corresponds to one node element, and the data of the XML element is read and written into the corresponding field descriptor.
  • step S206 determining whether the data of the XML element in the read XML serialization message to be converted has been written to the corresponding field description item or sub-field description item, and if yes, proceeding to step S207, if no, returning to step S201, Repeating the above steps S201 to S206, writing the unwritten XML element into the field description item or the sub-field description item until it is determined that all the data in the XML serialization message is written in the serialization structure, and proceeding to step S207
  • S207 The serialized data structure after the data is written is obtained by the field description item or the sub-field description item after the data of the XML element is written, and the operation ends.
  • a scene in which the first format is an XML format and the second format is a PB format is taken as an example, and data is extracted from a serialized structure after data writing and converted into a PB sequence having a second structure. The operation of the message is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
  • FIG. 3 a flow chart of outputting a PB serialization message from a serialized data structure after data writing according to the embodiment is shown, which specifically includes the following steps:
  • step S300 The server (or other device) starts the output of the PB serialization message, and proceeds to step S301.
  • the server enters an output mode of the PB format, in which the server outputs a PB serialization message.
  • Server load count The serialized data structure is written to perform format conversion.
  • the server extracts the data in the field description item and the data in the sub-data structure description item (if any) from the serialized data structure after the data is written, and proceeds to step S302.
  • S302 The server performs different operations according to different types of extracted data.
  • step S303 When the extracted data is a numeric type of data, the process proceeds to step S303;
  • step S305 When the extracted data is a sub data structure description item, the process proceeds to step S305.
  • the first manner is used for processing: first, in a variable (Varint) manner, the serial number and the data type of the data recorded in the field description item are first written into the PB serialized message. In the data item corresponding to the same serial number, data of the numerical type is written into the data item.
  • Varint variable
  • S304 When the extracted data is a string type data, the following method is used to process: first, in the Varint manner, the sequence number of the data recorded in the field description item is the same as the data type written in the PB serialization message. In the data item corresponding to the serial number, the string length of the data recorded in the field description item is written into the data item, and then the data of the string type is written into the data item.
  • the sequence number and the structure type (the structure type indication sub-data structure) in the sub-data structure description item are first written into the PB serialization message in the Varint manner.
  • the data length in the sub-data structure description item is written into the data item, and then returns to step S301, and the data in the sub-data structure description item is processed in the same manner as in steps S301 to S304.
  • the difference is mainly to replace the field description item in steps S301 to S304 with the sub-field description item, that is, when the extracted content is numeric type data, according to the first manner, the sub-field description item is firstly introduced in the Varint manner.
  • the serial number of the data recorded in the data type is written in the data item corresponding to the same serial number in the PB serialization message, and the data of the numerical type is written into the data item; when the extracted data is the data of the string type
  • the sequence number and data type of the data recorded in the subfield description item are first described in the Varint manner. PB message into sequences of the same sequence number corresponding to the data item, then the length of the substring description entry field data recorded in the data item is written, and then writes the character string data type in the data item.
  • step S306 determining whether the extracted data has been written into the corresponding data item in the PB serialization message, and if yes, proceeding to step S307, if not, returning to step S302, repeating the above steps S302 to S306, according to the unwritten data
  • the data type in the first mode or the second mode, writes the unwritten data into the corresponding data item until all the data in the serialized data structure is processed.
  • S307 Obtain a PB serialized message having a second structure from each data item after the data is written.
  • the first format is a PB format and the second format is an XML format
  • the data in the serialized message of the PB format of the first structure is parsed, and the parsed data is written into the sequence.
  • the operation of the data structure is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
  • All PB serialized messages conforming to the first structure can be parsed by using the serialized data structure.
  • the parsing process is shown in FIG. 4, which shows that the PB serialized message is performed according to the serialized data structure.
  • the process of parsing includes the following steps:
  • S400 The server loads the binary data stream of the PB to be converted serialized message.
  • the server reads the variable (Varint) value in the binary data stream of the PB to be converted serialized message one by one.
  • S402 The server performs different operations according to different types of extracted Varint values.
  • the server first determines the data type of the extracted data according to the correspondence recorded in the serialized data structure. For example, the server can know the data type of the data written in the field description item according to the data type in the field description item. In the specific operation, the server obtains the data type and sequence number corresponding to the variable value from the read variable value. According to the Varint coding standard, the data of the last 3 bits of the Varint value is placed in the data type, and the remaining bits are placed in the sequence number.
  • step S403 When the read Varint value is a numeric type, the process proceeds to step S403;
  • the sequence number of the read Varint value is the same as the sequence number of the sub data structure description item, it is determined that the read Varint value is a sub data structure description item.
  • the read Varint value is a numeric type
  • the following method is used: continue to read the next Varint value (the next Varint value is a numeric type data), and write the next Varint value.
  • the sequence number is the same as the sequence number of the Varint value. Since the sequence number and the data type of the data item in the PB format are recorded in the serialized data structure, and the order of each data item in the serialized data structure is also the same as the order of each data item in the PB serialized message to be converted, Therefore, according to the serialization structure, each data item in the PB serialization message can be quickly read out, and the read data is written into the serialized data structure according to the data type and the serial number.
  • the fourth method is used for processing: continue to read the next Varint value, obtain the length of the string, and then read the byte length of the string,
  • the read byte is encoded in UTF8 (8-bit Unicode Transformation Format) to obtain a string, which is written in the field description item whose sequence number is the same as the serial number of the string in the serialized data structure.
  • the data in the sub-data structure description item is processed in the same manner as in the steps S401 to S404, and the difference mainly lies in replacing the field description item in the steps S401 to S404 with the sub-field description item, that is, when the byte code is from the byte code
  • the third method is used for processing, and the third method is used for processing: continue reading the next Varint value, and writing the next Varint value to the serialized data structure.
  • the subfield is in the same subfield description as the sequence number of the Varint value.
  • the fourth method is used for processing: continue to read the next Varint value, obtain the length of the string, and then read the length of the string.
  • UTF8 encoding the read byte, obtaining a string, and writing the string into the subfield description item whose sequence number is the same as the serial number of the string in the serialized data structure.
  • step S406 determining whether all the Varint values in the PB to be converted serialized message have been written in the field description item or the subfield description item, if yes, proceeding to step S407, if not, returning to step S402, repeating the above steps S402 to S406, According to the data type of the unwritten Varint value, the unwritten variable value is written into the field description item or the subfield description item in the third mode or the fourth mode until all the data in the PB serialization message is written. In the serialized structure, proceed to step S407.
  • S407 The serialized data structure after the data is written by the field description item or the sub-field description item after the Varint value is written. End the operation.
  • the first format is a PB format and the second format is an XML format
  • the extracted data is converted into an XML serialization of the second structure according to the correspondence in the serialized data structure.
  • the operation of the message is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
  • the flowchart for outputting the XML serialization message from the serialized data structure after data writing includes the following steps:
  • the server enters an output mode in XML format, in which the server outputs an XML serialized message.
  • the server loads the serialized data structure after the data is written to perform format conversion.
  • the server first determines whether the extracted data is the structure name and the node type for judging the extracted data according to the correspondence recorded in the serialized data structure.
  • the name in the extracted data is the same as the structure name of the serialized data structure record, the root node in the XML serialization message is created with the data and structure name.
  • S502 The server extracts data and sub-data structure description items (if any) in the field description item from the serialized data structure after the data is written, and proceeds to step S503.
  • step S503 The server creates an XML node attribute, and writes data into the XML node attribute, and proceeds to step S504 to learn whether the data corresponds to the node element by using the field description item or the node type recorded in the sub-field description item to which the extracted data belongs. Node properties.
  • the following sixth manner is used for processing: Creating an XML node with the name in the field description item of the node attribute under the current node (the currently processed node directly or indirectly subordinate to the root node) Attribute, and write the data in the field description item to the XML node attribute.
  • the node attribute in the XML serialization message is created first, and then the node element is created.
  • this embodiment does not strictly define the node attribute and the order in which the node element is created.
  • S504 The server creates an XML node element.
  • the scene with no nested structure is processed in the following fifth way: Under the root node, an XML node is created with the name in the field description item of the node element, and the field description is performed. The data in the item or subfield description item is written to the XML node.
  • step S505 The server determines whether the node is a nested structure, and if yes, returns to step S502, and if no, proceeds to step S506.
  • the extracted structure includes the substructure name
  • the data corresponds to the sub data structure description item.
  • the extracted data is a sub-data structure description item, that is, when the node includes a nested structure
  • the sub-root node is created by using the name in the sub-data structure description item, and the data in the sub-data structure description item is extracted, and step S502 is adopted.
  • the same method in S504 processes the data in the sub-data structure description item, the main difference is that the field description items in steps S501 to S504 are replaced with sub-field description items, that is, when the extracted data is a node element, An XML node is created under the child root node with the name in the subfield description item of the node element, and the data in the subfield description item is written into the XML node.
  • the extracted data is a node attribute
  • an XML node attribute is created under the current node with the name in the sub-field description item of the node attribute, and the data in the sub-field description item is written into the XML node attribute.
  • S506 The server writes data in the field description item (or sub-field description item) into the XML node.
  • step S507 determining whether the extracted data has been written into the corresponding XML node element or the XML node attribute, and if yes, proceeding to step S508, if not, returning to step S502, repeating the above steps S502 to S507, according to the unwritten
  • the node type of the data, the unwritten data is written into the corresponding XML node element or the XML node attribute by using the fifth mode or the sixth mode described above, until all the data in the serialized data structure after the data is written is processed. Go to step S508.
  • the apparatus 600 includes a data structure generating unit 601, a parsing write unit 602, a data extracting unit 603, and a converting unit 604.
  • the data structure generating unit 601 includes a correspondence relationship setting unit 601 1 and a sub-structure setting unit 6012.
  • the data structure generating unit 601 is configured to generate a serialized data structure by using the set serialization description item according to the first structure of the first serialization message and the correspondence between the first structure and the second structure of the second serialization message;
  • the first serialized message has a first format
  • the second serialized message has a second format.
  • the serialized description item includes a data structure description item and a field description item set in the data structure description item, and each data structure description item The structure of each field description item in the structure and data structure description item is consistent with the first structure.
  • the data structure generating unit 601 includes a correspondence relationship setting unit 601 1 for using the sequence according to the first structure of the first serialization message and the correspondence between the first structure and the second structure of the second serialization message.
  • the information in the data structure description item of the description item and the information in the field description item are set in the generated serialized data structure.
  • the correspondence relationship setting unit 601 1 is configured to set the structure name in the data structure description item.
  • the structure name is a name of a root node in the XML serialization message having the first structure
  • the field description item includes a data type, a sequence number, a node type, and a name
  • the serial number is a data in the field description item.
  • the sequence number in the PB serialization message of the two structure; the correspondence relationship is set in the generated serialized data structure by the structure name in the data structure description item, the sequence number and the name in the field description item.
  • the data structure generating unit 601 further includes a sub-structure setting unit 6012, configured to set a sub-data structure description item in the data structure description item, where the sub-data structure description item is provided with a sub-field description item, and the corresponding relationship includes the same data structure.
  • the correspondence relationship setting unit 601 1 is configured to set a structure name, a field description item, and a sub data structure description item in the data structure description item, where the structure name is a name of the root node in the XML serialization message having the first structure, and the sub data structure description
  • the item includes a sequence number, a substructure name, and a subfield description item, and the field description item or the subfield description item includes a data type, a sequence number, a node type, and a name, and the serial number is a field description item or the data in the subfield description item has a second
  • the sequence number in the structured PB serialization message; the corresponding relationship is set in the generated structure by the structure name in the data structure description item, the substructure name in the subdata structure description item, the field description item, and the sequence number and name in the subfield description item. Serialized in a data structure.
  • the parsing and writing unit 602 is configured to read the XML element in the XML serialized message to be converted according to the serialized data structure, when the read XML element
  • the XML node corresponding to the XML element is determined to be the root node; the XML elements that are dependent or indirectly subordinate to the root node are read, and the name and the field description item in the XML element are
  • the names in the same are the same and the type of the XML element is the same as the node type in the field description item
  • the data of the parsed XML element is written in the field description item; when the name in the XML element and the sub-data structure description item
  • the XML element is used as a sub-root node, and each XML element subordinate to or indirectly subordinate to the sub-root node is
  • the data of the parsed XML element is written in the subfield.
  • the description item determining whether the data of the XML element in the read XML serialization message to be converted has been written to the corresponding field description item or sub-field description item, if yes, after writing the data of the XML element
  • the field description item or the sub-field description item obtains the serialized data structure after the data is written. If not, the unwritten XML element is written in the field description item or the sub-field description item, and the field description item or sub-field description item
  • the name and type of the node are the same as the name and type of the unwritten XML element.
  • the converting unit 604 is configured to determine, according to the correspondence relationship recorded in the serialized data structure, the data type of the extracted data; when the extracted data is a numerical type data, the first manner is performed by using the following method: First describe the item or sub The sequence number of the data recorded in the field description item is written in the data item corresponding to the same sequence number in the PB serialization message, and the data of the value type is written into the data item; when the extracted data is a string type data
  • the second method is used to process: first, in the variable manner, first input the sequence number and data type of the data recorded in the field description item or the sub-field description item into the data item corresponding to the same serial number in the PB serialization message, and then The string length of the data recorded in the field description item or subfield description item is written in the data item, and then the data of the string type is written into the data item; when the extracted data is a sub data structure description item, the variable is The method first writes the sequence number and structure type in the sub-data structure description item into the
  • the sub-field description item of the item describes the data in the item.
  • the first method is used for processing.
  • the second method is used for processing; whether the extracted data has been written into the corresponding data item in the PB serialization message, and if so, the data after the data is written.
  • the item obtains the serialized message of the second format having the second structure. If not, according to the data type of the unwritten data, the unwritten data is written into the corresponding data item according to the first mode or the second mode. .
  • the parsing and writing unit 602 is configured to read the variable values in the binary data stream of the PB to be converted serialized message one by one; from the read variable values Obtain the data type and serial number corresponding to the variable value;
  • the read variable value is a numeric type
  • the following fourth method is used for processing: continue to read the next variable value, and obtain The length of the string, and then the byte of the length of the string is read, the string is obtained, and the string is written into the field description item or the subfield description item of the sequence number in the serialized data structure;
  • the sequence number of the extracted variable value is the same as the sequence number of the sub data structure description item
  • the value of the next variable is continuously read, the data length is obtained, and then the number is read.
  • the length of the byte gets the bytecode set, and reads the variable value in the bytecode set.
  • the third way is used to process the read variable value.
  • the fourth method is used for processing; determining whether all variable values in the PB to be converted serialized message have been written in the field description item or the sub field description item, and if so, the field after the variable value is written.
  • the description item or subfield description item obtains the serialized data structure after the data is written. If not, according to the data type of the unwritten variable value, the unwritten variable value is written into the field by the third mode or the fourth mode. Description item or subfield description item.
  • the converting unit 604 is configured to determine, according to the correspondence relationship recorded in the serialized data structure, whether the extracted data is a structure name and determine a node type of the extracted data; when the extracted data includes a structure name, the data and The structure name creates the root node in the XML serialization message; when the extracted data is a node element, it is processed in the following fifth way: under the root node, the field description item or subfield description item name of the node element Create an XML node element and write the data in the field description item or sub-field description item into the XML node element; when the extracted data is a node attribute, the following sixth manner is used to process: Create an XML node attribute in the field description item or the name in the subfield description item, and write the data in the field description item or subfield description item into the XML node attribute; when the extracted data is a sub data structure description item , create a child root node with the name in the child data structure description item, extract the number in the child data
  • the data extracting unit 603 is for extracting data in the serialized data structure after the data is written.
  • the embodiment of the present invention generates a serialized data structure by using the set serialization description item, and writes the serialized message to be converted in the first format into the serialized data structure, and then serializes after the data is written.
  • the technical means for extracting data into a serialized message of the second format in the data structure can enable the server to facilitate serialized messages of different formats by using a serialized data structure compatible with serialized messages of multiple formats.
  • Fast conversion according to actual needs, select a serialization protocol of a specific format to transmit messages, for example, use XML format to transmit messages outside the server, to inherit the extensive application of the XML format in the system, and transmit messages in the PB format inside the server. Inheriting the efficiency of the PB format, thereby improving the flexibility of the server to deliver messages and optimizing the message transmission performance.
  • the words “first”, “second,” and the like are used to distinguish the same items or similar items whose functions and functions are substantially the same, in the field.
  • the skilled person can understand that the words “first”, “second”, etc. do not limit the quantity and execution order.

Abstract

Disclosed are a method and an apparatus for performing conversion among serialized messages. The method provided in an embodiment of the present invention comprises: generating, by using a set serialized description entry, a serialized data structure according to a first structure of a first serialized message and a correspondence between the first structure and a second structure of a second serialized message, the serialized description entry comprising a data structure description entry and a field description entry set in the data structure description entry; for a to-be-converted serialized message conforming to a first format of the first structure, parsing the to-be-converted serialized message according to the serialized data structure, and writing parsed data into the serialized data structure, so as to obtain the serialized data structure the data is written into; extracting the data in the serialized data structure the data is written into; and converting the extracted data into a serialized message with a second format of the second structure according to a correspondence recorded in the serialized data structure.

Description

序列化消息之间的转换方法和装置 技术领域  Method and apparatus for converting between serialized messages
本发明涉及数据处理技术领域, 特别涉及一种序列化消息之间的转换方法和装置。  The present invention relates to the field of data processing technologies, and in particular, to a method and apparatus for converting between serialized messages.
背景技术 Background technique
随着互联网的发展以及云计算的应用, 大型的分布式系统越来越多, 消息序列化是分布式系统 中重要的一环, 它像血液一样负责服务集群之间的消息传递。 在消息序列化中常见的序列化协议包 括自解释的序列化协议如 XML ( Extensible Markup Language, 扩展性标识语言)协议, 基于二进制 的序列化协议 ProtocolBuffer (协议緩存, 简称为 PB )和 JSON (JavaScript Object Notation, JavaScript 对象标记)协议等。  With the development of the Internet and the application of cloud computing, there are more and more large-scale distributed systems. Message serialization is an important part of distributed systems. It is responsible for message delivery between service clusters like blood. Serialization protocols commonly found in message serialization include self-explanatory serialization protocols such as XML (Extensible Markup Language) protocol, binary-based serialization protocol ProtocolBuffer (Protocol Cache, PB for short), and JSON (JavaScript). Object Notation, JavaScript object tagging, etc.
目前, 一种服务器一般仅支持一种格式的序列化协议, 如 WEB (网页)服务器仅支持 XML格 式或 JS0N格式的协议, 而业务服务器一般仅支持 PB格式的协议。  Currently, a server generally supports only one format of a serialization protocol. For example, a WEB server only supports an XML format or a JS0N format protocol, and a service server generally only supports a PB format protocol.
现有方案至少具有如下缺陷:  Existing solutions have at least the following drawbacks:
由于现有服务器一般仅能支持单一格式的消息, 限制了服务器之间的消息传递方式, 如仅能按 照 XML格式传输消息或者仅能按照 PB格式传输消息, 而 XML格式消息的消息体较大、 运行效率 较差, PB格式的消息可读性较差。 现有方案中服务器的消息传递的方式不灵活、 消息传输性能低下。 发明内容  Since the existing server can only support messages in a single format, the message transfer mode between the servers is limited, for example, the message can only be transmitted in the XML format or can only be transmitted in the PB format, and the message body of the XML format message is large. The operation efficiency is poor, and the message in the PB format is poorly readable. In the existing solution, the message delivery mode of the server is inflexible and the message transmission performance is low. Summary of the invention
鉴于上述问题, 本发明实施例提供了一种序列化消息之间的转换方法和相应的序列化消息之间 的转换装置, 以解决现有方案中服务器之间消息传递的方式不灵活、 消息传输性能低下的问题等。  In view of the above problems, the embodiments of the present invention provide a conversion method between serialized messages and a corresponding conversion device between serialized messages, so as to solve the inflexible manner of message transmission between servers in the existing solution. Low performance issues, etc.
为了达到上述目的, 本发明实施例采用的技术方案如下:  In order to achieve the above objective, the technical solution adopted by the embodiment of the present invention is as follows:
一方面, 本发明实施例提供了一种序列化消息之间的转换方法, 包括:  In one aspect, an embodiment of the present invention provides a method for converting between serialized messages, including:
按照第一序列化消息的第一结构以及第一结构与第二序列化消息的第二结构之间的对应关系, 利用设置的序列化描述项生成序列化数据结构; 其中, 第一序列化消息具有第一格式, 第二序列化 消息具有第二格式; 序列化描述项包括数据结构描述项和设置在数据结构描述项之中的字段描述项, 各数据结构描述项的结构及数据结构描述项之中各字段描述项的结构与第一结构相一致; 对符合第 一结构的第一格式的待转换序列化消息, 根据序列化数据结构对待转换序列化消息进行解析, 将解 析出的数据写入序列化数据结构中, 得到数据写入后的序列化数据结构; 提取数据写入后的序列化 数据结构中的数据; 按照序列化数据结构中记录的对应关系, 将提取出的数据转换为具有第二结构 的第二格式的序列化消息。  And generating, according to the first structure of the first serialized message and the correspondence between the first structure and the second structure of the second serialized message, the serialized data structure by using the set serialization description item; wherein, the first serialized message Having a first format, the second serialized message has a second format; the serialized description item includes a data structure description item and a field description item set in the data structure description item, a structure of each data structure description item, and a data structure description item The structure of each field description item is consistent with the first structure; for the serialized message to be converted in the first format conforming to the first structure, the serialized message is parsed according to the serialized data structure, and the parsed data is written. In the serialized data structure, the serialized data structure after the data is written is obtained; the data in the serialized data structure after the data is written is extracted; and the extracted data is converted into the data according to the correspondence recorded in the serialized data structure. A serialized message of a second format having a second structure.
另一方面, 本发明实施例提供了一种序列化消息之间的转换装置, 包括:  On the other hand, an embodiment of the present invention provides a device for converting between serialized messages, including:
数据结构生成单元, 用于按照第一序列化消息的第一结构以及第一结构与第二序列化消息的第 二结构之间的对应关系, 利用设置的序列化描述项生成序列化数据结构; 其中, 第一序列化消息具 有第一格式, 第二序列化消息具有第二格式; 序列化描述项包括数据结构描述项和设置在数据结构 描述项之中的字段描述项, 各数据结构描述项的结构及数据结构描述项之中各字段描述项的结构与 第一结构相一致;  a data structure generating unit, configured to generate a serialized data structure by using the set serialized description item according to the first structure of the first serialized message and the correspondence between the first structure and the second structure of the second serialized message; The first serialized message has a first format, and the second serialized message has a second format. The serialized description item includes a data structure description item and a field description item set in the data structure description item, and each data structure description item The structure of each field description item in the structure and data structure description item is consistent with the first structure;
解析写入单元, 用于对符合第一结构的第一格式的待转换序列化消息, 根据序列化数据结构对 待转换序列化消息进行解析, 将解析出的数据写入序列化数据结构中, 得到数据写入后的序列化数 据结构;  The parsing unit is configured to parse the serialized message to be converted in the first format conforming to the first structure, parse the serialized message according to the serialized data structure, and write the parsed data into the serialized data structure to obtain Serialized data structure after data is written;
数据提取单元, 用于提取数据写入后的序列化数据结构中的数据;  a data extracting unit, configured to extract data in the serialized data structure after the data is written;
转换单元, 用于按照序列化数据结构中记录的对应关系, 将提取出的数据转换为具有第二结构 的第二格式的序列化消息。  And a converting unit, configured to convert the extracted data into a serialized message of a second format having the second structure according to the correspondence recorded in the serialized data structure.
由上所述, 本发明实施例采用由设置的序列化描述项生成序列化数据结构, 将第一格式的待转 换序列化消息写入序列化数据结构, 再从数据写入后的序列化数据结构中将数据提取出来转换为第 二格式的序列化消息的技术手段, 能通过一种序列化数据结构兼容多种格式的序列化消息, 使服务 器能够对不同格式的序列化消息进行方便、 快速的转换, 根据实际需求选取特定格式的序列化协议 来传输序列化消息, 例如, 在服务器外部采用 XML格式传输消息, 以继承 XML格式在系统中应用 的广泛性, 在服务器内部采用 PB格式传输消息, 以继承 PB格式的高效性, 从而提高了服务器传递 消息的灵活性, 优化了消息传输性能。 附图说明 As described above, the embodiment of the present invention generates a serialized data structure by using the set serialization description item, and writes the serialized data message to be converted in the first format into the serialized data structure, and then serializes the data after the data is written. The technical means for extracting data into a serialized message of the second format in the structure can enable the server to conveniently and quickly serialize messages of different formats through a serialized data structure compatible with serialized messages of multiple formats. Conversion, according to actual needs, select a serialization protocol of a specific format to transmit serialized messages, for example, use XML format to transmit messages outside the server, to inherit the extensive application of the XML format in the system, and transmit the message in the PB format inside the server. In order to inherit the efficiency of the PB format, thereby improving the flexibility of the server to deliver messages and optimizing the message transmission performance. DRAWINGS
图 1示出了根据本发明一个实施例的一种序列化消息之间的转换方法流程图;  1 shows a flow chart of a method for converting between serialized messages according to an embodiment of the present invention;
图 2示出了根据本发明又一个实施例的按照序列化数据结构对 XML序列化消息进行解析的方法 示意图;  2 is a schematic diagram showing a method for parsing an XML serialized message according to a serialized data structure according to still another embodiment of the present invention;
图 3示出了根据本发明又一个实施例的从数据写入的序列化数据结构中输出 PB序列化消息的流 程图;  3 illustrates a flow diagram of outputting a PB serialized message from a serialized data structure written by data, in accordance with yet another embodiment of the present invention;
图 4示出了根据本发明又一个实施例的按照序列化数据结构对 PB消息进行解析的方法示意图; 图 5示出了根据本发明又一个实施例的从数据写入的序列化数据结构中输出 XML序列化消息的 流程图;  4 is a schematic diagram showing a method of parsing a PB message according to a serialized data structure according to still another embodiment of the present invention; FIG. 5 is a diagram showing a serialized data structure written from data according to still another embodiment of the present invention; Flowchart for outputting an XML serialized message;
图 6示出了根据本发明又一个实施例的一种序列化消息之间的转换装置的结构示意图。  FIG. 6 is a block diagram showing the structure of a device for converting between serialized messages according to still another embodiment of the present invention.
具体实施方式 detailed description
为使本发明的目的、 技术方案和优点更加清楚, 下面将结合附图对本发明实施方式作进一步地 详细描述。  The embodiments of the present invention will be further described in detail below with reference to the accompanying drawings.
本发明一个实施例提供了一种序列化消息之间的转换方法, 参见图 1 , 该方法包括如下步骤: S 100:按照第一序列化消息的第一结构以及该第一结构与第二序列化消息的第二结构之间的对 应关系, 利用设置的序列化描述项生成序列化数据结构, 其中该第一序列化消息具有第一格式, 该 第二序列化消息具有第二格式。  An embodiment of the present invention provides a method for converting between serialized messages. Referring to FIG. 1, the method includes the following steps: S100: following a first structure of a first serialized message, and the first structure and the second sequence Corresponding to the second structure of the message, the serialized data structure is generated using the set serialization description item, wherein the first serialized message has a first format and the second serialized message has a second format.
上述第一格式、 第二格式分别对应一种序列化协议的数据格式, 如 XML ( Extensible Markup Language,扩展性标识语言)格式、 PB(协议緩存,简称为 PB )格式或 JSON(JavaScript Object Notation, JavaScript对象标记)格式等等, 其中第一格式不同于第二格式。  The first format and the second format respectively correspond to a data format of a serialization protocol, such as an XML (Extensible Markup Language) format, a PB (Protocol Cache, abbreviated as PB) format, or a JSON (JavaScript Object Notation, JavaScript object tag) format, etc., where the first format is different from the second format.
序列化数据结构是序列化消息之间互相转化的桥梁, 其包含若干个序列化描述项。 一个序列化 描述项代表一个可被序列化的类型或字段, 用来描述序列化时的规则。  A serialized data structure is a bridge between serialized messages, which contains several serialized description items. A serialized description item represents a type or field that can be serialized to describe the rules at the time of serialization.
序列化描述项包括数据结构描述项和字段描述项。 数据结构描述项是一系列字段描述项的集 合, 一个数据结构描述项中可以包含多个字段描述项, 数据结构描述项中同样可以嵌套另一种数据 结构, 即在数据结构描述项中设置子数据结构描述项, 从而支持复杂的数据类型。  The serialization description item includes a data structure description item and a field description item. A data structure description item is a set of field description items. A data structure description item may include multiple field description items, and another data structure may be nested in the data structure description item, that is, set in the data structure description item. Subdata structure description items support complex data types.
子数据结构描述项与数据结构描述项基本相同,主要区别在于子数据结构描述项中还需要包括 一种关系信息, 由这种关系信息建立位于同一数据结构描述项中的子数据结构描述项与字段描述项 的关系。  The sub-data structure description item is basically the same as the data structure description item. The main difference is that the sub-data structure description item also needs to include a relationship information, and the sub-data structure description item located in the same data structure description item is established by the relationship information. The field describes the relationship of the items.
各数据结构描述项的结构及数据结构描述项之中各字段描述项、 子数据描述项(若存在时)的 结构需要与第一结构相一致。 下面结合示例进行说明。  The structure of each data structure description item and the structure of each field description item and sub-data description item (if present) in the data structure description item need to be consistent with the first structure. The following is explained in conjunction with the examples.
示例性的, 当第一格式为 XML格式时, 具有第一结构的第一序列化消息 (XML序列化消息) 的一个例子如下:  Illustratively, when the first format is an XML format, an example of a first serialized message (XML serialized message) having a first structure is as follows:
<config> 〃指示根节点  <config> 〃 indicates the root node
<name></name> 〃指示节点元素  <name></name> 〃 indicates node element
<address></address> 〃指示节点元素  <address></address> 〃 indicates node element
<childs childName= " "> //指示嵌套的子根节点  <childs childName= " "> // indicates nested child root nodes
<childName></childName> 〃指示子节点属性  <childName></childName> 〃 indicates child attribute
< childAddress ></childAddress> 〃指示子节点元素  < childAddress ></childAddress> indicates the child element
</childs>  </childs>
</config>  </config>
上述 XML序列化消息的第一结构中, 根节点名称为 "config" , 根节点中包括名称为 "name" 和 " address"节点元素,该第一结构中还包括一个嵌套子结构,子结构中的子根节点的名称为 " childs" , 子根节点中包括名称为 "childName" 的节点属性和名称为 "childAddress" 的节点元素。  In the first structure of the above XML serialization message, the root node name is "config", and the root node includes node elements named "name" and "address", and the first structure further includes a nested substructure, substructure The child root node has the name "childs" and the child root node includes the node attribute named "childName" and the node element named "childAddress".
当第二格式为 PB格式时, 具有第二结构的第二序列化消息 (PB序列化消息) 的一个例子如 下:  An example of a second serialized message (PB serialized message) having a second structure when the second format is the PB format is as follows:
message config {  Message config {
optional string name = 1; 〃指示序号为 1的数据项  Optional string name = 1; 〃 indicates a data item with sequence number 1
optional string address = 2; 〃指示序号为 2的数据项  Optional string address = 2; 〃 indicates a data item with sequence number 2
message childs {  Message childs {
optional string name = 1; 〃指示序号为 1的数据项  Optional string name = 1; 〃 indicates a data item with sequence number 1
optional string address = 2; 〃指示序号为 2的数据项 } Optional string address = 2; 〃 indicates a data item with sequence number 2 }
optional childs childsEntity = 3; 〃指示序号为 3的子结构  Optional childs childsEntity = 3; 〃 indicates a substructure of sequence number 3
}  }
上述 PB序列化消息的第二结构中, 包括序号为 1、 数据类型为字符串 (string ) 的数据项和序 号为 2、 数据类型为字符串的数据项。 上述第二结构中还包括一个序号为 3的嵌套子结构, 该子结构 中包括序号为 1、 数据类型为字符串的数据项和序号为 2、 数据类型为字符串的数据项。  The second structure of the PB serialization message includes a data item whose sequence number is 1, a data type is a string (string), and a data item whose data type is a character string. The second structure further includes a nested sub-structure of sequence number 3. The sub-structure includes a data item whose sequence number is 1, a data type is a character string, and a data item whose serial number is 2. and the data type is a character string.
则按照 XML序列化消息的第一结构以及该第一结构与 PB序列化消息的第二结构之间的对应 关系, 利用设置的序列化描述项生成序列化数据结构, 可以表示如下:  Then, according to the first structure of the XML serialization message and the correspondence between the first structure and the second structure of the PB serialization message, the serialized data structure is generated by using the set serialization description item, which can be expressed as follows:
数据结构 { //指示数据结构描述项  Data structure { // indicates data structure description item
结构名称 = "config" 〃指示结构名称  Structure name = "config" 〃 indicates the structure name
字段 { 序号 =1, 名称 = "name" , type=String } //指示字段描述项  Field { sequence number =1, name = "name" , type=String } // indicates field description item
字段 { 序号 =2, 名称 = ' address" , type=String } 〃指示字段描述项  Field { sequence number = 2, name = ' address" , type=String } 〃 indicates field description item
数据结构{ 〃指示子数据结构描述项  Data structure { 〃 indicates sub-data structure description item
序号 =3, 〃指示关系信息  No. =3, 〃 indicates relationship information
结构名称 = "childs" 〃指示子结构名称  Structure name = "childs" 〃 indicates substructure name
字段 {序号 =1, 名称 = = " childName" , 节点类型= "属性" , type=String}〃指示字段 描述项  Field {serial number =1, name = = " childName" , node type = "property", type=String}〃 indicates the field description item
字段 {序号 =2, 名称 = " childAddress" , type=String } 〃指示字段描述项 Field {serial number = 2, name = " childAddress" , type=String } 〃 indicates field description item
} }
}  }
上述序列化数据结构中包括一个数据结构描述项, 该数据结构描述项中包括结构名称、 字段描 述项和子数据结构描述项。 结构名称为上述第一结构中根节点名称 " config" 。  The serialized data structure includes a data structure description item, and the data structure description item includes a structure name, a field description item, and a sub data structure description item. The structure name is the root node name "config" in the first structure above.
字段描述项包括数据类型 (type ) 、 序号、 节点类型和名称, 其中节点类型为默认的节点类型 (如节点元素)时可以省略。 字段描述项中的序号对应于 PB格式中的序号, 字段描述项中的序号为 该字段描述项中的数据在具有第二结构的 PB序列化消息中的序号, 如字段描述项中包括 "序号 =1 " 时, 表示该字段描述项中的数据对应于 PB格式中序号为 1的数据项; 节点类型字段描述项中的节点 类型包括节点元素和节点属性, 默认的节点类型为节点元素, 可省略不写; 字段描述项中的名称对 应于 XML格式中节点元素或节点属性的名称。  Field description items include data type (type), sequence number, node type, and name, where the node type is the default node type (such as a node element) and can be omitted. The sequence number in the field description item corresponds to the sequence number in the PB format, and the sequence number in the field description item is the sequence number of the data in the field description item in the PB serialization message having the second structure, and the field description item includes the “sequence number. =1 ", indicating that the data in the field description item corresponds to the data item with sequence number 1 in the PB format; the node type in the node type field description item includes the node element and the node attribute, and the default node type is the node element, Omitting or not; the name in the field description item corresponds to the name of the node element or node attribute in the XML format.
子数据结构描述项包括序号、 子结构名称和子字段描述项。 上述示例中利用子数据结构描述 项中的序号 (序号 =3 ) 建立该子数据结构描述项与同一序列化数据结构中字段描述项之间的关系, 上述示例的数据结构中排序第一位的是序号为 1 的字段描述项, 排序第二位的是序号为 2的字段描 述项的, 则可以获知序号为 3 的子数据结构描述项与前两个字段描述项的关系为位于前两个字段描 述项的排序之后。 子数据结构描述项中的子结构名称对应于 XML格式中子根节点的名称。 子字段描 述项中包括数据类型、 序号、 节点类型和名称, 子字段描述项中各项信息的作用与字段描述项中各 项信息的作用相同。  The sub-data structure description item includes a sequence number, a sub-structure name, and a sub-field description item. In the above example, the relationship between the sub-data structure description item and the field description item in the same serialized data structure is established by using the sequence number (serial number=3) in the sub-data structure description item, and the first-order data structure is sorted in the first place. It is a field description item with sequence number 1. If the second field is the field description item with sequence number 2, the relationship between the sub-data structure description item with sequence number 3 and the first two field description items is located in the first two. After the field description item is sorted. The substructure name in the subdata structure description item corresponds to the name of the subroot node in the XML format. The subfield description item includes the data type, sequence number, node type, and name. The information in the subfield description item has the same effect as the information in the field description item.
由上可知, 通过数据结构描述项中的结构名称、 子数据结构描述项中的子结构名称以及字段描 述项和子字段描述项中的序号、 名称建立了 XML序列化消息的第一结构与 PB序列化消息的第二结 构之间的对应关系; 对于不存在嵌套结构的场景, 通过数据结构描述项中的结构名称和字段描述项 中的序号、 名称建立 XML序列化消息的第一结构与 PB序列化消息的第二结构之间的对应关系; 即 通过数据结构描述项中的信息和字段描述项中的信息将对应关系设置在序列化数据结构中。 一种变 形方式中, 也可以仅通过字段描述项中的信息将对应关系设置在序列化数据结构中。  It can be seen from the above that the first structure and the PB sequence of the XML serialized message are established by the structure name in the data structure description item, the substructure name in the subdata structure description item, and the sequence number and the name in the field description item and the sub field description item. Corresponding relationship between the second structure of the message; for the scenario where the nested structure does not exist, the first structure and the PB of the XML serialization message are established by the structure name and the sequence number and the name in the field description item in the data structure description item The correspondence between the second structures of the serialized messages; that is, the correspondence is set in the serialized data structure by the information in the data structure description item and the information in the field description item. In a variant, the correspondence may also be set in the serialized data structure only by the information in the field description item.
上述数据结构描述项、 子数据结构描述项、 字段描述项和子字段描述项中的信息根据序列化消 息的格式和结构不同而发生变化, 在此不进行严格限定。  The information in the data structure description item, the sub-data structure description item, the field description item, and the sub-field description item are changed according to the format and structure of the serialization message, and are not strictly limited herein.
通过上述操作, 序列化数据结构创建完成后, 进入步骤 S 101。  After the serialization data structure is created by the above operation, the process proceeds to step S101.
S 101 : 对符合第一结构的第一格式的待转换序列化消息,根据序列化数据结构对该待转换序列 化消息进行解析, 将解析出的数据写入该序列化数据结构中, 得到数据写入后的序列化数据结构。  S101: parsing the to-be-converted serialized message according to the serialized data structure, and parsing the parsed data into the serialized data structure to obtain data Serialized data structure after writing.
例如, 在写入数据时, 可以在字段描述项或子字段描述项中增加一个数值 (value ) , 将解析 出的数据写入 value中。  For example, when writing data, you can add a value (value) to the field description item or subfield description item, and write the parsed data into the value.
S 102: 提取该数据写入后的序列化数据结构中的数据。  S102: Extract data in the serialized data structure after the data is written.
S 103 : 按照序列化数据结构中记录的对应关系,将提取出的数据转换为具有第二结构的第二格 式的序列化消息。  S103: Convert the extracted data into a serialized message of a second format having a second structure according to the correspondence recorded in the serialized data structure.
本发明又一个实施例以第一格式为 XML格式、第二格式为 PB格式的场景为例,对上述步骤 S 101 中的操作进行说明, 其中 XML格式的序列化消息具有第一结构, PB格式的序列化消息具有第二结 构; 对序列化消息之间的转换方法的其他内容参见本发明的其他实施例。 In another embodiment of the present invention, a scenario in which the first format is an XML format and the second format is a PB format is taken as an example, and the foregoing step S 101 is performed. The operation in the description is described in which the serialized message in the XML format has the first structure, and the serialized message in the PB format has the second structure; other contents of the conversion method between the serialized messages are referred to other embodiments of the present invention.
在通过上述步骤 S 100获取到序列化数据结构后, 符合第一结构的所有 XML序列化消息都可以 作为待转换序列化消息采用该序列化数据结构进行解析, 解析过程参见图 2, 示出了按照序列化数据 结构对 XML序列化消息进行解析的过程, 包括如下步骤:  After the serialized data structure is obtained through the above step S100, all the XML serialized messages conforming to the first structure can be parsed as the serialized data structure to be converted, and the parsing process is shown in FIG. 2, which shows The process of parsing an XML serialized message according to a serialized data structure includes the following steps:
S200: 服务器 (或其他设备) 加载待转换的 XML序列化消息。  S200: The server (or other device) loads the XML serialization message to be converted.
S201 : 服务器根据该待转换的 XML序列化消息校正根节点。  S201: The server corrects the root node according to the XML serialization message to be converted.
服务器采用流式解析法, 根据序列化数据结构对待转换的 XML序列化消息中的 XML元素进行 读取, 对读取出的待转换的 XML序列化消息中的 XML元素逐一进行处理。 当读取出的 XML元素 中的名称与数据结构描述项中的结构名称相同时, 确定该 XML元素对应的 XML节点为根节点。  The server uses the stream analysis method to read the XML elements in the XML serialization message to be converted according to the serialized data structure, and processes the XML elements in the read XML serialization message to be processed one by one. When the name in the read XML element is the same as the structure name in the data structure description item, it is determined that the XML node corresponding to the XML element is the root node.
通过校正根节点使 XML的节点和序列化数据结构中对应的字段描述项能够一致。  By correcting the root node, the nodes of the XML and the corresponding field description items in the serialized data structure can be made consistent.
S202: 服务器解析当前节点中的节点属性, 将作为节点属性的 XML元素的数据写入序列化数据 结构中对应的字段描述项中, 进入步骤 S203  S202: The server parses the node attribute in the current node, and writes the data of the XML element as the node attribute into the corresponding field description item in the serialized data structure, and proceeds to step S203.
当前节点为当前正在处理的从属或间接从属于根节点的 XML节点, 当前节点中的 XML元素从 属或间接从属于根节点。 对当前节点中的 XML元素的数据逐一解析, 将解析出来的数据写入序列化 数据结构。 当该 XML元素中的名称与字段描述项中的名称相同且该 XML元素的类型与字段描述项 中的节点类型相同时, 将解析出的该 XML元素的数据写入在该字段描述项中。  The current node is an XML node that is currently being processed or indirectly dependent on the root node, and the XML element in the current node is dependent or indirectly dependent on the root node. The data of the XML element in the current node is parsed one by one, and the parsed data is written into the serialized data structure. When the name in the XML element is the same as the name in the field description item and the type of the XML element is the same as the node type in the field description item, the parsed data of the XML element is written in the field description item.
XML元素的类型包括节点属性和节点元素, 本实施例在执行解析时, 先执行对当前处理的当前 节点中的节点属性的解析, 再执行对节点元素的解析, 然而, 本实施例并不对节点属性和节点元素 的解析顺序进行严格限定。  The type of the XML element includes a node attribute and a node element. In this embodiment, when the parsing is performed, the parsing of the node attribute in the currently processed current node is performed, and then the parsing of the node element is performed. However, this embodiment does not apply to the node. The order in which attributes and node elements are parsed is strictly limited.
例如, 对序列化数据结构中记录的如下字段描述项:  For example, describe the following field descriptions in the serialized data structure:
字段 {序号 =1, 名称 = "childName" , 节点类型= "属性" , type=String} 将解析出的节点属 性的数据写入该字段描述项后, 具体如下:  Field {serial number =1, name = "childName" , node type = "property", type=String} After writing the parsed node attribute data into the field description item, the details are as follows:
字段 {序号 = 1, 名称 = "childName" , 节点类型= "属性" , type=String, value= "XXX" } 其中, 上述 value中写入的 XXX表示解析出的作为节点属性的 XML元素的数据。  Field {serial number = 1, name = "childName", node type = "property", type=String, value= "XXX" } where XXX written in the above value represents the parsed data of the XML element as the node attribute .
S203 : 服务器解析当前节点中的节点元素, 进入步骤 S204  S203: The server parses the node element in the current node, and proceeds to step S204.
读取出当前处理 XML节点中的 XML元素的名称和 XML元素的数据。  Reads the name of the XML element currently in the XML node and the data of the XML element.
S204: 服务器判断是否存在嵌套结构, 若是, 返回步骤 S201 , 若否, 进入步骤 S205 根据该 XML元素的名称判断是否存在嵌套结构, 当读取出的 XML元素的名称与子数据结构描 述项中的子结构名称相同时, 确认存在嵌套结构。  S204: The server determines whether there is a nested structure, and if yes, returns to step S201, and if no, proceeds to step S205 to determine whether there is a nested structure according to the name of the XML element, and the name and sub-data structure description item of the read XML element When the substructure names in the same are the same, confirm that there is a nested structure.
其中, 对于上述存在嵌套结构的场景, 返回步骤 S201后, 将当前处理的 XML元素作为子根节 点 (用该子根节点替换根节点) , 读取子根节点中各 XML元素, 当子根节点中的 XML元素的名称 与子字段描述项(用子字段描述项替换字段描述项)中的名称相同且该 XML元素的类型与子字段描 述项中的节点类型相同时, 将解析出的该 XML元素的数据写入在该子字段描述项中。  For the scenario in which the nested structure exists, after returning to step S201, the currently processed XML element is used as a child root node (replace the root node with the child root node), and each XML element in the child root node is read, and the child root is read. The name of the XML element in the node will be the same as the name in the subfield description item (substituting the field description item with the subfield description item) and the type of the XML element is the same as the node type in the subfield description item. The data of the XML element is written in the subfield description item.
S205 : 服务器将作为节点元素的 XML元素的数据写入对应的字段描述符中, 进入步骤 S206 读取出的 XML元素的名称与字段描述符中的名称相同,并且该字段描述符中的节点类型为节点 元素时,确认该 XML元素对应一个节点元素,读取出该 XML元素的数据写入对应的字段描述符中。  S205: The server writes the data of the XML element as the node element into the corresponding field descriptor, and the name of the XML element read in step S206 is the same as the name in the field descriptor, and the node type in the field descriptor When it is a node element, it is confirmed that the XML element corresponds to one node element, and the data of the XML element is read and written into the corresponding field descriptor.
S206: 判断读取出的待转换的 XML序列化消息中的 XML元素的数据是否都已经写入至相应的 字段描述项或子字段描述项, 若是, 进入步骤 S207 , 若否, 返回步骤 S201 , 重复执行上述步骤 S201 至 S206, 将未写入的 XML元素写入字段描述项或子字段描述项中, 直至判断出 XML序列化消息中 的所有数据都写入序列化结构中, 进入步骤 S207  S206: determining whether the data of the XML element in the read XML serialization message to be converted has been written to the corresponding field description item or sub-field description item, and if yes, proceeding to step S207, if no, returning to step S201, Repeating the above steps S201 to S206, writing the unwritten XML element into the field description item or the sub-field description item until it is determined that all the data in the XML serialization message is written in the serialization structure, and proceeding to step S207
S207: 由写入 XML元素的数据后的字段描述项或子字段描述项得到数据写入后的序列化数据结 构, 结束操作。  S207: The serialized data structure after the data is written is obtained by the field description item or the sub-field description item after the data of the XML element is written, and the operation ends.
本发明又一个实施例以第一格式为 XML格式、 第二格式为 PB格式的场景为例, 主要对从数据 写入后的序列化结构中提取出数据并转换为具有第二结构的 PB序列化消息的操作进行说明。对序列 化消息之间的转换方法的其他内容参见本发明的其他实施例。  In another embodiment of the present invention, a scene in which the first format is an XML format and the second format is a PB format is taken as an example, and data is extracted from a serialized structure after data writing and converted into a PB sequence having a second structure. The operation of the message is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
在将 XML序列化消息中存储的数据写入序列化数据结构中后, 需要将数据结构中的数据以 PB 格式输出, 最终完成从 XML格式的序列化消息到 PB格式的序列化消息的转换, 在执行转换时遵循 PB的协议规范。 参见图 3, 示出了本实施例提供的从数据写入后的序列化数据结构中输出 PB序列 化消息的流程图, 具体包括如下步骤:  After the data stored in the XML serialization message is written into the serialized data structure, the data in the data structure needs to be output in PB format, and finally the serialized message from the XML format to the serialized message in the PB format is converted. Follow the PB protocol specification when performing the conversion. Referring to FIG. 3, a flow chart of outputting a PB serialization message from a serialized data structure after data writing according to the embodiment is shown, which specifically includes the following steps:
S300: 服务器 (或其他设备) 启动 PB序列化消息的输出, 进入步骤 S301  S300: The server (or other device) starts the output of the PB serialization message, and proceeds to step S301.
服务器进入 PB格式的输出模式, 在该输出模式下, 服务器输出 PB序列化消息。 服务器加载数 据写入后的序列化数据结构以执行格式转换。 The server enters an output mode of the PB format, in which the server outputs a PB serialization message. Server load count The serialized data structure is written to perform format conversion.
S301 : 服务器从数据写入后的序列化数据结构中提取出字段描述项中的数据和子数据结构描述 项 (如果存在时) 中的数据, 进入步骤 S302。  S301: The server extracts the data in the field description item and the data in the sub-data structure description item (if any) from the serialized data structure after the data is written, and proceeds to step S302.
S302: 服务器根据提取出的数据的不同类型执行不同操作。  S302: The server performs different operations according to different types of extracted data.
当提取出的数据为数值类型的数据时, 进入步骤 S303;  When the extracted data is a numeric type of data, the process proceeds to step S303;
当提取出的数据为字符串类型的数据时, 进入步骤 S304;  When the extracted data is string type data, the process proceeds to step S304;
当提取出的数据为子数据结构描述项时, 进入步骤 S305。  When the extracted data is a sub data structure description item, the process proceeds to step S305.
S303 : 当提取出的数据为数值类型的数据时, 采用下述第一方式进行处理: 以变量 (Varint)方式 先将字段描述项中记录的该数据的序号与数据类型写入 PB序列化消息中相同序号对应的数据项中, 再将数值类型的数据写入该数据项中。  S303: When the extracted data is a numeric type of data, the first manner is used for processing: first, in a variable (Varint) manner, the serial number and the data type of the data recorded in the field description item are first written into the PB serialized message. In the data item corresponding to the same serial number, data of the numerical type is written into the data item.
S304: 当提取出的数据为字符串类型的数据时, 采用下述第二方式进行处理: 以 Varint方式先 将字段描述项中记录的该数据的序号与数据类型写入 PB序列化消息中相同序号对应的数据项中,再 将字段描述项中记录的该数据的字符串长度写入该数据项中, 然后将字符串类型的数据写入该数据 项中。  S304: When the extracted data is a string type data, the following method is used to process: first, in the Varint manner, the sequence number of the data recorded in the field description item is the same as the data type written in the PB serialization message. In the data item corresponding to the serial number, the string length of the data recorded in the field description item is written into the data item, and then the data of the string type is written into the data item.
S305 : 当提取出的数据为子数据结构描述项时, 以 Varint方式先将该子数据结构描述项中的序 号和结构类型(该结构类型指示子数据结构)写入 PB序列化消息中相同序号对应的数据项中, 再将 该子数据结构描述项中数据长度写入该数据项中, 然后返回步骤 S301, 采用与步骤 S301至 S304中 相同的方法对子数据结构描述项中的数据进行处理, 区别点主要在于将步骤 S301至 S304中的字段 描述项替换为子字段描述项, 即当提取出的内容为数值类型的数据时, 按照上述第一方式, 以 Varint 方式先将子字段描述项中记录的该数据的序号与数据类型写入 PB 序列化消息中相同序号对应的数 据项中, 再将数值类型的数据写入该数据项中; 当提取出的数据为字符串类型的数据时, 按照上述 第二方式,以 Varint方式先将子字段描述项中记录的该数据的序号与数据类型写入 PB序列化消息中 相同序号对应的数据项中, 再将子字段描述项中记录的该数据的字符串长度写入该数据项中, 然后 将字符串类型的数据写入该数据项中。  S305: When the extracted data is a sub-data structure description item, the sequence number and the structure type (the structure type indication sub-data structure) in the sub-data structure description item are first written into the PB serialization message in the Varint manner. In the corresponding data item, the data length in the sub-data structure description item is written into the data item, and then returns to step S301, and the data in the sub-data structure description item is processed in the same manner as in steps S301 to S304. The difference is mainly to replace the field description item in steps S301 to S304 with the sub-field description item, that is, when the extracted content is numeric type data, according to the first manner, the sub-field description item is firstly introduced in the Varint manner. The serial number of the data recorded in the data type is written in the data item corresponding to the same serial number in the PB serialization message, and the data of the numerical type is written into the data item; when the extracted data is the data of the string type According to the second manner described above, the sequence number and data type of the data recorded in the subfield description item are first described in the Varint manner. PB message into sequences of the same sequence number corresponding to the data item, then the length of the substring description entry field data recorded in the data item is written, and then writes the character string data type in the data item.
S306: 判断提取出的数据是否都已经写入 PB 序列化消息中对应的数据项中, 若是, 进入步骤 S307 , 若否, 返回步骤 S302, 重复执行上述步骤 S302至 S306, 根据未写入的数据的数据类型, 采 用第一方式或第二方式, 将未写入的数据写入对应的数据项中, 直至对序列化数据结构中的所有数 据处理完毕,  S306: determining whether the extracted data has been written into the corresponding data item in the PB serialization message, and if yes, proceeding to step S307, if not, returning to step S302, repeating the above steps S302 to S306, according to the unwritten data The data type, in the first mode or the second mode, writes the unwritten data into the corresponding data item until all the data in the serialized data structure is processed.
S307: 由写入数据后的各数据项得到具有第二结构的 PB序列化消息。  S307: Obtain a PB serialized message having a second structure from each data item after the data is written.
至此, 完成从 XML序列化消息到 PB序列化消息的转换, 结束操作。  At this point, the conversion from the XML serialization message to the PB serialization message is completed, and the operation ends.
本发明又一个实施例以第一格式为 PB格式、 第二格式为 XML格式的场景为例, 主要对解析第 一结构的 PB格式的序列化消息中的数据, 将解析出的数据写入序列化数据结构的操作进行说明。 序 列化消息之间的转换方法的其他内容参见本发明的其他实施例。  In another embodiment of the present invention, a scenario in which the first format is a PB format and the second format is an XML format is used as an example, and the data in the serialized message of the PB format of the first structure is parsed, and the parsed data is written into the sequence. The operation of the data structure is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
在获取到序列化数据结构后,符合第一结构的所有 PB序列化消息都可以采用该序列化数据结构 进行解析, 解析过程参见图 4, 示出了按照序列化数据结构对 PB序列化消息进行解析的过程, 包括 如下步骤:  After obtaining the serialized data structure, all PB serialized messages conforming to the first structure can be parsed by using the serialized data structure. The parsing process is shown in FIG. 4, which shows that the PB serialized message is performed according to the serialized data structure. The process of parsing includes the following steps:
S400: 服务器加载 PB待转换序列化消息的二进制数据流。  S400: The server loads the binary data stream of the PB to be converted serialized message.
S401 : 服务器逐个读取 PB待转换序列化消息的二进制数据流中的变量 (Varint )值。  S401: The server reads the variable (Varint) value in the binary data stream of the PB to be converted serialized message one by one.
S402: 服务器根据提取出的 Varint值的不同类型执行不同操作。  S402: The server performs different operations according to different types of extracted Varint values.
服务器首先会按照序列化数据结构中记录的对应关系, 判断提取出的数据的数据类型, 例如, 服务器根据字段描述项中的数据类型即可获知该字段描述项中写入的数据的数据类型。 具体操作时, 服务器从读取到的变量值中获取该变量值对应的数据类型和序号。根据 Varint编码标准, Varint值的 二进制数据后 3比特位放置数据类型, 剩余的比特位放置序号。  The server first determines the data type of the extracted data according to the correspondence recorded in the serialized data structure. For example, the server can know the data type of the data written in the field description item according to the data type in the field description item. In the specific operation, the server obtains the data type and sequence number corresponding to the variable value from the read variable value. According to the Varint coding standard, the data of the last 3 bits of the Varint value is placed in the data type, and the remaining bits are placed in the sequence number.
当读取出的 Varint值为数值类型时, 进入步骤 S403 ;  When the read Varint value is a numeric type, the process proceeds to step S403;
当读取出的 Varint值为字符串类型时, 进入步骤 S404;  When the read Varint value is a string type, proceed to step S404;
当读取出的 Varint值为子数据结构描述项时, 进入步骤 S405。  When the read Varint value is a sub data structure description item, the process proceeds to step S405.
在此, 当读取出的 Varint值的序号与子数据结构描述项的序号相同时, 判定读取出的 Varint值 为子数据结构描述项。  Here, when the sequence number of the read Varint value is the same as the sequence number of the sub data structure description item, it is determined that the read Varint value is a sub data structure description item.
S403 : 当读取出的 Varint值为数值类型时, 采用下述第三方式进行处理: 继续读取下一个 Varint 值 (该下一个 Varint值为数值类型的数据) , 将该下一个 Varint值写入序列化数据结构中序号与该 Varint值的序号相同的字段描述项中。 由于序列化数据结构中记录了 PB格式中数据项的序号和数据 类型, 并且序列化数据结构中各数据项的排序也与待转换的 PB序列化消息中各数据项的排序相同, 从而根据序列化结构能够将 PB序列化消息中的各数据项快速读取出来,根据数据类型和序号将读取 出的的数据写入序列化数据结构中。 S403: When the read Varint value is a numeric type, the following method is used: continue to read the next Varint value (the next Varint value is a numeric type data), and write the next Varint value. In the serialized data structure, the sequence number is the same as the sequence number of the Varint value. Since the sequence number and the data type of the data item in the PB format are recorded in the serialized data structure, and the order of each data item in the serialized data structure is also the same as the order of each data item in the PB serialized message to be converted, Therefore, according to the serialization structure, each data item in the PB serialization message can be quickly read out, and the read data is written into the serialized data structure according to the data type and the serial number.
S404:当读取出的 Varint值为字符串类型时,采用下述第四方式进行处理:继续读取下一个 Varint 值, 得到字符串长度, 然后再读取字符串长度大小的字节, 对读取到的字节进行 UTF8 ( 8-bit Unicode Transformation Format ) 编码, 得到字符串, 将该字符串写入序列化数据结构中序号与该字符串的序 号相同的字段描述项中。  S404: When the read Varint value is a string type, the fourth method is used for processing: continue to read the next Varint value, obtain the length of the string, and then read the byte length of the string, The read byte is encoded in UTF8 (8-bit Unicode Transformation Format) to obtain a string, which is written in the field description item whose sequence number is the same as the serial number of the string in the serialized data structure.
S405 : 当读取出的 Varint值的序号与子数据结构描述项的序号相同时, 继续读取下一个 Varint 值, 得到数据长度, 然后再读取数据长度大小的字节得到字节码集合, 返回步骤 S401。 采用与步骤 S401至 S404中相同的方法对子数据结构描述项中的数据进行处理, 区别点主要在于将步骤 S401至 S404中的字段描述项替换为子字段描述项,即当从该字节码集合中读取出的 Varint值为数值类型时, 采用上述第三方式进行处理, 采用下述第三方式进行处理: 继续读取下一个 Varint值, 将该下一个 Varint值写入序列化数据结构中序号与该 Varint值的序号相同的子字段描述项中。当从该字节码集合 中读取出的 Varint值为字符串类型时, 采用上述第四方式进行处理: 继续读取下一个 Varint值, 得 到字符串长度, 然后再读取字符串长度大小的字节, 对读取到的字节进行 UTF8编码, 得到字符串, 将该字符串写入序列化数据结构中序号与该字符串的序号相同的子字段描述项中。  S405: when the sequence number of the read Varint value is the same as the sequence number of the sub-data structure description item, continue to read the next Varint value, obtain the data length, and then read the byte of the data length size to obtain the bytecode set. Go back to step S401. The data in the sub-data structure description item is processed in the same manner as in the steps S401 to S404, and the difference mainly lies in replacing the field description item in the steps S401 to S404 with the sub-field description item, that is, when the byte code is from the byte code When the Varint value read in the set is a numeric type, the third method is used for processing, and the third method is used for processing: continue reading the next Varint value, and writing the next Varint value to the serialized data structure. The subfield is in the same subfield description as the sequence number of the Varint value. When the Varint value read from the bytecode set is a string type, the fourth method is used for processing: continue to read the next Varint value, obtain the length of the string, and then read the length of the string. Byte, UTF8 encoding the read byte, obtaining a string, and writing the string into the subfield description item whose sequence number is the same as the serial number of the string in the serialized data structure.
S406:判断 PB待转换序列化消息中的所有 Varint值是否都已经写入字段描述项或子字段描述项 中, 若是, 进入步骤 S407, 若否, 返回步骤 S402, 重复执行上述步骤 S402至 S406, 根据未写入的 Varint值的数据类型, 采用第三方式或第四方式将未写入的变量值写入字段描述项或子字段描述项 中, 直至 PB序列化消息中的所有数据都写入序列化结构中, 进入步骤 S407  S406: determining whether all the Varint values in the PB to be converted serialized message have been written in the field description item or the subfield description item, if yes, proceeding to step S407, if not, returning to step S402, repeating the above steps S402 to S406, According to the data type of the unwritten Varint value, the unwritten variable value is written into the field description item or the subfield description item in the third mode or the fourth mode until all the data in the PB serialization message is written. In the serialized structure, proceed to step S407.
S407:由写入 Varint值后的字段描述项或子字段描述项得到数据写入后的序列化数据结构。 结束 操作。  S407: The serialized data structure after the data is written by the field description item or the sub-field description item after the Varint value is written. End the operation.
本发明又一个实施例以第一格式为 PB格式、 第二格式为 XML格式的场景为例, 对按照序列化 数据结构中的对应关系, 将提取出的数据转换为第二结构的 XML序列化消息的操作进行说明。 序列 化消息之间的转换方法的其他内容参见本发明的其他实施例。  In another embodiment of the present invention, a scenario in which the first format is a PB format and the second format is an XML format is taken as an example, and the extracted data is converted into an XML serialization of the second structure according to the correspondence in the serialized data structure. The operation of the message is explained. Further details of the method of converting between serialized messages are found in other embodiments of the invention.
在将 PB序列化消息中存储的数据写入序列化数据结构中后, 需要将数据结构中的数据以 XML 格式输出, 最终完成从 PB格式到 XML格式的转换, 参见图 5, 示出了本实施例提供的从数据写入 后的序列化数据结构中输出 XML序列化消息的流程图, 具体包括如下步骤:  After the data stored in the PB serialization message is written into the serialized data structure, the data in the data structure needs to be output in XML format, and finally the conversion from the PB format to the XML format is completed. Referring to FIG. 5, the present embodiment is shown. The flowchart for outputting the XML serialization message from the serialized data structure after data writing provided by the embodiment includes the following steps:
S500: 服务器 (或其他设备) 启动 XML序列化消息的输出, 进入步骤 S501  S500: The server (or other device) starts the output of the XML serialization message, and proceeds to step S501.
服务器进入 XML格式的输出模式, 在该输出模式下, 服务器输出 XML序列化消息。 服务器加 载数据写入后的序列化数据结构以执行格式转换。  The server enters an output mode in XML format, in which the server outputs an XML serialized message. The server loads the serialized data structure after the data is written to perform format conversion.
S501 : 服务器创建 XML根节点, 进入步骤 S502  S501: The server creates an XML root node, and proceeds to step S502.
服务器首先会按照序列化数据结构中记录的对应关系, 判断提取出的数据是否为结构名称以及 判断提取出的数据的节点类型。 当提取出的数据中的名称与序列化数据结构记录的结构名称相同时, 以该数据和结构名称, 创建 XML序列化消息中的根节点。  The server first determines whether the extracted data is the structure name and the node type for judging the extracted data according to the correspondence recorded in the serialized data structure. When the name in the extracted data is the same as the structure name of the serialized data structure record, the root node in the XML serialization message is created with the data and structure name.
S502: 服务器从数据写入后的序列化数据结构中提取出字段描述项中的数据和子数据结构描述 项 (如果存在时) , 进入步骤 S503  S502: The server extracts data and sub-data structure description items (if any) in the field description item from the serialized data structure after the data is written, and proceeds to step S503.
S503 : 服务器创建 XML节点属性, 将数据写入该 XML节点属性中, 进入步骤 S504 通过提取出的数据所属的字段描述项或子字段描述项中记录的节点类型可以获知该数据对应于 节点元素还是节点属性。  S503: The server creates an XML node attribute, and writes data into the XML node attribute, and proceeds to step S504 to learn whether the data corresponds to the node element by using the field description item or the node type recorded in the sub-field description item to which the extracted data belongs. Node properties.
当提取出的数据为节点属性时, 采用下述第六方式进行处理: 在当前节点 (当前处理的直接或 间接从属于根节点的节点)下以节点属性的字段描述项中的名称创建 XML节点属性, 并将字段描述 项中的数据写入该 XML节点属性中。  When the extracted data is a node attribute, the following sixth manner is used for processing: Creating an XML node with the name in the field description item of the node attribute under the current node (the currently processed node directly or indirectly subordinate to the root node) Attribute, and write the data in the field description item to the XML node attribute.
本实施例在执行转换时, 先创建 XML序列化消息中的节点属性, 再创建节点元素, 然而, 本实 施例并不对节点属性和节点元素的创建顺序进行严格限定。  In this embodiment, when performing the conversion, the node attribute in the XML serialization message is created first, and then the node element is created. However, this embodiment does not strictly define the node attribute and the order in which the node element is created.
S504: 服务器创建 XML节点元素。  S504: The server creates an XML node element.
当提取出的数据为节点元素时, 对不具有嵌套结构的场景, 采用下述第五方式进行处理: 在根 节点下以节点元素的字段描述项中的名称创建 XML节点,并将字段描述项或子字段描述项中的数据 写入该 XML节点中。  When the extracted data is a node element, the scene with no nested structure is processed in the following fifth way: Under the root node, an XML node is created with the name in the field description item of the node element, and the field description is performed. The data in the item or subfield description item is written to the XML node.
对具有嵌套结构的场景,在本步骤中在根节点下以节点元素的字段描述项中的名称创建 XML节 点元素后, 进入步骤 S505  For a scenario with a nested structure, in this step, after creating an XML node element under the root node with the name in the field description item of the node element, proceed to step S505.
S505 : 服务器判断节点是否为嵌套结构, 若是, 返回步骤 S502, 若否, 进入步骤 S506。 当提取 出的数据中包括子结构名称时获知该数据对应于子数据结构描述项。 当提取出的数据为子数据结构描述项时, 即节点包括嵌套结构时, 以该子数据结构描述项中的 名称创建子根节点, 提取子数据结构描述项中的数据, 采用与步骤 S502至 S504中相同的方法对子 数据结构描述项中的数据进行处理, 区别点主要在于将步骤 S501至 S504中的字段描述项替换为子 字段描述项, 即当提取出的数据为节点元素时, 在子根节点下以节点元素的子字段描述项中的名称 创建 XML节点, 并将子字段描述项中的数据写入该 XML节点中。 当提取出的数据为节点属性时, 在当前节点下以节点属性的子字段描述项中的名称创建 XML节点属性,并将子字段描述项中的数据 写入该 XML节点属性中。 S505: The server determines whether the node is a nested structure, and if yes, returns to step S502, and if no, proceeds to step S506. When the extracted structure includes the substructure name, it is known that the data corresponds to the sub data structure description item. When the extracted data is a sub-data structure description item, that is, when the node includes a nested structure, the sub-root node is created by using the name in the sub-data structure description item, and the data in the sub-data structure description item is extracted, and step S502 is adopted. The same method in S504 processes the data in the sub-data structure description item, the main difference is that the field description items in steps S501 to S504 are replaced with sub-field description items, that is, when the extracted data is a node element, An XML node is created under the child root node with the name in the subfield description item of the node element, and the data in the subfield description item is written into the XML node. When the extracted data is a node attribute, an XML node attribute is created under the current node with the name in the sub-field description item of the node attribute, and the data in the sub-field description item is written into the XML node attribute.
S506: 服务器将字段描述项 (或子字段描述项) 中的数据写入该 XML节点中。  S506: The server writes data in the field description item (or sub-field description item) into the XML node.
S507:判断提取出的数据是否都已经写入至相应的 XML节点元素或 XML节点属性中, 若是, 进 入步骤 S508, 若否, 返回步骤 S502, 重复执行上述步骤 S502至 S507, 根据未写入的数据的节点类 型, 采用上述第五方式或第六方式将未写入的数据写入相应的 XML节点元素或 XML节点属性中, 直至对数据写入后的序列化数据结构中的所有数据处理完毕, 进入步骤 S508。  S507: determining whether the extracted data has been written into the corresponding XML node element or the XML node attribute, and if yes, proceeding to step S508, if not, returning to step S502, repeating the above steps S502 to S507, according to the unwritten The node type of the data, the unwritten data is written into the corresponding XML node element or the XML node attribute by using the fifth mode or the sixth mode described above, until all the data in the serialized data structure after the data is written is processed. Go to step S508.
S 508:由数据写入后的 XML节点元素或 XML节点属性得到具有第二结构的 XML序列化消息, 结束操作。 本发明又一个实施例提供了一种序列化消息之间的转换装置 600。 参见图 6, 该装置 600包括数 据结构生成单元 601、解析写入单元 602、数据提取单元 603和转换单元 604。 数据结构生成单元 601 包括对应关系设置单元 601 1和子结构设置单元 6012。  S 508: The XML serialization message with the second structure is obtained from the XML node element or the XML node attribute after the data is written, and the operation ends. Yet another embodiment of the present invention provides a conversion apparatus 600 between serialized messages. Referring to Figure 6, the apparatus 600 includes a data structure generating unit 601, a parsing write unit 602, a data extracting unit 603, and a converting unit 604. The data structure generating unit 601 includes a correspondence relationship setting unit 601 1 and a sub-structure setting unit 6012.
数据结构生成单元 601 用于按照第一序列化消息的第一结构以及第一结构与第二序列化消息的 第二结构之间的对应关系, 利用设置的序列化描述项生成序列化数据结构; 其中, 第一序列化消息 具有第一格式, 第二序列化消息具有第二格式; 序列化描述项包括数据结构描述项和设置在数据结 构描述项之中的字段描述项, 各数据结构描述项的结构及数据结构描述项之中各字段描述项的结构 与第一结构相一致。 具体的, 数据结构生成单元 601 包括对应关系设置单元 601 1, 用于按照第一序 列化消息的第一结构以及第一结构与第二序列化消息的第二结构之间的对应关系, 利用序列化描述 项的数据结构描述项中的信息和字段描述项中的信息将对应关系设置在生成的序列化数据结构中。 第一格式为 XML格式, 第二格式为 PB格式时, 或者, 第一格式为 PB格式, 第二格式为 XML格式 时, 对应关系设置单元 601 1, 用于在数据结构描述项中设置结构名称和字段描述项, 该结构名称为 具有第一结构的 XML序列化消息中根节点的名称, 该字段描述项中包括数据类型、 序号、 节点类型 和名称, 序号为字段描述项中的数据在具有第二结构的 PB序列化消息中的序号; 通过数据结构描述 项中的结构名称、 字段描述项中的序号和名称将对应关系设置在生成的序列化数据结构中。  The data structure generating unit 601 is configured to generate a serialized data structure by using the set serialization description item according to the first structure of the first serialization message and the correspondence between the first structure and the second structure of the second serialization message; The first serialized message has a first format, and the second serialized message has a second format. The serialized description item includes a data structure description item and a field description item set in the data structure description item, and each data structure description item The structure of each field description item in the structure and data structure description item is consistent with the first structure. Specifically, the data structure generating unit 601 includes a correspondence relationship setting unit 601 1 for using the sequence according to the first structure of the first serialization message and the correspondence between the first structure and the second structure of the second serialization message. The information in the data structure description item of the description item and the information in the field description item are set in the generated serialized data structure. When the first format is the XML format, the second format is the PB format, or the first format is the PB format, and the second format is the XML format, the correspondence relationship setting unit 601 1 is configured to set the structure name in the data structure description item. And a field description item, the structure name is a name of a root node in the XML serialization message having the first structure, the field description item includes a data type, a sequence number, a node type, and a name, and the serial number is a data in the field description item. The sequence number in the PB serialization message of the two structure; the correspondence relationship is set in the generated serialized data structure by the structure name in the data structure description item, the sequence number and the name in the field description item.
数据结构生成单元 601还包括子结构设置单元 6012, 用于在数据结构描述项中设置子数据结构 描述项, 其中, 子数据结构描述项中设置有子字段描述项, 对应关系包括位于同一数据结构描述项 中的子数据结构描述项与字段描述项的关系。 则对应关系设置单元 601 1, 用于在数据结构描述项中 设置结构名称、 字段描述项和子数据结构描述项, 结构名称为具有第一结构的 XML序列化消息中根 节点的名称, 子数据结构描述项中包括序号、 子结构名称和子字段描述项, 字段描述项或子字段描 述项中包括数据类型、 序号、 节点类型和名称, 序号为字段描述项或子字段描述项中的数据在具有 第二结构的 PB序列化消息中的序号; 通过数据结构描述项中的结构名称、 子数据结构描述项中的子 结构名称、 字段描述项和子字段描述项中的序号和名称将对应关系设置在生成的序列化数据结构中。  The data structure generating unit 601 further includes a sub-structure setting unit 6012, configured to set a sub-data structure description item in the data structure description item, where the sub-data structure description item is provided with a sub-field description item, and the corresponding relationship includes the same data structure. The relationship between the subdata structure description item in the description item and the field description item. The correspondence relationship setting unit 601 1 is configured to set a structure name, a field description item, and a sub data structure description item in the data structure description item, where the structure name is a name of the root node in the XML serialization message having the first structure, and the sub data structure description The item includes a sequence number, a substructure name, and a subfield description item, and the field description item or the subfield description item includes a data type, a sequence number, a node type, and a name, and the serial number is a field description item or the data in the subfield description item has a second The sequence number in the structured PB serialization message; the corresponding relationship is set in the generated structure by the structure name in the data structure description item, the substructure name in the subdata structure description item, the field description item, and the sequence number and name in the subfield description item. Serialized in a data structure.
当第一格式为 XML格式, 第二格式为 PB格式时, 解析写入单元 602用于根据序列化数据结构 对待转换的 XML序列化消息中的 XML元素进行读取, 当读取出的 XML元素中的名称与数据结构 描述项中的结构名称相同时, 确定 XML元素对应的 XML节点为根节点; 读取从属或间接从属于根 节点的各 XML元素, 当 XML元素中的名称与字段描述项中的名称相同且 XML元素的类型与字段 描述项中的节点类型相同时, 将解析出的 XML元素的数据写入在字段描述项中; 当 XML元素中的 名称与子数据结构描述项中的子结构名称相同时, 将 XML元素作为子根节点, 读取从属或间接从属 于子根节点的各 XML元素, 当从属或间接从属于子根节点的 XML元素的名称与子字段描述项中的 名称相同且 XML元素的类型与子字段描述项中的节点类型相同时, 将解析出的 XML元素的数据写 入在子字段描述项中; 判断读取出的待转换的 XML序列化消息中的 XML元素的数据是否都已经写 入至相应的字段描述项或子字段描述项, 若是, 由写入 XML元素的数据后的字段描述项或子字段描 述项得到数据写入后的序列化数据结构, 若否, 将未写入的 XML元素写入字段描述项或子字段描述 项中, 该字段描述项或子字段描述项的名称与节点类型与未写入的 XML元素的名称和类型相同。  When the first format is the XML format and the second format is the PB format, the parsing and writing unit 602 is configured to read the XML element in the XML serialized message to be converted according to the serialized data structure, when the read XML element When the name in the name is the same as the structure name in the data structure description item, the XML node corresponding to the XML element is determined to be the root node; the XML elements that are dependent or indirectly subordinate to the root node are read, and the name and the field description item in the XML element are When the names in the same are the same and the type of the XML element is the same as the node type in the field description item, the data of the parsed XML element is written in the field description item; when the name in the XML element and the sub-data structure description item When the substructure names are the same, the XML element is used as a sub-root node, and each XML element subordinate to or indirectly subordinate to the sub-root node is read, and the name and sub-field description item in the sub-field description item are subordinate or indirectly subordinate to the sub-root node. When the name is the same and the type of the XML element is the same as the node type in the subfield description item, the data of the parsed XML element is written in the subfield. In the description item, determining whether the data of the XML element in the read XML serialization message to be converted has been written to the corresponding field description item or sub-field description item, if yes, after writing the data of the XML element The field description item or the sub-field description item obtains the serialized data structure after the data is written. If not, the unwritten XML element is written in the field description item or the sub-field description item, and the field description item or sub-field description item The name and type of the node are the same as the name and type of the unwritten XML element.
转换单元 604, 用于按照序列化数据结构中记录的对应关系, 判断提取出的数据的数据类型; 当 提取出的数据为数值类型的数据时, 采用下述第一方式进行处理: 以变量方式先将字段描述项或子 字段描述项中记录的数据的序号与数据类型写入 PB序列化消息中相同序号对应的数据项中,再将数 值类型的数据写入数据项中; 当提取出的数据为字符串类型的数据时, 采用下述第二方式进行处理: 以变量方式先将字段描述项或子字段描述项中记录的数据的序号与数据类型写入 PB 序列化消息中 相同序号对应的数据项中, 再将字段描述项或子字段描述项中记录的数据的字符串长度写入数据项 中, 然后将字符串类型的数据写入数据项中; 当提取出的数据为子数据结构描述项时, 以变量方式 先将子数据结构描述项中的序号和结构类型写入 PB序列化消息中相同序号对应的数据项中,再将子 数据结构描述项中数据长度写入数据项中, 然后提取子数据结构描述项的子字段描述项中的数据, 当提取出的数据为数值类型的数据时, 采用第一方式进行处理, 当提取出的数据为字符串类型的数 据时,采用第二方式进行处理;判断提取出的数据是否都已经写入 PB序列化消息中对应的数据项中, 若是, 由写入数据后的各数据项得到具有第二结构的第二格式的序列化消息, 若否, 根据未写入的 数据的数据类型, 采用第一方式或第二方式, 将未写入的数据写入对应的数据项中。 The converting unit 604 is configured to determine, according to the correspondence relationship recorded in the serialized data structure, the data type of the extracted data; when the extracted data is a numerical type data, the first manner is performed by using the following method: First describe the item or sub The sequence number of the data recorded in the field description item is written in the data item corresponding to the same sequence number in the PB serialization message, and the data of the value type is written into the data item; when the extracted data is a string type data The second method is used to process: first, in the variable manner, first input the sequence number and data type of the data recorded in the field description item or the sub-field description item into the data item corresponding to the same serial number in the PB serialization message, and then The string length of the data recorded in the field description item or subfield description item is written in the data item, and then the data of the string type is written into the data item; when the extracted data is a sub data structure description item, the variable is The method first writes the sequence number and structure type in the sub-data structure description item into the data item corresponding to the same serial number in the PB serialization message, and then writes the data length in the sub-data structure description item into the data item, and then extracts the sub-data structure. The sub-field description item of the item describes the data in the item. When the extracted data is a numeric type of data, the first method is used for processing. When the extracted data is a string type data, the second method is used for processing; whether the extracted data has been written into the corresponding data item in the PB serialization message, and if so, the data after the data is written The item obtains the serialized message of the second format having the second structure. If not, according to the data type of the unwritten data, the unwritten data is written into the corresponding data item according to the first mode or the second mode. .
当第一格式为 PB格式, 第二格式为 XML格式时, 解析写入单元 602, 用于逐个读取 PB待转换 序列化消息的二进制数据流中的变量值; 从读取到的变量值中获取变量值对应的数据类型和序号; 当读取出的变量值为数值类型时, 采用下述第三方式进行处理: 继续读取下一个变量值, 将下一个 变量值写入序列化数据结构中序号与变量值的序号相同的字段描述项或子字段描述项中, 当读取出 的变量值为字符串类型时, 采用下述第四方式进行处理: 继续读取下一个变量值, 得到字符串长度, 然后再读取字符串长度大小的字节, 得到字符串, 将字符串写入序列化数据结构中序号与字符串的 序号相同的字段描述项或子字段描述项中; 当读取出的变量值的序号与子数据结构描述项的序号相 同时, 继续读取下一个变量值, 得到数据长度, 然后再读取数据长度大小的字节得到字节码集合, 以及, 读取字节码集合中的变量值, 当读取出的变量值为数值类型时, 采用第三方式进行处理, 当 读取出的变量值为字符串类型时, 采用第四方式进行处理; 判断 PB待转换序列化消息中的所有变量 值是否都已经写入字段描述项或子字段描述项中, 若是, 由写入变量值后的字段描述项或子字段描 述项得到数据写入后的序列化数据结构, 若否, 根据未写入的变量值的数据类型, 采用第三方式或 第四方式将未写入的变量值写入字段描述项或子字段描述项中。  When the first format is the PB format and the second format is the XML format, the parsing and writing unit 602 is configured to read the variable values in the binary data stream of the PB to be converted serialized message one by one; from the read variable values Obtain the data type and serial number corresponding to the variable value; When the read variable value is a numeric type, use the following third method to process: continue reading the next variable value, and write the next variable value to the serialized data structure. In the field description item or sub-field description item whose serial number is the same as the serial number of the variable value, when the read variable value is a string type, the following fourth method is used for processing: continue to read the next variable value, and obtain The length of the string, and then the byte of the length of the string is read, the string is obtained, and the string is written into the field description item or the subfield description item of the sequence number in the serialized data structure; When the sequence number of the extracted variable value is the same as the sequence number of the sub data structure description item, the value of the next variable is continuously read, the data length is obtained, and then the number is read. The length of the byte gets the bytecode set, and reads the variable value in the bytecode set. When the read variable value is a numeric type, the third way is used to process the read variable value. When the string type is used, the fourth method is used for processing; determining whether all variable values in the PB to be converted serialized message have been written in the field description item or the sub field description item, and if so, the field after the variable value is written The description item or subfield description item obtains the serialized data structure after the data is written. If not, according to the data type of the unwritten variable value, the unwritten variable value is written into the field by the third mode or the fourth mode. Description item or subfield description item.
转换单元 604, 用于按照序列化数据结构中记录的对应关系, 判断提取出的数据是否为结构名称 以及判断提取出的数据的节点类型;当提取出的数据包括结构名称时,以该数据和结构名称创建 XML 序列化消息中的根节点; 当提取出的数据为节点元素时, 采用下述第五方式进行处理: 在根节点下 以节点元素的字段描述项或子字段描述项中的名称创建 XML节点元素,并将字段描述项或子字段描 述项中的数据写入 XML节点元素中; 当提取出的数据为节点属性时, 采用下述第六方式进行处理: 在当前节点下以节点属性的字段描述项或子字段描述项中的名称创建 XML节点属性,并将字段描述 项或子字段描述项中的数据写入 XML节点属性中; 当提取出的数据为子数据结构描述项时, 以子数 据结构描述项中的名称创建子根节点, 提取子数据结构描述项中的数据, 当提取出的数据为节点元 素时, 在子根节点下采用第五方式进行处理, 当提取出的数据为节点属性时, 采用第六方式进行处 理; 判断提取出的数据是否都已经写入至相应的 XML节点元素或 XML节点属性中, 若是, 由数据 写入后的 XML节点元素或 XML节点属性得到具有第二结构的第二格式的序列化消息, 若否, 根据 未写入的数据的节点类型,采用上述第五方式或第六方式将未写入的数据写入相应的 XML节点元素 或 XML节点属性中。  The converting unit 604 is configured to determine, according to the correspondence relationship recorded in the serialized data structure, whether the extracted data is a structure name and determine a node type of the extracted data; when the extracted data includes a structure name, the data and The structure name creates the root node in the XML serialization message; when the extracted data is a node element, it is processed in the following fifth way: under the root node, the field description item or subfield description item name of the node element Create an XML node element and write the data in the field description item or sub-field description item into the XML node element; when the extracted data is a node attribute, the following sixth manner is used to process: Create an XML node attribute in the field description item or the name in the subfield description item, and write the data in the field description item or subfield description item into the XML node attribute; when the extracted data is a sub data structure description item , create a child root node with the name in the child data structure description item, extract the number in the child data structure description item When the extracted data is a node element, the fifth method is used for processing under the child root node, and when the extracted data is a node attribute, the sixth method is used for processing; determining whether the extracted data has been written To the corresponding XML node element or XML node attribute, if yes, the XML node element or the XML node attribute written by the data obtains the serialized message of the second format having the second structure, and if not, according to the unwritten data The node type is written into the corresponding XML node element or XML node attribute by using the fifth mode or the sixth mode described above.
数据提取单元 603用于提取数据写入后的序列化数据结构中的数据。  The data extracting unit 603 is for extracting data in the serialized data structure after the data is written.
本发明装置实施例中各单元的具体工作方式可以参见本发明的方法实施例。  For a specific working mode of each unit in the device embodiment of the present invention, reference may be made to the method embodiment of the present invention.
由上所述, 本发明实施例采用由设置的序列化描述项生成序列化数据结构, 将第一格式的待转 换序列化消息写入该序列化数据结构, 再从数据写入后的序列化数据结构中将数据提取出来转换为 第二格式的序列化消息的技术手段, 能通过一种序列化数据结构兼容多种格式的序列化消息, 使服 务器能够对不同格式的序列化消息进行方便、 快速的转换, 根据实际需求选取特定格式的序列化协 议来传输消息, 例如, 在服务器外部采用 XML格式传输消息, 以继承 XML格式在系统中应用的广 泛性, 在服务器内部采用 PB格式传输消息, 以继承 PB格式的高效性, 从而提高了服务器传递消息 的灵活性, 优化了消息传输性能。  As described above, the embodiment of the present invention generates a serialized data structure by using the set serialization description item, and writes the serialized message to be converted in the first format into the serialized data structure, and then serializes after the data is written. The technical means for extracting data into a serialized message of the second format in the data structure can enable the server to facilitate serialized messages of different formats by using a serialized data structure compatible with serialized messages of multiple formats. Fast conversion, according to actual needs, select a serialization protocol of a specific format to transmit messages, for example, use XML format to transmit messages outside the server, to inherit the extensive application of the XML format in the system, and transmit messages in the PB format inside the server. Inheriting the efficiency of the PB format, thereby improving the flexibility of the server to deliver messages and optimizing the message transmission performance.
为了便于清楚描述本发明实施例的技术方案, 在发明的实施例中, 采用了 "第一"、 "第二,,等字样 对功能和作用基本相同的相同项或相似项进行区分, 本领域技术人员可以理解"第一"、 "第二,,等字样 并不对数量和执行次序进行限定。  In order to facilitate the clear description of the technical solutions of the embodiments of the present invention, in the embodiments of the present invention, the words "first", "second," and the like are used to distinguish the same items or similar items whose functions and functions are substantially the same, in the field. The skilled person can understand that the words "first", "second", etc. do not limit the quantity and execution order.
以上所述仅为本发明的较佳实施例而已, 并非用于限定本发明的保护范围。 凡在本发明的精神 和原则之内所作的任何修改、 等同替换、 改进等, 均包含在本发明的保护范围内。  The above is only the preferred embodiment of the present invention and is not intended to limit the scope of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and scope of the invention are intended to be included within the scope of the invention.

Claims

权利 要求 书 claims
1、 一种序列化消息之间的转换方法, 包括: 1. A conversion method between serialized messages, including:
按照第一序列化消息的第一结构以及所述第一结构与第二序列化消息的第二结构之间的对应关 系, 利用设置的序列化描述项生成序列化数据结构; 其中, 第一序列化消息具有第一格式, 第二序 列化消息具有第二格式; 所述序列化描述项包括数据结构描述项和设置在数据结构描述项之中的字 段描述项, 所述各数据结构描述项的结构及数据结构描述项之中各字段描述项的结构与所述第一结 构相一致; According to the first structure of the first serialized message and the corresponding relationship between the first structure and the second structure of the second serialized message, a serialized data structure is generated using the set serialization description item; wherein, the first sequence The serialization message has a first format, and the second serialization message has a second format; the serialization description item includes a data structure description item and a field description item set in the data structure description item, and the The structure of each field description item among the structure and data structure description items is consistent with the first structure;
对符合所述第一结构的第一格式的待转换序列化消息, 根据所述序列化数据结构对所述待转换 序列化消息进行解析, 将解析出的数据写入所述序列化数据结构中, 得到数据写入后的序列化数据 结构; For a serialized message to be converted that conforms to the first format of the first structure, the serialized message to be converted is parsed according to the serialized data structure, and the parsed data is written into the serialized data structure. , get the serialized data structure after data is written;
提取所述数据写入后的序列化数据结构中的数据; Extract the data in the serialized data structure after the data is written;
按照所述序列化数据结构中记录的所述对应关系, 将提取出的数据转换为具有第二结构的第二 格式的序列化消息。 According to the corresponding relationship recorded in the serialized data structure, the extracted data is converted into a serialized message in a second format with a second structure.
2、 根据权利要求 1所述的方法, 其中, 所述按照第一序列化消息的第一结构以及所述第一结构 与第二序列化消息的第二结构之间的对应关系, 利用设置的序列化描述项生成序列化数据结构包括: 按照第一序列化消息的第一结构以及所述第一结构与第二序列化消息的第二结构之间的对应关 系, 利用所述序列化描述项的数据结构描述项中的信息和字段描述项中的信息将所述对应关系设置 在生成的所述序列化数据结构中。 2. The method according to claim 1, wherein, according to the first structure of the first serialized message and the corresponding relationship between the first structure and the second structure of the second serialized message, using the set Generating the serialized data structure from the serialized description item includes: according to the first structure of the first serialized message and the corresponding relationship between the first structure and the second structure of the second serialized message, using the serialized description item The information in the data structure description item and the information in the field description item set the corresponding relationship in the generated serialized data structure.
3、 根据权利要求 2所述的方法, 其中, 所述第一格式为 XML格式, 所述第二格式为 PB格式, 或者, 所述第一格式为 PB格式, 所述第二格式为 XML格式, 3. The method according to claim 2, wherein the first format is XML format, the second format is PB format, or the first format is PB format, and the second format is XML format. ,
所述利用所述序列化描述项的数据结构描述项中的信息和字段描述项中的信息将所述对应关系 设置在生成的所述序列化数据结构中包括: The use of the information in the data structure description item and the information in the field description item of the serialization description item to set the corresponding relationship in the generated serialization data structure includes:
在数据结构描述项中设置结构名称和字段描述项,所述结构名称为具有第一结构的 XML序列化 消息中根节点的名称, 所述字段描述项中包括数据类型、 序号、 节点类型和名称, 所述序号为所述 字段描述项中的数据在具有第二结构的 PB序列化消息中的序号; A structure name and a field description item are set in the data structure description item. The structure name is the name of the root node in the XML serialized message with the first structure. The field description item includes data type, sequence number, node type and name, The sequence number is the sequence number of the data in the field description item in the PB serialization message with the second structure;
通过所述数据结构描述项中的结构名称、 所述字段描述项中的序号和名称将所述对应关系设置 在生成的所述序列化数据结构中。 The corresponding relationship is set in the generated serialized data structure through the structure name in the data structure description item and the sequence number and name in the field description item.
4、 根据权利要求 2所述的方法, 其中, 所述数据结构描述项中设置子数据结构描述项, 所述子 数据结构描述项中设置有子字段描述项, 所述对应关系包括位于同一数据结构描述项中的子数据结 构描述项与字段描述项的关系; 4. The method according to claim 2, wherein a sub-data structure description item is set in the data structure description item, a sub-field description item is set in the sub-data structure description item, and the corresponding relationship includes a sub-data structure description item located in the same data structure. The relationship between the sub-data structure description items and field description items in the structure description item;
当所述第一格式为 XML格式、 所述第二格式为 PB格式时, 或者, 当所述第一格式为 PB格式、 所述第二格式为 XML格式, When the first format is XML format and the second format is PB format, or when the first format is PB format and the second format is XML format,
所述利用所述序列化描述项的数据结构描述项中的信息和字段描述项中的信息将所述对应关系 设置在生成的所述序列化数据结构中包括: The use of the information in the data structure description item and the information in the field description item of the serialization description item to set the corresponding relationship in the generated serialization data structure includes:
在所述数据结构描述项中设置结构名称、 字段描述项和子数据结构描述项, 所述结构名称为具 有第一结构的 XML序列化消息中根节点的名称, 所述子数据结构描述项中包括序号、 子结构名称和 子字段描述项, 所述字段描述项或所述子字段描述项中包括数据类型、 序号、 节点类型和名称, 所 述序号为所述字段描述项或子字段描述项中的数据在具有第二结构的 P B序列化消息中的序号; 通过所述数据结构描述项中的结构名称、 所述子数据结构描述项中的子结构名称、 所述字段描 述项和所述子字段描述项中的序号和名称将所述对应关系设置在生成的所述序列化数据结构中。 A structure name, a field description item and a sub-data structure description item are set in the data structure description item. The structure name is the name of the root node in the XML serialization message with the first structure. The sub-data structure description item includes a sequence number. , substructure name and subfield description item, the field description item or the subfield description item includes data type, serial number, node type and name, the serial number is the data in the field description item or subfield description item The sequence number in the PB serialized message with the second structure; through the structure name in the data structure description item, the substructure name in the sub-data structure description item, the field description item and the sub-field description The serial number and name in the item set the corresponding relationship in the generated serialized data structure.
5、根据权利要求 4所述的方法,其中, 所述第一格式为 XML格式, 所述第二格式为 PB格式时, 所述对符合所述第一结构的第一格式的待转换序列化消息, 根据所述序列化数据结构对所述待转换 序列化消息进行解析, 将解析出的数据写入所述序列化数据结构中, 得到数据写入后的序列化数据 结构包括: 5. The method according to claim 4, wherein: when the first format is XML format, and when the second format is PB format, the pair of serialization to be converted in the first format that conforms to the first structure message, parse the serialized message to be converted according to the serialized data structure, write the parsed data into the serialized data structure, and obtain the serialized data structure after the data is written, including:
根据所述序列化数据结构对待转换的 XML 序列化消息中的 XML 元素进行读取, 当读取出的 XML元素中的名称与数据结构描述项中的结构名称相同时,确定所述 XML元素对应的 XML节点为 根节点; 读取从属或间接从属于根节点的各 XML元素, 当所述 XML元素中的名称与字段描述项中的名 称相同且所述 XML元素的类型与字段描述项中的节点类型相同时, 将解析出的所述 XML元素的数 据写入在所述字段描述项中; 当所述 XML 元素中的名称与子数据结构描述项中的子结构名称相同 时, 将所述 XML元素作为子根节点, 读取从属或间接从属于子根节点的各 XML元素, 当从属或间 接从属于子根节点的 XML元素的名称与子字段描述项中的名称相同且所述 XML元素的类型与子字 段描述项中的节点类型相同时, 将解析出的所述 XML元素的数据写入在所述子字段描述项中; 判断读取出的所述待转换的 XML序列化消息中的 XML元素的数据是否都已经写入至相应的所 述字段描述项或子字段描述项, 若是, 由写入 XML元素的数据后的所述字段描述项或子字段描述项 得到数据写入后的序列化数据结构,若否,将未写入的 XML元素写入字段描述项或子字段描述项中, 所述字段描述项或子字段描述项的名称与节点类型与所述未写入的 XML元素的名称和类型相同。 The XML element in the XML serialized message to be converted is read according to the serialized data structure. When the name in the read XML element is the same as the structure name in the data structure description item, it is determined that the XML element corresponds to The XML node is the root node; Read each XML element that is subordinate or indirectly subordinate to the root node. When the name in the XML element is the same as the name in the field description item and the type of the XML element is the same as the node type in the field description item, it will be parsed The data of the XML element is written in the field description item; when the name in the XML element is the same as the substructure name in the subdata structure description item, the XML element is used as a child root node, Read each XML element that is subordinate or indirectly subordinate to the sub-root node, when the name of the XML element that is subordinate or indirectly subordinate to the sub-root node is the same as the name in the sub-field description item and the type of the XML element is the same as the sub-field description item. When the node types in are the same, write the parsed XML element data into the subfield description item; determine whether the read XML element data in the XML serialization message to be converted are all has been written to the corresponding field description item or subfield description item. If so, the serialized data structure after the data is written is obtained from the field description item or subfield description item after the data of the XML element is written. If No, write the unwritten XML element into the field description item or subfield description item, and the name and node type of the field description item or subfield description item are the same as the name and type of the unwritten XML element. .
6、 根据权利要求 5所述的方法, 其中, 所述按照所述序列化数据结构中记录的所述对应关系, 将提取出的数据转换为具有第二结构的第二格式的序列化消息包括: 6. The method according to claim 5, wherein, according to the corresponding relationship recorded in the serialized data structure, converting the extracted data into a serialized message in a second format with a second structure includes :
按照所述序列化数据结构中记录的所述对应关系, 判断提取出的数据的数据类型; Determine the data type of the extracted data according to the corresponding relationship recorded in the serialized data structure;
当提取出的数据为数值类型的数据时, 采用下述第一方式进行处理: 以变量方式先将所述字段 描述项或子字段描述项中记录的所述数据的序号与数据类型写入 PB 序列化消息中相同序号对应的 数据项中, 再将所述数值类型的数据写入所述数据项中; When the extracted data is numerical type data, the following first method is used for processing: first write the sequence number and data type of the data recorded in the field description item or subfield description item into PB in variable mode into the data item corresponding to the same sequence number in the serialized message, and then write the numerical type data into the data item;
当提取出的数据为字符串类型的数据时, 采用下述第二方式进行处理: 以变量方式先将所述字 段描述项或子字段描述项中记录的所述数据的序号与数据类型写入 PB 序列化消息中相同序号对应 的数据项中, 再将所述字段描述项或子字段描述项中记录的所述数据的字符串长度写入所述数据项 中, 然后将所述字符串类型的数据写入所述数据项中; When the extracted data is string type data, the following second method is used for processing: first write the sequence number and data type of the data recorded in the field description item or subfield description item in a variable way In the data item corresponding to the same sequence number in the PB serialized message, the string length of the data recorded in the field description item or subfield description item is written into the data item, and then the string type is The data is written into the data item;
当提取出的数据为子数据结构描述项时, 以变量方式先将所述子数据结构描述项中的序号和结 构类型写入 PB序列化消息中相同序号对应的数据项中,再将所述子数据结构描述项中数据长度写入 所述数据项中, 然后提取所述子数据结构描述项的子字段描述项中的数据, 当提取出的内容为数值 类型的数据时, 采用所述第一方式进行处理, 当提取出的内容为字符串类型的数据时, 采用所述第 二方式进行处理; When the extracted data is a sub-data structure description item, first write the sequence number and structure type in the sub-data structure description item into the data item corresponding to the same sequence number in the PB serialization message in a variable manner, and then write the The data length in the sub-data structure description item is written into the data item, and then the data in the sub-field description item of the sub-data structure description item is extracted. When the extracted content is numerical type data, the first The first method is used for processing. When the extracted content is string type data, the second method is used for processing;
判断提取出的数据是否都已经写入 PB序列化消息中对应的数据项中, 若是, 由写入数据后的各 数据项得到具有第二结构的第二格式的序列化消息, 若否, 根据未写入的数据的数据类型, 采用所 述第一方式或第二方式, 将所述未写入的数据写入对应的数据项中。 Determine whether the extracted data has been written into the corresponding data items in the PB serialization message. If so, obtain the serialized message in the second format with the second structure from each data item after the data is written. If not, according to For the data type of the unwritten data, the first method or the second method is used to write the unwritten data into the corresponding data item.
7、根据权利要求 4所述的方法,其中, 所述第一格式为 PB格式, 所述第二格式为 XML格式时, 所述根据所述序列化数据结构对所述待转换序列化消息进行解析, 将解析出的数据写入所述序列化 数据结构中, 得到数据写入后的序列化数据结构包括: 7. The method according to claim 4, wherein: when the first format is PB format, and when the second format is XML format, the serialized message to be converted is processed according to the serialized data structure. Parse and write the parsed data into the serialized data structure. The serialized data structure obtained after the data is written includes:
逐个读取 PB待转换序列化消息的二进制数据流中的变量值; Read the variable values in the binary data stream of the PB serialized message to be converted one by one;
从读取到的变量值中获取所述变量值对应的数据类型和序号; Obtain the data type and serial number corresponding to the variable value from the read variable value;
当读取出的变量值为数值类型时, 采用下述第三方式进行处理: 继续读取下一个变量值, 将所 述下一个变量值写入序列化数据结构中序号与所述变量值的序号相同的字段描述项或子字段描述项 中, When the read variable value is a numeric type, the following third method is used for processing: continue to read the next variable value, and write the next variable value into the serialized data structure between the serial number and the variable value. Among field description items or subfield description items with the same serial number,
当读取出的变量值为字符串类型时, 采用下述第四方式进行处理: 继续读取下一个变量值, 得 到字符串长度, 然后再读取字符串长度大小的字节, 得到字符串, 将所述字符串写入序列化数据结 构中序号与所述字符串的序号相同的字段描述项或子字段描述项中; When the read variable value is a string type, the following fourth method is used for processing: continue to read the next variable value to obtain the string length, and then read bytes equal to the string length to obtain the string , write the string into the field description item or subfield description item whose serial number is the same as the serial number of the string in the serialized data structure;
当读取出的变量值的序号与子数据结构描述项的序号相同时, 继续读取下一个变量值, 得到数 据长度, 然后再读取数据长度大小的字节得到字节码集合, 以及, 读取所述字节码集合中的变量值, 当读取出的变量值为数值类型时, 采用所述第三方式进行处理, 当读取出的变量值为字符串类型时, 采用所述第四方式进行处理; When the sequence number of the read variable value is the same as the sequence number of the sub-data structure description item, continue to read the next variable value to obtain the data length, and then read bytes of the data length size to obtain the bytecode set, and, Read the variable values in the bytecode set. When the read variable value is of numeric type, the third method is used for processing. When the read variable value is of string type, the above method is used. The fourth method is processed;
判断所述 PB待转换序列化消息中的所有变量值是否都已经写入字段描述项或子字段描述项中, 若是, 由写入变量值后的所述字段描述项或子字段描述项得到数据写入后的序列化数据结构, 若否, 根据未写入的变量值的数据类型, 采用所述第三方式或第四方式将所述未写入的变量值写入字段描 述项或子字段描述项中。 Determine whether all variable values in the PB serialized message to be converted have been written into field description items or subfield description items. If so, obtain data from the field description items or subfield description items after writing variable values. The serialized data structure after writing, if not, uses the third or fourth method to write the unwritten variable value into the field description item or subfield according to the data type of the unwritten variable value. in the description item.
8、 根据权利要求 7所述的方法, 其中, 所述按照所述序列化数据结构中记录的所述对应关系, 将提取出的数据转换为具有第二结构的第二格式的序列化消息包括: 按照所述序列化数据结构中记录的所述对应关系, 判断提取出的数据是否为结构名称以及判断 提取出的数据的节点类型; 8. The method according to claim 7, wherein, according to the corresponding relationship recorded in the serialized data structure, converting the extracted data into a serialized message in a second format with a second structure includes : According to the corresponding relationship recorded in the serialized data structure, determine whether the extracted data is a structure name and determine the node type of the extracted data;
当提取出的数据包括结构名称时, 以所述数据和结构名称创建 XML序列化消息中的根节点; 当提取出的数据为节点元素时, 采用下述第五方式进行处理: 在所述根节点下以所述节点元素 的字段描述项或子字段描述项中的名称创建 XML节点元素,并将字段描述项或子字段描述项中的数 据写入所述 XML节点元素中; When the extracted data includes a structure name, create the root node in the XML serialized message using the data and the structure name; when the extracted data is a node element, use the following fifth method for processing: At the root Create an XML node element under the node with the name in the field description item or subfield description item of the node element, and write the data in the field description item or subfield description item into the XML node element;
当提取出的数据为节点属性时, 采用下述第六方式进行处理: 在当前节点下以所述节点属性的 字段描述项或子字段描述项中的名称创建 XML节点属性,并将字段描述项或子字段描述项中的数据 写入所述 XML节点属性中; When the extracted data is a node attribute, the following sixth method is used for processing: Create an XML node attribute under the current node with the name in the field description item or subfield description item of the node attribute, and change the field description item Or the data in the subfield description item is written into the XML node attribute;
当提取出的数据为子数据结构描述项时, 以所述子数据结构描述项中的名称创建子根节点, 提 取子数据结构描述项中的数据, 当提取出的数据为节点元素时, 在子根节点下采用所述第五方式进 行处理, 当提取出的数据为节点属性时, 采用所述第六方式进行处理; When the extracted data is a sub-data structure description item, a sub-root node is created with the name in the sub-data structure description item, and the data in the sub-data structure description item is extracted. When the extracted data is a node element, in The fifth method is used for processing under the child root node. When the extracted data is a node attribute, the sixth method is used for processing;
判断提取出的数据是否都已经写入至相应的 XML节点元素或 XML节点属性中, 若是, 由数据 写入后的 XML节点元素或 XML节点属性得到具有第二结构的第二格式的序列化消息, 若否, 根据 未写入的数据的节点类型,采用上述第五方式或第六方式将所述未写入的数据写入相应的 XML节点 元素或 XML节点属性中。 Determine whether the extracted data has been written into the corresponding XML node element or XML node attribute. If so, obtain a serialized message in the second format with the second structure from the XML node element or XML node attribute after the data is written. , if not, according to the node type of the unwritten data, use the above fifth method or the sixth method to write the unwritten data into the corresponding XML node element or XML node attribute.
9、 一种序列化消息之间的转换装置, 包括: 9. A conversion device between serialized messages, including:
数据结构生成单元, 用于按照第一序列化消息的第一结构以及所述第一结构与第二序列化消息 的第二结构之间的对应关系, 利用设置的序列化描述项生成序列化数据结构; 其中, 第一序列化消 息具有第一格式, 第二序列化消息具有第二格式; 所述序列化描述项包括数据结构描述项和设置在 数据结构描述项之中的字段描述项, 所述各数据结构描述项的结构及数据结构描述项之中各字段描 述项的结构与所述第一结构相一致; A data structure generation unit configured to generate serialized data using the set serialization description item according to the first structure of the first serialized message and the corresponding relationship between the first structure and the second structure of the second serialized message. structure; wherein, the first serialized message has a first format, and the second serialized message has a second format; the serialized description item includes a data structure description item and a field description item set in the data structure description item, so The structure of each data structure description item and the structure of each field description item in the data structure description item are consistent with the first structure;
解析写入单元, 用于对符合所述第一结构的第一格式的待转换序列化消息, 根据所述序列化数 据结构对所述待转换序列化消息进行解析, 将解析出的数据写入所述序列化数据结构中, 得到数据 写入后的序列化数据结构; A parsing and writing unit, configured to parse the serialized message to be converted in the first format that conforms to the first structure, parse the serialized message to be converted according to the serialized data structure, and write the parsed data into In the serialized data structure, the serialized data structure after the data is written is obtained;
数据提取单元, 用于提取所述数据写入后的序列化数据结构中的数据; A data extraction unit, used to extract the data in the serialized data structure after the data is written;
转换单元, 用于按照所述序列化数据结构中记录的所述对应关系, 将提取出的数据转换为具有 第二结构的第二格式的序列化消息。 A conversion unit configured to convert the extracted data into a serialized message in a second format with a second structure according to the corresponding relationship recorded in the serialized data structure.
10、 根据权利要求 9 所述的装置, 其中, 所述数据结构生成单元包括对应关系设置单元, 用于 按照第一序列化消息的第一结构以及所述第一结构与第二序列化消息的第二结构之间的对应关系, 利用所述序列化描述项的数据结构描述项中的信息和字段描述项中的信息将所述对应关系设置在生 成的所述序列化数据结构中。 10. The device according to claim 9, wherein the data structure generating unit includes a correspondence relationship setting unit configured to determine the relationship between the first structure and the second serialized message according to the first structure of the first serialized message. The corresponding relationship between the second structures is set in the generated serialized data structure by using the information in the data structure description item and the information in the field description item of the serialization description item.
1 1、根据权利要求 10所述的装置,其中, 所述第一格式为 XML格式, 所述第二格式为 PB格式, 或者, 所述第一格式为 PB格式, 所述第二格式为 XML格式, 1 1. The device according to claim 10, wherein: the first format is XML format, the second format is PB format, or, the first format is PB format, and the second format is XML Format,
所述对应关系设置单元, 用于在所述数据结构描述项中设置结构名称和字段描述项, 所述结构 名称为具有第一结构的 XML序列化消息中根节点的名称, 所述字段描述项中包括数据类型、 序号、 节点类型和名称, 所述序号为所述字段描述项中的数据在具有第二结构的 PB序列化消息中的序号; 通过所述数据结构描述项中的结构名称、 所述字段描述项中的序号和名称将所述对应关系设置在生 成的所述序列化数据结构中。 The correspondence relationship setting unit is used to set a structure name and a field description item in the data structure description item. The structure name is the name of the root node in the XML serialization message with the first structure. In the field description item Including data type, sequence number, node type and name, the sequence number is the sequence number of the data in the field description item in the PB serialization message with the second structure; through the structure name in the data structure description item, the The serial number and name in the field description item set the corresponding relationship in the generated serialized data structure.
12、 根据权利要求 10所述的装置, 其中, 所述数据结构生成单元包括子结构设置单元, 用于在 所述数据结构描述项中设置子数据结构描述项, 其中, 所述子数据结构描述项中设置有子字段描述 项, 所述对应关系包括位于同一数据结构描述项中的子数据结构描述项与字段描述项的关系; 12. The device according to claim 10, wherein the data structure generating unit includes a substructure setting unit for setting a subdata structure description item in the data structure description item, wherein the subdata structure description The item is provided with a subfield description item, and the corresponding relationship includes a relationship between a subdata structure description item and a field description item located in the same data structure description item;
所述对应关系设置单元, 用于在所述数据结构描述项中设置结构名称、 字段描述项和子数据结 构描述项, 所述结构名称为具有第一结构的 XML序列化消息中根节点的名称, 所述子数据结构描述 项中包括序号、 子结构名称和子字段描述项, 所述字段描述项或所述子字段描述项中包括数据类型、 序号、 节点类型和名称, 所述序号为所述字段描述项或子字段描述项中的数据在具有第二结构的 PB 序列化消息中的序号; 通过所述数据结构描述项中的结构名称、 所述子数据结构描述项中的子结构 化数据结构中。 The correspondence relationship setting unit is used to set a structure name, a field description item and a sub-data structure description item in the data structure description item, where the structure name is the name of the root node in the XML serialization message with the first structure, so The sub-data structure description item includes a serial number, a sub-structure name and a sub-field description item, the field description item or the sub-field description item includes a data type, a serial number, a node type and a name, and the serial number is the field description The item or subfield describes the sequence number of the data in the item in the PB serialized message with the second structure; the structure name in the data structure description item, the substructure in the subdata structure description item ized data structure.
13、 根据权利要求 12所述的装置, 其中, 所述第一格式为 XML格式, 所述第二格式为 PB格式 时, 13. The device according to claim 12, wherein the first format is XML format, and when the second format is PB format,
所述解析写入单元, 用于根据所述序列化数据结构对待转换的 XML序列化消息中的 XML元素 进行读取, 当读取出的 XML元素中的名称与数据结构描述项中的结构名称相同时, 确定所述 XML 元素对应的 XML节点为根节点; The parsing and writing unit is used to read the XML elements in the XML serialized message to be converted according to the serialized data structure. When the name in the read XML element is the same as the structure name in the data structure description item, When the same, determine that the XML node corresponding to the XML element is the root node;
读取从属或间接从属于根节点的各 XML元素, 当所述 XML元素中的名称与字段描述项中的名 称相同且所述 XML元素的类型与字段描述项中的节点类型相同时, 将解析出的所述 XML元素的数 据写入在所述字段描述项中; 当所述 XML 元素中的名称与子数据结构描述项中的子结构名称相同 时, 将所述 XML元素作为子根节点, 读取从属或间接从属于子根节点的各 XML元素, 当从属或间 接从属于子根节点的 XML元素的名称与子字段描述项中的名称相同且所述 XML元素的类型与子字 段描述项中的节点类型相同时, 将解析出的所述 XML元素的数据写入在所述子字段描述项中; 判断读取出的所述待转换的 XML序列化消息中的 XML元素的数据是否都已经写入至相应的所 述字段描述项或子字段描述项, 若是, 由写入 XML元素的数据后的所述字段描述项或子字段描述项 得到数据写入后的序列化数据结构,若否,将未写入的 XML元素写入字段描述项或子字段描述项中, 所述字段描述项或子字段描述项的名称与节点类型与所述未写入的 XML元素的名称和类型相同。 Read each XML element that is subordinate or indirectly subordinate to the root node. When the name in the XML element is the same as the name in the field description item and the type of the XML element is the same as the node type in the field description item, it will be parsed The data of the XML element is written in the field description item; when the name in the XML element is the same as the substructure name in the subdata structure description item, the XML element is used as a child root node, Read each XML element that is subordinate or indirectly subordinate to the sub-root node, when the name of the XML element that is subordinate or indirectly subordinate to the sub-root node is the same as the name in the sub-field description item and the type of the XML element is the same as the sub-field description item. When the node types in are the same, write the parsed XML element data into the subfield description item; determine whether the read XML element data in the XML serialization message to be converted are all has been written to the corresponding field description item or subfield description item. If so, the serialized data structure after the data is written is obtained from the field description item or subfield description item after the data of the XML element is written. If No, write the unwritten XML element into the field description item or subfield description item, and the name and node type of the field description item or subfield description item are the same as the name and type of the unwritten XML element. .
14、 根据权利要求 13所述的装置, 其中, 所述转换单元, 用于按照所述序列化数据结构中记录 的所述对应关系, 判断提取出的数据的数据类型; 当提取出的数据为数值类型的数据时, 采用下述 第一方式进行处理: 以变量方式先将所述字段描述项或子字段描述项中记录的所述数据的序号与数 据类型写入 PB 序列化消息中相同序号对应的数据项中, 再将所述数值类型的数据写入所述数据项 中; 14. The device according to claim 13, wherein the conversion unit is configured to determine the data type of the extracted data according to the corresponding relationship recorded in the serialized data structure; when the extracted data is When numeric type data is used, the following first method is used for processing: First, the sequence number and data type of the data recorded in the field description item or subfield description item are written into the same sequence number in the PB serialized message in variable mode. In the corresponding data item, the numerical type data is then written into the data item;
当提取出的数据为字符串类型的数据时, 采用下述第二方式进行处理: 以变量方式先将所述字 段描述项或子字段描述项中记录的所述数据的序号与数据类型写入 PB 序列化消息中相同序号对应 的数据项中, 再将所述字段描述项或子字段描述项中记录的所述数据的字符串长度写入所述数据项 中, 然后将所述字符串类型的数据写入所述数据项中; When the extracted data is string type data, the following second method is used for processing: first write the sequence number and data type of the data recorded in the field description item or subfield description item in a variable way In the data item corresponding to the same sequence number in the PB serialized message, the string length of the data recorded in the field description item or subfield description item is written into the data item, and then the string type is The data is written into the data item;
当提取出的数据为子数据结构描述项时, 以变量方式先将所述子数据结构描述项中的序号和结 构类型写入 PB序列化消息中相同序号对应的数据项中,再将所述子数据结构描述项中数据长度写入 所述数据项中, 然后提取所述子数据结构描述项的子字段描述项中的数据, 当提取出的数据为数值 类型的数据时, 采用所述第一方式进行处理, 当提取出的数据为字符串类型的数据时, 采用所述第 二方式进行处理; 判断提取出的数据是否都已经写入 PB序列化消息中对应的数据项中, 若是, 由写 入数据后的各数据项得到具有第二结构的第二格式的序列化消息, 若否, 根据未写入的数据的数据 类型, 采用所述第一方式或第二方式, 将所述未写入的数据写入对应的数据项中。 When the extracted data is a sub-data structure description item, first write the sequence number and structure type in the sub-data structure description item into the data item corresponding to the same sequence number in the PB serialization message in a variable manner, and then write the The data length in the sub-data structure description item is written into the data item, and then the data in the sub-field description item of the sub-data structure description item is extracted. When the extracted data is numerical type data, the first Process in one way, when the extracted data is string type data, use the second way to process; Determine whether the extracted data has been written into the corresponding data items in the PB serialization message, if so, A serialized message in the second format with the second structure is obtained from each data item after the data is written. If not, according to the data type of the unwritten data, the first method or the second method is used to obtain the serialized message in the second format. Unwritten data is written to the corresponding data item.
15、 根据权利要求 12所述的装置, 其中, 所述第一格式为 PB格式, 所述第二格式为 XML格式 时, 所述解析写入单元, 用于逐个读取 PB待转换序列化消息的二进制数据流中的变量值; 15. The device according to claim 12, wherein the first format is PB format, and when the second format is XML format, the parsing and writing unit is used to read PB serialized messages to be converted one by one. variable values in the binary data stream;
从读取到的变量值中获取所述变量值对应的数据类型和序号; Obtain the data type and serial number corresponding to the variable value from the read variable value;
当读取出的变量值为数值类型时, 采用下述第三方式进行处理: 继续读取下一个变量值, 将所 述下一个变量值写入序列化数据结构中序号与所述变量值的序号相同的字段描述项或子字段描述项 中, When the read variable value is a numeric type, the following third method is used for processing: continue to read the next variable value, and write the next variable value into the serialized data structure between the serial number and the variable value. Among field description items or subfield description items with the same serial number,
当读取出的变量值为字符串类型时, 采用下述第四方式进行处理: 继续读取下一个变量值, 得 到字符串长度, 然后再读取字符串长度大小的字节, 得到字符串, 将所述字符串写入序列化数据结 构中序号与所述字符串的序号相同的字段描述项或子字段描述项中; When the read variable value is a string type, the following fourth method is used for processing: continue to read the next variable value to obtain the string length, and then read bytes equal to the string length to obtain the string , write the string into the field description item or subfield description item whose serial number is the same as the serial number of the string in the serialized data structure;
当读取出的变量值的序号与子数据结构描述项的序号相同时, 继续读取下一个变量值, 得到数 据长度, 然后再读取数据长度大小的字节得到字节码集合, 以及, 读取所述字节码集合中的变量值, 当读取出的变量值为数值类型时, 采用所述第三方式进行处理, 当读取出的变量值为字符串类型时, 采用所述第四方式进行处理; When the sequence number of the read variable value is the same as the sequence number of the sub-data structure description item, continue to read the next variable value to obtain the data length, and then read bytes of the data length size to obtain the bytecode set, and, Read the variable values in the bytecode set. When the read variable value is of numeric type, the third method is used for processing. When the read variable value is of string type, the above method is used. The fourth method is processed;
判断所述 PB待转换序列化消息中的所有变量值是否都已经写入字段描述项或子字段描述项中, 若是, 由写入变量值后的所述字段描述项或子字段描述项得到数据写入后的序列化数据结构, 若否, 根据未写入的变量值的数据类型, 采用所述第三方式或第四方式将所述未写入的变量值写入字段描 述项或子字段描述项中。 Determine whether all variable values in the PB serialized message to be converted have been written into field description items or subfield description items. If so, obtain data from the field description items or subfield description items after writing variable values. The serialized data structure after writing, if not, uses the third or fourth method to write the unwritten variable value into the field description item or subfield according to the data type of the unwritten variable value. in the description item.
16、 根据权利要求 15所述的装置, 其中, 所述转换单元, 用于按照所述序列化数据结构中记录 的所述对应关系, 判断提取出的数据是否为结构名称以及判断提取出的数据的节点类型; 当提取出 的数据包括结构名称时, 以所述数据和结构名称创建 XML序列化消息中的根节点; 16. The device according to claim 15, wherein the conversion unit is configured to determine whether the extracted data is a structure name and determine whether the extracted data is a structure name according to the corresponding relationship recorded in the serialized data structure. The node type; when the extracted data includes a structure name, create the root node in the XML serialization message with the data and structure name;
当提取出的数据为节点元素时, 采用下述第五方式进行处理: 在所述根节点下以所述节点元素 的字段描述项或子字段描述项中的名称创建 XML节点元素,并将字段描述项或子字段描述项中的数 据写入所述 XML节点元素中; When the extracted data is a node element, the following fifth method is used for processing: Create an XML node element under the root node with the name in the field description item or subfield description item of the node element, and add the field The data in the description item or subfield description item is written into the XML node element;
当提取出的数据为节点属性时, 采用下述第六方式进行处理: 在当前节点下以所述节点属性的 字段描述项或子字段描述项中的名称创建 XML节点属性,并将字段描述项或子字段描述项中的数据 写入所述 XML节点属性中; When the extracted data is a node attribute, the following sixth method is used for processing: Create an XML node attribute under the current node with the name in the field description item or subfield description item of the node attribute, and change the field description item Or the data in the subfield description item is written into the XML node attribute;
当提取出的数据为子数据结构描述项时, 以所述子数据结构描述项中的名称创建子根节点, 提 取子数据结构描述项中的数据, 当提取出的数据为节点元素时, 在子根节点下采用所述第五方式进 行处理, 当提取出的数据为节点属性时, 采用所述第六方式进行处理; When the extracted data is a sub-data structure description item, a sub-root node is created with the name in the sub-data structure description item, and the data in the sub-data structure description item is extracted. When the extracted data is a node element, in The fifth method is used for processing under the child root node. When the extracted data is a node attribute, the sixth method is used for processing;
判断提取出的数据是否都已经写入至相应的 XML节点元素或 XML节点属性中, 若是, 由数据 写入后的 XML节点元素或 XML节点属性得到具有第二结构的第二格式的序列化消息, 若否, 根据 未写入的数据的节点类型,采用上述第五方式或第六方式将所述未写入的数据写入相应的 XML节点 元素或 XML节点属性中。 Determine whether the extracted data has been written into the corresponding XML node element or XML node attribute. If so, obtain a serialized message in the second format with the second structure from the XML node element or XML node attribute after the data is written. , if not, according to the node type of the unwritten data, use the above fifth method or the sixth method to write the unwritten data into the corresponding XML node element or XML node attribute.
PCT/CN2013/091193 2013-12-31 2013-12-31 Method and apparatus for performing conversion among serialized messages WO2015100652A1 (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN201380081227.7A CN105793842B (en) 2013-12-31 2013-12-31 Conversion method and device between serialized message
PCT/CN2013/091193 WO2015100652A1 (en) 2013-12-31 2013-12-31 Method and apparatus for performing conversion among serialized messages

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2013/091193 WO2015100652A1 (en) 2013-12-31 2013-12-31 Method and apparatus for performing conversion among serialized messages

Publications (1)

Publication Number Publication Date
WO2015100652A1 true WO2015100652A1 (en) 2015-07-09

Family

ID=53492994

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2013/091193 WO2015100652A1 (en) 2013-12-31 2013-12-31 Method and apparatus for performing conversion among serialized messages

Country Status (2)

Country Link
CN (1) CN105793842B (en)
WO (1) WO2015100652A1 (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108628885B (en) * 2017-03-20 2023-04-18 腾讯科技(深圳)有限公司 Data synchronization method and device and storage equipment
CN111931200B (en) * 2020-07-13 2024-02-23 车智互联(北京)科技有限公司 Data serialization method, mobile terminal and readable storage medium
CN115623087B (en) * 2022-11-21 2023-02-28 飞天诚信科技股份有限公司 Method and system for analyzing PB format circulation data

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101931548A (en) * 2009-06-24 2010-12-29 华为技术有限公司 Method, apparatus and system for label management of access network
CN102185901A (en) * 2011-04-19 2011-09-14 北京神州数码思特奇信息技术股份有限公司 Client message conversion method
CN102810069A (en) * 2012-07-16 2012-12-05 深圳市融创天下科技股份有限公司 JAVA object requesting and responding methods, devices and systems and terminal

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
GB2432930A (en) * 2005-12-01 2007-06-06 Honeywell Int Inc Translating format of configuration data when updating software
CN102420862B (en) * 2011-11-29 2015-12-02 上海计算机软件技术开发中心 A kind of Internet of things middleware message form conversion method
US20130304510A1 (en) * 2012-05-08 2013-11-14 Drfirst.Com, Inc. Health information exchange system and method

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101931548A (en) * 2009-06-24 2010-12-29 华为技术有限公司 Method, apparatus and system for label management of access network
CN102185901A (en) * 2011-04-19 2011-09-14 北京神州数码思特奇信息技术股份有限公司 Client message conversion method
CN102810069A (en) * 2012-07-16 2012-12-05 深圳市融创天下科技股份有限公司 JAVA object requesting and responding methods, devices and systems and terminal

Also Published As

Publication number Publication date
CN105793842B (en) 2019-10-18
CN105793842A (en) 2016-07-20

Similar Documents

Publication Publication Date Title
US8060652B2 (en) Extensible binary mark-up language for efficient XML-based data communications and related systems and methods
US8583704B2 (en) Systems and methods for efficient data transfer
CN102185901B (en) Client message conversion method
TWI377819B (en) System and method for history driven optimization of web services communication
JP5242887B2 (en) Flexible transfer of typed application data
EP1684191A2 (en) Method and system for binary serialization of documents
WO2016078608A1 (en) Road coding method and device
US11675744B2 (en) Performing a code conversion in a smaller target encoding space
WO2015100652A1 (en) Method and apparatus for performing conversion among serialized messages
US20080313291A1 (en) Method and apparatus for encoding data
CN110020412B (en) Method and device for generating IMIX standard message and electronic equipment
US20070239393A1 (en) Knowledge based encoding of data
CN109144514A (en) JSON formatted data parses storage method and device
US9536016B2 (en) On-disk multimap
US20210168224A1 (en) Systems and methods for binary message transformation using custom descriptors
JP2006216024A (en) Efficient conversion of interchange format message
US8670973B2 (en) Language translation reuse in different systems
CN101047663A (en) Method and system for reducing effective load
CN104281632B (en) Coding-decoding method and system based on Object Protocol mapping
US20050091405A1 (en) Method and system for using multiple data type representations to deliver data objects to components in a distributed system
Pfisterer et al. Energy-optimized data serialization for heterogeneous WSNs using middleware synthesis
JP4830558B2 (en) Structured document encoding apparatus and structured document decoding apparatus
CN101151612A (en) Method of and system for providing random access to a document
Priyanga et al. A fixed length payload encoding for can
CN115989488A (en) Storing serialized structured data generally in a standardized serialized data structure

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 13900803

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 13900803

Country of ref document: EP

Kind code of ref document: A1