Processing method for unified analysis of mobile terminal communication protocol
By using JSON rules in mobile application development to convert data from parameter dictionary to binary arrays and decode them into structured objects, the complexity and maintenance problems of traditional byte alignment methods are solved, and efficient and cross-platform compatible data transmission is achieved and parsing reduce development costs.
Patent Information
- Application Number
- CN202411284663.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-13
- Publication Date
- 2024-12-06
AI Technical Summary
The traditional byte-aligned data transmission method has high development complexity, difficulty in maintenance and is not suitable for high-level application development in mobile application development. Especially when using high-level programming languages, it is more complicated to process binary data, which is not conducive to rapid development. and iteration.
By formulating unified JSON rules, efficient data transmission and parsing is achieved, the development process is simplified, system performance and cross-platform compatibility is improved, specifically including defining rule files, converting requested data from parameter dictionary to binary arrays, and according to the rules The file is decoded into a structured object.
It realizes efficient data transmission and parsing, reduces development and maintenance costs, improves cross-platform compatibility and flexibility, simplifies data processing, and enhances system stability and performance.
Smart Images

Figure BDA0005042781540000081 
Figure BDA0005042781540000121 
Figure BDA0005042781540000171
Abstract
Description
Technical Field
[0001] The present invention relates to the field of mobile communication technology, and more specifically to a processing method for unified parsing of mobile terminal communication protocols. Background Art
[0002] In mobile application development, data transmission between mobile phones and servers is the core link.
[0003] The traditional way of data transmission is byte-aligned data transmission; byte alignment is an important concept in computer science that involves the way data is stored in memory to ensure efficient data access. Simply put, byte alignment means that data is arranged according to specific rules so that computers can read and write data more efficiently. This alignment rule is usually related to the size of the data type and the hardware architecture of the computer. For example, an integer may need to be aligned to a 4-byte boundary to ensure that the computer can read the integer in its entirety in one operation, rather than reading and combining the data in two operations.
[0004] The byte-aligned data transmission method is a common technology used in the underlying communication protocol. It achieves efficient data transmission and parsing by precisely controlling the byte length of each data field. The specific steps include:
[0005] Data format definition: The server and client predefine the format of data transmission, specifying the byte length, type, and order of each field.
[0006] Data packaging: When sending data, the data is packaged into a binary stream according to a predefined format.
[0007] Data parsing: When receiving data, parse each data field from the binary stream according to the predefined format.
[0008] The advantages of byte-aligned data transmission method are:
[0009] High data transmission efficiency: The data packets are compact, the amount of data transmitted is small, and bandwidth is saved.
[0010] Fast parsing speed: Due to the use of fixed byte length, the parsing process is simple and fast.
[0011] Disadvantages: High development complexity: The byte boundaries of each field need to be handled accurately, which increases development complexity and the possibility of errors.
[0012] Difficult maintenance: Any change in a field requires modifying the parsing logic of both the sender and receiver, increasing maintenance costs.
[0013] Not suitable for high-level application development: When using high-level programming languages (such as TypeScript), processing binary data is more complicated, which is not conducive to rapid development and iteration. Summary of the invention
[0014] In order to solve the above problems, the present invention achieves efficient data transmission and parsing by formulating unified JSON rules, simplifies the development process, and improves system performance and cross-platform compatibility.
[0015] To achieve the above object, the present invention provides the following technical solutions, which mainly include:
[0016] The processing method for unified parsing of mobile communication protocols includes:
[0017] Define a rule file, which is used to convert the byte-aligned structure defined by the server into a configuration file;
[0018] Load the defined rule file into the system so that the system can process data according to the rules;
[0019] Convert the requested data from the parameter dictionary into a binary array and package the data;
[0020] When the server returns binary data, the system decodes the binary data into a structured object according to the definition of the rule file.
[0021] Preferably, the configuration file uses a JSON structure, and the fields required by the protocol are passed in according to the configuration file rules.
[0022] Preferably, define a JSON rule file:
[0023] main: main protocol number;
[0024] child: secondary protocol number;
[0025] format: request type, including binary or custom parsing format;
[0026] request: array of request fields;
[0027] Request fields:
[0028] name: field name;
[0029] type: field type;
[0030] size: field length;
[0031] response: array of response fields;
[0032] Response fields:
[0033] type: field type;
[0034] size: field length;
[0035] propertyRefer: other associated structures;
[0036] propertyKey: other associated json files.
[0037] Preferably, when loading the rule file, the system will read the pre-defined JSON rule file when it is started or running, and guide the next operation according to the rules in the file. The specific process includes:
[0038] Parsing and conversion: The system must first read and parse the rule file in JSON format, converting the data in the file, such as field name, type, and size, into a data structure that the program can understand;
[0039] Data structure initialization: Once the rule file is successfully parsed, the system will store the parsed information in the appropriate data structure for subsequent use;
[0040] Dynamic loading and storage: The whole process is dynamic, and the system can load different rule files at any time according to actual needs; it is used to handle different data formats and communication requirements.
[0041] 6. Preferably, the specific steps of converting the requested data from the parameter dictionary into a binary array are:
[0042] S1. Initialization phase:
[0043] Initialize a data processing context based on the field information that needs to be passed in the request;
[0044] Create an IntConverter instance to manage and encode the data stream and format the data to be packaged.
[0045] S2. Traverse the rule file:
[0046] According to the JSON rule file loaded earlier, process each field, obtain the field information, and perform conversion based on the information;
[0047] Processing hash table type fields:
[0048] If the data structure of some fields is complex, the system will extract the corresponding data from the hash table of the request parameters and process it;
[0049] Handling array type fields:
[0050] If a field is an array, the system will iterate over each element in the array, convert it to binary format one by one and write it to IntConverter;
[0051] Handling other types of fields:
[0052] For other types of data fields, the system obtains the corresponding data from the hash table and converts it into binary;
[0053] Binary conversion of field values:
[0054] Convert the value of each field into the corresponding number of bytes according to its type and write it into IntConverter to ensure that each field is stored in the correct binary format;
[0055] Returns binary data:
[0056] Finally, the system converts all processed fields into a complete binary array, ready for transmission to the server.
[0057] Preferably, the specific steps of restoring binary data to structured data are:
[0058] Initialization process:
[0059] The system first creates an empty hash table or dictionary to store the parsed data;
[0060] During the parsing process, each field will be put into this hash table in turn, and finally a complete response data object will be formed;
[0061] Traverse the byte data list:
[0062] The system reads fields from the binary data one by one according to the definition in the rule file and parses these fields into the corresponding format;
[0063] Fields in processing rules:
[0064] If the structure of some fields is complex, the system will recursively parse these fields and parse out the complex data structure layer by layer;
[0065] Handling array type fields:
[0066] If a field is an array, the system reads the length of the array and parses each array element one by one until the entire array is parsed;
[0067] Handling other types of fields:
[0068] For simple data types other than arrays, the system directly reads the corresponding data length from the binary data, converts it to the corresponding value, and stores it in the hash table;
[0069] Return the parsing result:
[0070] Finally, the system returns the parsed data, and the hash table contains the names of all fields and their corresponding values.
[0071] Preferably, convert each field from binary to a value suitable for JSON structure. Here are the steps for it:
[0072] Field parsing and initialization:
[0073] The system iterates over each field, extracting the field's name, type, and size.
[0074] Use a Map data structure to efficiently find and store the values (key-value pairs) corresponding to the fields.
[0075] Byte serialization and padding:
[0076] For multi-byte fields (such as strings or arrays), the system creates a byte array of sufficient size and fills it with characters or data one by one, ensuring byte alignment.
[0077] If the field is of character type, the system will convert the characters into their corresponding ASCII codes and store them.
[0078] Byte writing and type processing:
[0079] The system determines how many bytes should be written based on the type of the field (such as integer, string, etc.) and writes these bytes to the IntConverter.
[0080] The IntConverter manages these bytes and ensures that they are processed and stored correctly.
[0081] Exception and boundary handling:
[0082] The system will handle possible exceptions, such as when a field value is empty or undefined. In this case, the system will use the default value to ensure that the parsed data is correct.
[0083] If a multi-byte field is insufficient, the system will use a zero-fill strategy to avoid data loss.
[0084] Through this core algorithm, the system can convert binary data into JSON format field by field according to the rule file, which is convenient for subsequent use.
[0085] Preferably, in S2, each field is processed according to the previously loaded JSON rule file, and the processing algorithm is addBinaryElementToJson:
[0086] The main function of the algorithm is to convert each field from binary to a value that fits into the JSON structure. Here are its steps:
[0087] Field parsing and initialization:
[0088] The system iterates over each field, extracting the field's name, type, and size;
[0089] Use a Map data structure to efficiently find and store the values corresponding to the fields;
[0090] Byte serialization and padding:
[0091] For multi-byte fields (such as strings or arrays), the system creates a byte array large enough and fills it with characters or data one by one to ensure byte alignment;
[0092] If the field is of character type, the system will convert the characters into their corresponding ASCII codes and store them;
[0093] Byte writing and type processing:
[0094] The system determines how many bytes should be written based on the type of the field (such as integer, string, etc.) and writes these bytes to IntConverter;
[0095] IntConverter manages these bytes and ensures they are processed and stored correctly;
[0096] Exception and boundary handling:
[0097] The system handles possible exceptions, such as when a field value is empty or undefined; in these cases, the system uses the default value.
[0098] It can be seen from the above technical solution that, compared with the prior art, the beneficial effects of the technical solution can be summarized as follows:
[0099] 1. Efficient data transmission and analysis
[0100] By combining JSON rule files with binary data transmission, you can simplify the data parsing process while maintaining data transmission efficiency. Specifically:
[0101] Binary format transmission: Data is transmitted over the network in a compact binary format, which reduces the amount of data transmitted and increases the transmission speed. It is particularly suitable for scenarios with limited network bandwidth or high real-time requirements.
[0102] Efficient parsing: Using rule files, the system can parse binary data quickly and accurately without manually processing the parsing logic of each field, reducing the possibility of errors.
[0103] 2. Flexible rule definition and scalability
[0104] Through the dynamic definition of JSON rule files, the system can easily adapt to different data structures and protocol requirements:
[0105] Flexibility: JSON rule files can be adjusted according to different application scenarios without modifying the core code of the system. Only the rule files need to be modified to adapt to new data types or protocols.
[0106] Scalability: When the data format or communication protocol changes, developers only need to adjust or add new rule files without large-scale reconstruction of the system.
[0107] 3. Cross-platform compatibility
[0108] JSON is a standardized data format that can be easily processed by multiple programming languages and platforms:
[0109] Universality: Since the data transmission and parsing methods defined by the JSON rule file are universal, this solution can work seamlessly between different devices and platforms, such as mobile phones, servers, PCs, etc.
[0110] Platform independence: The underlying data transmission of the system adopts binary format, but the rules are defined and parsed through JSON, which makes the solution applicable to different operating systems and programming languages.
[0111] 4. Reduce development and maintenance costs
[0112] Simplify the development process: Developers do not need to have an in-depth understanding of the complex underlying communication protocols and data transmission details. They only need to define fields and data types through JSON rule files to complete data packaging and parsing.
[0113] Easy to maintain: If the system needs to be updated or the data format needs to be changed, only the JSON rule file needs to be modified without large-scale modification of the code logic, which greatly reduces maintenance costs.
[0114] 5. Security
[0115] Exception handling: The system uses default value mechanism and boundary processing to ensure that data transmission and parsing will not be interrupted when the field value is empty or undefined, thereby enhancing the stability of the system.
[0116] Error capture mechanism: During the parsing and packaging process, the system introduces an error capture mechanism that can effectively prevent system crashes due to data anomalies.
[0117] 6. Optimized memory management and performance
[0118] Memory management: Through the use of IntConverter, the system can efficiently manage memory, especially in the case of high load or large-scale data transfer, the performance is significantly optimized.
[0119] Fast parsing: Through hash mapping and red-black tree structures, the system can quickly search and parse data within a time complexity of O(1) or O(log n), improving the overall data processing efficiency. DETAILED DESCRIPTION
[0120] The technical solutions in the embodiments of the present invention are described clearly and completely below. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0121] Embodiment 1
[0122] Here, we want to request and parse the market data of a certain stock code. To achieve this goal, we need to define a rule file, which converts the byte-aligned data structure defined by the server into a JSON configuration file that is easier to process. This makes data transmission and parsing simpler and has better compatibility and flexibility.
[0123] JSON (JavaScript Object Notation) is a format for data exchange. Its structure is simple, like a list or table, which helps different systems or applications understand and exchange information with each other.
[0124] Next, according to the rules in the configuration file, the required fields are passed into the protocol, and then these fields can be used to initiate a request for stock data.
[0125] The first part of the detailed steps:
[0126] Define JSON rule file
[0127] The purpose of the JSON rule file is to help the system understand how to process data. When parsing the market data of a certain stock code, we need to define a set of rules that will tell the system how to parse or package the stock data. These rules include:
[0128] main: Main protocol number, indicating which category of data this is a request for (for example, the main protocol number for requesting stock data).
[0129] Child: secondary protocol number, indicating the specific operation under the primary protocol number (for example, requesting the details of a specific stock).
[0130] format: The requested data format, which may be binary format or custom format.
[0131] request: defines an array of request fields, including the name, type, and size of each field (for example, stock code, request time, etc.).
[0132] response: An array defining the response fields, including the name, type, and size of the fields (e.g., stock price, opening time, etc.).
[0133] For example, suppose you need to request data for a certain stock, and the rule file is:
[0134]
[0135] main:1001 means we want to request stock data.
[0136] child: 2001 indicates the data of a specific stock.
[0137] The request part defines the fields to be passed in the request, such as the stock symbol (6 characters) and the request time (14 characters).
[0138] The response part defines the format of the returned data, such as the stock price and opening time.
[0139] This rule file defines how the system passes these fields to the server and how the server returns this data to the client.
[0140] The role of the rule file is to tell the system how to send and receive data, ensuring that the server and the client can understand the information transmitted to each other.
[0141] Specifically:
[0142] Passing to the server: When you want to request some data from the server, such as the price of a certain stock, the system needs to know what information to send and how to package it. In the rule file, it is defined which fields (such as stock code, request time, etc.) need to be sent, as well as their types and lengths.
[0143] For example, the rule file tells the system to send a 6-character stock code and a 14-character request time. The system will package the data according to this rule and send it to the server in the correct format.
[0144] Receive data from the server: After receiving the request, the server will return the data in the format defined in the rule file. For example, the server returns the stock price (floating point type, 4 bytes) and the opening time (string type, 8 characters). The system knows how to parse the returned data through the rule file and correctly understand the stock price and the opening time.
[0145] The rule file is like a "translation guide" that helps the system send requests and receive responses correctly, ensuring that the communication between the two parties is conducted in a unified format to avoid incorrect or unrecognizable data.
[0146] Part 2: Loading the rules file
[0147] This section describes how to load the defined JSON rule file into the system so that the system can correctly process data according to the rules.
[0148] Loading a rule file means that when the system starts or runs, it will read the pre-defined JSON rule file and guide the next operation according to the rules in the file. The specific process includes:
[0149] Parsing and conversion: The system must first read and parse the rule file in JSON format, and convert the data in the file (such as field name, type, size, etc.) into a data structure that the program can understand.
[0150] Data structure initialization: Once the rule file is parsed successfully, the system will store the parsed information in appropriate data structures, such as hash tables, arrays, etc., for later use.
[0151] Dynamic loading and storing: The whole process is dynamic, which means that the system can load different rule files at any time according to actual needs. This ensures the flexibility of the system and can handle different data formats and communication requirements.
[0152] Through this loading mechanism, the system can seamlessly transfer and parse data according to the rule files, ensuring good performance and stability in complex environments.
[0153] Simply put, this step is that the system loads the pre-defined JSON rule file and is ready to use these rules to process data.
[0154] Part 3: Data Packaging Process
[0155] This section describes how to convert the requested data from the parameter dictionary (which can be understood as a set of key-value pairs, similar to the pairing of "field name" and "value") into a binary array so that efficient data transmission can be performed.
[0156] In the process of data transmission, in order to ensure efficiency and cross-platform compatibility, it is usually necessary to package the data into a binary format. This not only reduces the amount of data transmission, but also ensures smoother communication between different platforms (such as mobile devices and servers).
[0157] Efficient memory management: Flexibly allocate and manage memory through IntConverter (a tool class for managing and converting data), ensuring that data transmission does not slow down the system in high-load scenarios.
[0158] Dynamic structure parsing: Able to handle a variety of complex data structures, such as nested objects or arrays, ensuring correct parsing and packaging under different data types.
[0159] Scalability: The algorithm design takes into account future expansion needs and can easily integrate new data types or rules to facilitate the response to ever-changing business scenarios.
[0160] Platform independence: This packaging method does not depend on a specific operating system or hardware, and data can be seamlessly transferred between different platforms.
[0161] Security: When packaging and transmitting data, an error capture mechanism is used to ensure that the system can still run stably when an exception occurs, thereby ensuring the reliability of data transmission.
[0162] Algorithm Logic
[0163] How to convert data into binary format?
[0164] Initialization phase:
[0165] Initialize a data processing context (that is, prepare space to store and process this data) based on the field information that needs to be passed in the request.
[0166] Create an IntConverter instance to manage and encode the data stream and format the data to be packaged.
[0167] Traverse the rules file:
[0168] According to the JSON rule file loaded earlier, process each field (for example, stock code, request time, etc.), obtain the field information (such as length, type), and convert based on this information.
[0169] Processing hash table type fields:
[0170] If the data structure of some fields is complex (such as hash table type), the system will extract the corresponding data from the hash table of the request parameters and process it.
[0171] Handling array type fields:
[0172] If a field is an array (such as multiple stock symbols), the system will iterate over each element in the array, convert it to binary format one by one, and write it to the IntConverter.
[0173] Handling other types of fields:
[0174] For other types of data fields, the system obtains the corresponding data from the hash table and converts it into binary.
[0175] Binary conversion of field values:
[0176] Convert the value of each field into the corresponding number of bytes according to its type (such as string, integer, floating point number, etc.), and write it into IntConverter to ensure that each field is stored in the correct binary format.
[0177] Returns binary data:
[0178] Finally, the system converts all processed fields into a complete binary array, ready for transmission to the server.
[0179] The data packaging process is closely related to the previous rule file loading process. They are two closely coordinated parts of the data transmission process.
[0180] Relevance analysis:
[0181] Rules file loading is the basis:
[0182] Before data packaging, the system first needs to load and parse the JSON rule file, which defines how to transmit and parse data. This step is the basis of data packaging.
[0183] The rule file details the name, type, and size of each field, which is essential in the data packaging process. The system reads this rule file to know how each field should be converted into binary format.
[0184] Data packaging dependency rule file:
[0185] Once the rule files are loaded, the data packaging process can be executed according to these rules.
[0186] When packaging, the system will find the location of each field in the request data and determine the data type and length of each field according to the instructions in the rule file. The system will then convert these fields into binary format according to the rules.
[0187] For example, if the rule file specifies that a certain field is a 4-byte integer, then during the data packing process, the system will ensure that this field is packed as 4 bytes.
[0188] The rule file guides the packaging format:
[0189] The JSON rule file also defines the order and format in which the data should be transmitted (such as binary or custom format). These rules ensure that the data remains consistent when it is packaged. The system will strictly follow these rules to arrange the order and alignment of the data.
[0190] For example, if the rule file stipulates that the "stock code" field is a 6-byte string, the system will accurately convert this field into 6-byte binary data during packaging and insert it into the entire data packet.
[0191] Cross-platform compatibility is achieved through rule files:
[0192] Because the rule files are loaded dynamically, the system can flexibly handle different protocols or data structures, thereby achieving cross-platform compatibility. The data packaging process relies on the rule files to adapt to the requirements of different platforms and ensure that the transmitted binary data can be correctly parsed by the receiver.
[0193] Part 4: Data Analysis
[0194] When the server returns binary data, how does the system decode the binary data into structured objects according to the definition of the rule file so that the client can correctly understand and use the data.
[0195] The binary data packet returned by the server will be gradually parsed by the system according to the definition in the rule file, and finally converted into structured data similar to a dictionary or object (for example: stock price, opening time, etc.).
[0196] The algorithm logic includes:
[0197] The parsing process is similar to packaging, but in the opposite direction, which is to restore binary data into structured data. The following are the steps of data parsing:
[0198] Initialization process:
[0199] The system first creates an empty hash table or dictionary to store the parsed data.
[0200] During the parsing process, each field will be placed in this hash table in turn, eventually forming a complete response data object.
[0201] Traverse the byte data list:
[0202] The system reads fields from the binary data one by one according to the definition in the rule file, and parses these fields into corresponding formats (such as integers, strings, floating-point numbers, etc.).
[0203] Fields in processing rules:
[0204] If the structure of some fields is complex (such as hash table type or nested structure), the system will recursively parse these fields and parse out the complex data structure layer by layer.
[0205] Handling array type fields:
[0206] If a field is an array (such as the prices of multiple stocks), the system reads the length of the array and parses each array element one by one until the entire array is parsed.
[0207] Handling other types of fields:
[0208] For simple non-array data types (such as integers and strings), the system directly reads the corresponding data length from the binary data, converts it to the corresponding value, and stores it in the hash table.
[0209] Return the parsing result:
[0210] Finally, the system returns the parsed data (stored in a hash table). This hash table contains the names of all fields and their corresponding values, similar to:
[0211]
[0212] "price":150.25,
[0213] "openTime":"09:30"}
[0214] The data parsing process is to gradually decode the binary data returned by the server into structured objects or dictionaries according to the previously loaded JSON rule files, so that the client can easily read and use the data.
[0215] The system accurately extracts the data of each field from the binary stream according to the field type, length and other information in the rule file, and restores them into an easy-to-understand format.
[0216] The main logic of parsing the response field information. This section will explain in more detail how to parse the binary data returned by the server.
[0217] The main logic of response field information parsing
[0218] initialization:
[0219] The system creates an "offset index" variable with an initial value of 0. This index is used to keep track of where in the binary data packet the parsing is currently done.
[0220] Each time a field is parsed, the offset index is updated to the position of the next field.
[0221] Fields in processing rules:
[0222] The system will read the fields in the binary data one by one according to the rule file. If a field is a hash table type or a complex data structure, the system will recursively call itself and continue to parse the field until all subfields are parsed.
[0223] For example, if a field in the data returned by the server is a nested object, the system will continue to parse each field in the object recursively.
[0224] Array types in processing rules:
[0225] If a field is of array type (such as the prices of multiple stocks), the system will first obtain the length of the array according to the definition in the rule file.
[0226] Then, the system will traverse each array element according to the length of the array, parse them out one by one, and store them in the array object.
[0227] For example, if the returned data contains an array of 5 stock prices, the system will read 5 floating point numbers and store them in an array.
[0228] Processing hash table type fields:
[0229] For hash table type fields, the system creates a dataObject object to store the parsed key-value pairs.
[0230] The system then iterates over all fields defined in the rule file, extracts the corresponding values from the binary data, and stores them in the dataObject.
[0231] Handling non-array fields:
[0232] If the field is not an array type, such as a single string or integer, the system will directly read the corresponding number of bytes from the binary data and convert it to the corresponding value.
[0233] For example, if the returned data contains a 4-byte integer, the system reads those 4 bytes from the binary stream, converts them to an integer, and then stores it in the response hash table.
[0234] Return the parsing result:
[0235] Once all fields are parsed, the system returns a hash table or dictionary object containing the parsed content, which stores all the information extracted from the binary data.
[0236] Core algorithm addBinaryElementToJson:
[0237] The main function of this algorithm is to convert each field from binary to a value suitable for the JSON structure. The following are its steps:
[0238] Field parsing and initialization:
[0239] The system iterates over each field, extracting the field's name, type, and size.
[0240] Use a Map data structure to efficiently find and store the values (key-value pairs) corresponding to the fields.
[0241] Byte serialization and padding:
[0242] For multi-byte fields (such as strings or arrays), the system creates a byte array of sufficient size and fills it with characters or data one by one, ensuring byte alignment.
[0243] If the field is of character type, the system will convert the characters into their corresponding ASCII codes and store them.
[0244] Byte writing and type processing:
[0245] The system determines how many bytes should be written based on the type of the field (such as integer, string, etc.) and writes these bytes to the IntConverter.
[0246] The IntConverter manages these bytes and ensures that they are processed and stored correctly.
[0247] Exception and boundary handling:
[0248] The system will handle possible exceptions, such as when a field value is empty or undefined. In this case, the system will use the default value to ensure that the parsed data is correct.
[0249] If a multi-byte field is insufficient, the system will use a zero-fill strategy to avoid data loss.
[0250] Through this core algorithm, the system can convert binary data into JSON format field by field according to the rule file, which is convenient for subsequent use.
[0251] In this step, the system parses each field one by one from the received binary data according to the format and fields defined in the rule file and stores them in the dictionary.
[0252] The system tracks the progress of the parsing via offset subscripts and uses a recursive approach to handle complex structures such as nested objects and arrays.
[0253] Finally, the system returns a complete, structured response object containing all parsed data.
[0254] Core algorithm addBinaryElementToJson
[0255] The main task of this algorithm is to parse binary data into JSON format. Specifically, the data returned by the server is in binary form, and we need to convert these binary data into readable JSON objects according to the definition in the rule file. The following are the main steps of the algorithm:
[0256] 1. Field parsing and initialization:
[0257] First, the system iterates over each field defined in the rule file and extracts the field's name, type, and size.
[0258] The system uses a Map or similar key-value pair structure to store the names and values of these fields. This makes it easy to find the value corresponding to each field.
[0259] 2. Byte serialization and padding:
[0260] For multi-byte fields (such as strings, arrays, or integers), the system creates a byte array for these fields. The size of this byte array is determined by the field size defined in the rule file.
[0261] The system fills this array byte by byte. If the field is a string, the system converts each character in the string into its ASCII code and then writes these ASCII codes into the byte array one by one.
[0262] If the field is an array type, the system directly converts each element in the array into binary format and fills it into the byte array in sequence.
[0263] The system ensures that all fields are converted and stored strictly as defined in the rule file. If there are insufficient fields, a zero-fill strategy is used to ensure byte alignment.
[0264] 3. Byte writing and type processing:
[0265] The system determines how many bytes to write based on the type of the field (integer, string, floating point, etc.) For example, a 4-byte integer will take up 4 bytes, while each character of a string will take up 1 byte.
[0266] IntConverter is a tool class for managing binary data streams. Its function is to write these calculated byte data one by one into the final binary stream and ensure that the byte length of each field is correct.
[0267] The system uses the getByteValueLength function to determine the number of bytes each field should occupy and performs precise processing based on the type.
[0268] 4. Exception and boundary handling:
[0269] During the parsing process, some fields may be empty or undefined. To ensure the robustness of the system, the algorithm introduces a default value mechanism to handle these abnormal situations.
[0270] For example, if a string field has no value, a default empty string or 0 value is used instead to ensure that the parsing process is not interrupted.
[0271] For multi-byte fields (such as arrays or strings), the system uses a zero-filling strategy to fill the remaining bytes with zero values when the field data is insufficient. This strategy can minimize the risk of data loss.
[0272] The addBinaryElementToJson algorithm traverses the field information in the rule file, parses each field from the binary data in turn, and converts the data into a format suitable for the JSON structure. It uses multiple mechanisms to ensure the robustness and efficiency of the parsing process:
[0273] Field parsing and initialization: Extract the name, type, and size of each field in preparation for parsing.
[0274] Byte serialization and padding: Convert data to binary and ensure byte alignment.
[0275] Byte writing and type handling: The number of bytes of each field is handled accurately according to the data type.
[0276] Exception and boundary handling: handles situations where fields are empty or undefined to ensure that the system does not fail due to data anomalies.
[0277] The final output of the whole process is a structured JSON object, which contains all the parsed data fields. The values of these fields will be parsed and converted strictly according to the definition of the rule file to ensure the integrity and correctness of the data.
[0278] This core algorithm addBinaryElementToJson is mainly used in the data parsing process. Its function is to parse the binary data into structured JSON objects when receiving them. Specifically, it is responsible for gradually restoring the original binary data returned by the server into key-value pairs (i.e. JSON format) that can be directly used by the client according to the definition of the JSON rule file.
[0279] Specific parts of the core algorithm application:
[0280] In data parsing, this algorithm is used to parse the binary data returned by the server, extract each field from the binary, and convert it into JSON format.
[0281] It will traverse the binary data returned by the server, and use addBinaryElementToJson to convert the data into easy-to-read JSON objects field by field according to the field type, length and other information defined in the rule file.
[0282] Specifically:
[0283] Receiving data: When the system receives a binary data packet from the server, the system begins to parse the data packet step by step according to the format in the rule file.
[0284] Calling the addBinaryElementToJson algorithm: The system processes each field in turn, calling this core algorithm to extract the value of the field from the binary data and convert it into the corresponding JSON key-value pair.
[0285] Generate JSON object: After the value of each field is parsed by this algorithm, it will be added to the final JSON object. After all fields are parsed, the entire binary data packet is converted into a complete JSON object.
[0286] Example:
[0287] Assume that the server returns a binary packet containing the following:
[0288] A stock code (string type, 6 bytes)
[0289] A price (floating point type, 4 bytes)
[0290] An opening time (string type, 8 bytes)
[0291] The system will call addBinaryElementToJson to parse these fields one by one:
[0292] Read 6 bytes from the binary stream and parse it into the string "stock symbol".
[0293] Read 4 bytes and parse as floating point number "price".
[0294] Read 8 bytes and parse them into the string "opening time".
[0295] Finally, all parsed fields are combined into a JSON object, similar to:
[0296]
[0297] Therefore, the addBinaryElementToJson algorithm is the key part for converting the received binary data into an object in JSON format.
[0298] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0299] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A processing method for unified analysis of mobile terminal communication protocols, characterized in that: include: Define a rule file, which is used to convert the byte-aligned structure defined by the server into a configuration file; Load the defined rule file into the system so that the system can process data according to the rules; Convert the requested data from the parameter dictionary into a binary array and package the data; When the server returns binary data, the system decodes the binary data into a structured object according to the definition of the rule file.
2. The processing method according to claim 1, characterized in that: The configuration file uses a JSON structure and the fields required by the protocol are passed in according to the configuration file rules.
3. The processing method according to claim 2, characterized in that: Define the JSON rule file: main: main protocol number; child: secondary protocol number; format: request type, including binary or custom parsing format; request: array of request fields; Request fields: name: field name; type: field type; size: field length; response: array of response fields; Response fields: type: field type; size: field length; propertyRefer: other associated structures; propertyKey: other associated json files.
4. The processing method according to claim 3, characterized in that: Load the rule file. When the system starts or runs, it will read the pre-defined JSON rule file and guide the next operation according to the rules in the file. The specific process includes: Parsing and conversion: The system must first read and parse the rule file in JSON format, converting the data in the file, such as field name, type, and size, into a data structure that the program can understand; Data structure initialization: Once the rule file is successfully parsed, the system will store the parsed information in the appropriate data structure for subsequent use; Dynamic loading and storage: The whole process is dynamic, and the system can load different rule files at any time according to actual needs; it is used to handle different data formats and communication requirements.
5. The processing method according to claim 1, characterized in that: The specific steps to convert the requested data from the parameter dictionary into a binary array are: S1. Initialization phase: Initialize a data processing context based on the field information that needs to be passed in the request; Create an IntConverter instance to manage and encode the data stream and format the data to be packaged. S2. Traverse the rule file: According to the JSON rule file loaded earlier, process each field, obtain the field information, and perform conversion based on the information; Processing hash table type fields: If the data structure of some fields is complex, the system will extract the corresponding data from the hash table of the request parameters and process it; Handling array type fields: If a field is an array, the system will iterate over each element in the array, convert it to binary format one by one and write it to IntConverter; Handling other types of fields: For other types of data fields, the system obtains the corresponding data from the hash table and converts it into binary; Binary conversion of field values: Convert the value of each field into the corresponding number of bytes according to its type and write it into IntConverter to ensure that each field is stored in the correct binary format; Returns binary data: Finally, the system converts all processed fields into a complete binary array, ready for transmission to the server.
6. The processing method according to claim 1, characterized in that: Specific steps to restore binary data to structured data: Initialization process: The system first creates an empty hash table or dictionary to store the parsed data; During the parsing process, each field will be put into this hash table in turn, and finally a complete response data object will be formed; Traverse the byte data list: The system reads fields from the binary data one by one according to the definition in the rule file and parses these fields into the corresponding format; Fields in processing rules: If the structure of some fields is complex, the system will recursively parse these fields and parse out the complex data structure layer by layer; Handling array type fields: If a field is an array, the system reads the length of the array and parses each array element one by one until the entire array is parsed; Handling other types of fields: For simple data types other than arrays, the system directly reads the corresponding data length from the binary data, converts it to the corresponding value, and stores it in the hash table; Return the parsing result: Finally, the system returns the parsed data, and the hash table contains the names of all fields and their corresponding values.
7. The processing method according to claim 5, characterized in that: Convert each field from binary to a value suitable for JSON structure. Here are the steps: Field parsing and initialization: The system iterates over each field, extracting the field's name, type, and size. Use a Map data structure to efficiently find and store the values (key-value pairs) corresponding to the fields. Byte serialization and padding: For multi-byte fields (such as strings or arrays), the system creates a byte array of sufficient size and fills it with characters or data one by one, ensuring byte alignment. If the field is of character type, the system will convert the characters into their corresponding ASCII codes and store them. Byte writing and type processing: The system determines how many bytes should be written based on the type of the field (such as integer, string, etc.) and writes these bytes to the IntConverter. The IntConverter manages these bytes and ensures that they are processed and stored correctly. Exception and boundary handling: The system will handle possible exceptions, such as when a field value is empty or undefined. In this case, the system will use the default value to ensure that the parsed data is correct. If a multi-byte field is insufficient, the system will use a zero-fill strategy to avoid data loss. Through this core algorithm, the system can convert binary data into JSON format field by field according to the rule file, which is convenient for subsequent use.
8. The processing method according to claim 5, characterized in that: In S2, each field is processed according to the JSON rule file loaded earlier, and the processing algorithm is addBinaryElementToJson: The main function of the algorithm is to convert each field from binary to a value that fits into the JSON structure. Here are its steps: Field parsing and initialization: The system iterates over each field, extracting the field's name, type, and size; Use a Map data structure to efficiently find and store the values corresponding to the fields; Byte serialization and padding: For multi-byte fields (such as strings or arrays), the system creates a byte array large enough and fills it with characters or data one by one to ensure byte alignment; If the field is of character type, the system will convert the characters into their corresponding ASCII codes and store them; Byte writing and type processing: The system determines how many bytes should be written based on the type of the field (such as integer, string, etc.) and writes these bytes to IntConverter; IntConverter manages these bytes and ensures they are processed and stored correctly; Exception and boundary handling: The system handles possible exceptions, such as when a field value is empty or undefined; in these cases, the system uses the default value.
Citation Information
Cited By
Standardized heterogeneous communication data interaction design method
CN121193836A
