Configuration file conversion or processing method, electronic device, and storage medium
By converting JSON format configuration files into a frame structure and removing redundant information, the problem of traditional JSON format occupying large storage space and memory is solved, and more efficient configuration file storage and processing is achieved.
Patent Information
- Application Number
- CN202410761576.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-13
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-06-13
AI Technical Summary
Traditional JSON format configuration files occupy a large storage space, and the parsing code occupies a lot of memory, causing RAM overflow in embedded systems.
Convert the JSON format configuration file into a frame structure, remove redundant information, and use the frame structure for storage and processing, including preset frame header, frame sequence and CRC check.
It reduces the storage space requirement of the configuration file, simplifies the processing process, avoids RAM overflow, and improves the efficiency and simplicity of configuration file processing.
Smart Images

Figure CN118689557B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a configuration file conversion or processing method, an electronic device, and a storage medium. Background Art
[0002] Configuration files are often used to define system behavior and parameter settings, and they play a vital role in embedded systems. Currently, traditional configuration files are typically formatted in JSON (JavaScript Object Notation). However, JSON-formatted configuration files consume a large amount of storage space, as does the code used to parse them. Furthermore, the conversion process requires a significant amount of temporary memory, which can lead to RAM overflow in embedded systems with limited RAM.
[0003] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of this application, and therefore may include information that does not constitute prior art known to ordinary technicians in this field. Summary of the Invention
[0004] In order to provide a basic understanding of some aspects of the disclosed embodiments, a brief summary is given below. The summary is not an extensive review, nor is it intended to identify key / critical elements or delineate the scope of protection of these embodiments, but rather serves as a prelude to the detailed description that follows.
[0005] Embodiments of the present application provide a configuration file conversion or processing method, an electronic device, and a storage medium to reduce the storage space occupied by the configuration file.
[0006] An embodiment of the present application provides a configuration file conversion method, including: obtaining a JSON format configuration file to be converted; creating corresponding frame structures for each object in the JSON format configuration file; the frame structure is a sequence in which the key-value pairs included in the object are arranged in a preset order and a preset frame header is added; and each frame structure is formed into a target conversion file.
[0007] In the above embodiment, since the JSON file format contains a large number of human-readable elements, such as key names, quotation marks, commas, and line breaks, the configuration file in JSON format will occupy a large amount of storage space. By converting the JSON configuration file into a frame structure, since the frame structure only needs to sequentially store the key-value pairs and preset frame headers included in the object, the configuration file in JSON format can be converted into a configuration file in the form of a frame structure. Therefore, redundant information such as key names, quotation marks, commas, and line breaks can be removed, thereby reducing the storage space occupied by the configuration file. At the same time, when processing the configuration file in JSON format, each time a configuration is added, the key name and key value need to be re-parsed and reused. By converting the JSON configuration file into a frame structure configuration file, redundant information is reduced. When processing the configuration file in the frame structure, the processing process and processing complexity of the configuration file can be simplified, making the addition of the configuration file simpler and more convenient.
[0008] Furthermore, the JSON format configuration file is obtained in the following manner: obtaining a preset target structure description table; the target structure description table includes a structure name and a data structure corresponding to the structure name; the data structure includes: a configuration name, a configuration offset, a configuration length, and a default value; for each of the structure names: constructing a structure for the data structure corresponding to the structure name as a grandchild node; constructing a structure containing the grandchild node as a child node for the structure name; wherein the child node serves as an object in the JSON format configuration file.
[0009] In the above implementation, by constructing the configuration name, configuration offset, configuration length, default value, etc. corresponding to the structure name into a grandchild node, and then constructing a structure containing the grandchild node as a child node for the structure name, the structure corresponding to the structure name can be easily obtained. After constructing a corresponding structure for each structure name, the JSON format configuration file to be converted can be obtained.
[0010] Furthermore, the object is a child node corresponding to the structure name in the JSON format configuration file; creating a corresponding frame structure for the object in the JSON format configuration file includes: obtaining the child node key-value pair corresponding to the object and the grandchild node key-value pair included in the child node; arranging the child node key-value pair and the grandchild node key-value pair in a predetermined order to obtain a frame sequence; wherein, the frame sequence with a preset frame header and the total length of the sub-items added is the frame structure corresponding to the object.
[0011] In the above embodiment, by only obtaining the child node key-value pairs and the grandchild node key-value pairs and constructing them into a frame sequence, the frame sequence will not contain redundant information such as key names, quotation marks, commas, and line breaks, thereby making the storage space occupied by the converted configuration file smaller.
[0012] Furthermore, each of the frame structures is formed into a target conversion file, including: adding file header information in a preset buffer, the file header information including data length and a first CRC check value; serializing each of the frame structures into the buffer to obtain a frame to be processed; calculating a second CRC check value of the frame to be processed; and adding the second CRC check value to the end of the frame to be processed to obtain a target conversion file.
[0013] In the above embodiment, since data may be subject to transmission errors during transmission, a second CRC (Cyclic Redundancy Check) value is calculated and added to the end of the frame to be processed. This allows the receiving device to determine whether the data is complete based on the second CRC value.
[0014] Furthermore, the frame structure includes: a byte order attribute, serializing each frame structure into the buffer to obtain a frame to be processed, including: when the byte order attribute corresponding to the frame structure is big endian, adjusting the data item corresponding to the frame structure to big endian storage.
[0015] In the above implementation, the default byte order for data items is usually little-endian, but some data items may be stored in big-endian. Therefore, by using the byte order attribute to indicate the data items that need to be stored in big-endian, the frame structure that used to be in little-endian byte order is adjusted to big-endian byte order. This can meet special data processing requirements and avoid data reading errors caused by incorrect reading order when the electronic device accesses the frame structure.
[0016] Furthermore, after forming each of the frame structures into a target conversion file, the method further includes:
[0017] The target conversion file is written into a preset computer device.
[0018] In the above embodiment, by writing the target conversion file into a preset computer device, since the computer device directly receives the target conversion file in the form of a frame structure, and since the target conversion file removes redundant information such as key names, quotation marks, commas, and line breaks, the process in which the computer device parses the target conversion file and stores it in the flash memory of the computer device is easier and simpler than the process in which the computer device parses the JSON format configuration file and stores it in the flash memory of the computer device.
[0019] Furthermore, the computer device is a smart lock; the configuration content of the JSON format configuration file includes the lock voice volume, the lock automatic locking time, and the lock type.
[0020] An embodiment of the present application provides a configuration file processing method, including: receiving the target conversion file; parsing the target conversion file to obtain the structure name and frame length corresponding to each frame structure; and storing the data structure corresponding to the structure name to a preset memory location based on the frame length and the structure name.
[0021] In the above implementation, by parsing the target conversion file, the data in the target conversion file is copied to the memory location pointed to by the corresponding structure variable, so as to facilitate use by subsequent programs.
[0022] Furthermore, before parsing the target conversion file, the method further includes: checking whether the length of the target conversion file is correct; and parsing the target conversion file if the length is correct.
[0023] In the above embodiment, if the configuration file stored in the computer device is incorrect, the computer device may cause an error in operation. Therefore, the length of the target conversion file is first checked, and the target conversion file is parsed only when the length is correct, which can avoid the storage of an incorrect configuration file in the computer device.
[0024] Furthermore, before parsing the target conversion file, the method further includes: verifying whether data of the target conversion file is correct; and parsing the target conversion file if the data is correct.
[0025] In the above embodiment, if the configuration file stored in the computer device is incorrect, the computer device may cause an error in operation. Therefore, the data of the target conversion file is first verified, and the target conversion file is parsed only when the data is correct, which can avoid the storage of an incorrect configuration file in the computer device.
[0026] An embodiment of the present application provides an electronic device, including a processor and a memory, wherein the memory stores computer-executable instructions that can be executed by the processor, and the processor executes the computer-executable instructions to implement the above-mentioned configuration file conversion method or the above-mentioned configuration file processing method.
[0027] In some embodiments, the storage medium stores computer-executable instructions. When the computer-executable instructions are called and executed by the processor, the computer-executable instructions enable the processor to implement the above-mentioned configuration file conversion method or the above-mentioned configuration file processing method.
[0028] The above general description and the following description are exemplary and explanatory only and are not intended to limit the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] One or more embodiments are exemplarily described by corresponding drawings. These exemplary descriptions and drawings do not limit the embodiments. Elements with the same reference numerals in the drawings are considered similar elements. The drawings do not constitute a scale limitation. In addition,
[0030] Figure 1 is a schematic diagram of a configuration file conversion method provided in an embodiment of the present application;
[0031] Figure 2 This is a schematic diagram of a configuration file processing method provided in an embodiment of the present application;
[0032] Figure 3 This is a schematic diagram of an electronic device provided in an embodiment of the present application.
[0033] Reference numerals:
[0034] 1: Processor; 2: Memory; 3: Communication interface; 4: Bus. DETAILED DESCRIPTION
[0035] In order to be able to understand the features and technical contents of the embodiments of the present application in more detail, the implementation of the embodiments of the present application is described in detail below in conjunction with the accompanying drawings. The accompanying drawings are for reference only and are not used to limit the embodiments of the present application. In the following technical description, for the sake of convenience of explanation, a full understanding of the disclosed embodiments is provided through multiple details. However, one or more embodiments can still be implemented without these details. In other cases, to simplify the drawings, well-known structures and devices can be simplified for display.
[0036] In the description and claims of the embodiments of the present application and the accompanying drawings, the terms "first," "second," and the like are used to distinguish similar objects and are not necessarily used to describe a particular order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate for the purposes of describing the embodiments of the present application. In addition, the terms "including," "having," and any variations thereof are intended to cover non-exclusive inclusions.
[0037] Unless otherwise stated, the term "plurality" means two or more.
[0038] The term "correspondence" may refer to an association relationship or a binding relationship. The correspondence between A and B means that there is an association relationship or a binding relationship between A and B.
[0039] Example 1
[0040] The present application provides a configuration file conversion method. Figure 1 As shown, Figure 1The following is a basic flow chart of the configuration file conversion method provided in the embodiment of the present application, including:
[0041] Step S101: Obtain a JSON format configuration file to be converted.
[0042] It is understood that a configuration file is a file used to define the initial configuration parameters and device properties of a computer device. For example, if the computer device is a smart lock, the initial configuration parameters may include the lock's voice volume, the lock's automatic lock time, and so on. The device properties may be the lock type. For example, if the computer device is a smart alarm clock, the initial configuration parameters may include the alarm voice volume, and the device properties may be the alarm clock type.
[0043] In some embodiments, a JSON format configuration file pre-written by an engineer can be directly obtained.
[0044] In some embodiments, the JSON format configuration file to be converted can also be obtained by: obtaining a preset target structure description table; the target structure description table includes a structure name and a data structure corresponding to the structure name; the data structure includes: a configuration name, a configuration offset, a configuration length, and a default value. For each structure name: constructing a structure as a grandchild node for the data structure corresponding to the structure name; constructing a structure containing the grandchild node as a child node for the structure name; wherein the child node is an object in the JSON format configuration file.
[0045] In some embodiments, the target structure description table specifies structure names, each of which includes one or more configuration names. Each configuration name is further provided with a configuration offset, configuration length, and default value. It will be understood that the configuration offset represents the data offset, the configuration length represents the data length, and the default value is the initial value of the configuration name. Furthermore, to facilitate engineers writing JSON-formatted configuration files based on the target structure description table, each configuration name can also be provided with a valid range and content description.
[0046] As you can understand, configuration names can also be called variable names. The valid range is the range of values within which the default value is set, and the content description explains the meaning of different default values. The target structure description table helps engineers quickly understand the data structure and configuration logic in the project, improving code maintainability and readability. Furthermore, the target structure description table facilitates communication and collaboration between teams when multiple teams are jointly developing or maintaining large projects.
[0047] For example, Table 1 is a target structure description table of smart locks. As shown in Table 1, the structure name includes setting items and attribute items. The setting items include multiple configuration names, such as sound and automatic locking, and the attribute items include a configuration name, such as lock type. The configuration name named sound corresponds to a configuration offset of 0, a configuration length of 1, a default value of 0x03, a valid range of 0x00-0x03, and the content description indicates that when the default value is 0x00, the lock voice is silent; when the default value is 0x01, the lock voice is low; when the default value is 0x02, the lock voice is medium; when the default value is 0x03, the lock voice is loud. The configuration name "Automatic Lock" corresponds to a configuration offset of 1, a configuration length of 2, a default value of 15, and a valid range of 0 or 5-300. The content description indicates that when the default value is 0, the lock is manually locked; when the default value is 5-300, it means the lock is automatically locked, and the automatic locking delay time is equal to the default value, and the data storage method of the automatic lock is big endian. The configuration name "Lock Type" corresponds to a configuration offset of 0, a configuration length of 1, a default value of 1, and a valid range of 0 or 1. The content description indicates that when the default value is 0, it represents a handle lock; when the default value is 1, it represents a fully automatic lock.
[0048]
[0049] Table 1
[0050] In some embodiments, constructing a structure as a grandchild node for a data structure corresponding to a structure name may include: determining a configuration offset, configuration length, and default value corresponding to each configuration name in the data structure. Assigning a numerical sequence to each configuration name. For each configuration name: using the numerical sequence of the configuration name as the key of the grandchild node, using the configuration offset, configuration length, and default value corresponding to the numerical sequence as the key within the grandchild node, and using the specific value of the configuration offset, the specific value of the configuration length, and the specific value of the default value as the value of the corresponding key within the grandchild node.
[0051] In some embodiments, constructing a structure as a grandchild node for a data structure corresponding to a structure name may include: determining a configuration offset, configuration length, default value, and endian attribute corresponding to each configuration name in the data structure. Assigning a numerical serial number to each configuration name. For each configuration name: using the numerical serial number of the configuration name as the key of the grandchild node, using the configuration offset, configuration length, default value, and endian attribute corresponding to the numerical serial number as the key in the grandchild node, and using the specific value of the configuration offset, the specific value of the configuration length, the specific value of the default value, and the specific value of the endian attribute as the value of the corresponding key in the grandchild node.
[0052] In some embodiments, constructing a structure including a grandchild node as a child node for a structure name may include assigning a numerical serial number to each structure name. For each structure name, the numerical serial number of the structure name is used as the key of the child node, and the grandchild node corresponding to the structure name is used as the value in the child node.
[0053] In some embodiments, a JSON format configuration file contains two levels of nodes: child nodes and grandchild nodes. All structure names are named with numerical serial numbers and sorted by numerical serial numbers. At the same time, for each structure name, the configuration name included in the structure name is named with numerical serial numbers and sorted by numerical serial numbers. Among them, the structure name is the key of the child node, and the configuration name is the key of the grandchild node. That is, the keys of all child nodes and grandchild nodes are named with numerical serial numbers and sorted by numerical serial numbers. Each child node represents an independent structure, and the grandchild node represents the specific variable in the structure of the child node. The grandchild node contains three variables, namely: configuration offset (offset), configuration length (Len), and default value (Data). Optionally, the grandchild node can also contain a variable as an endianness attribute (Endianness). For example, when the endian attribute is described in the content description of the target structure description table, when the target structure description table is converted into a JSON format configuration file, the endian attribute is added to the structure of the configuration name corresponding to the content description.
[0054] Exemplarily, the target structure description table of the smart lock defined in Table 1 is converted to construct the JSON format configuration file of the smart lock shown in Table 2. In order to facilitate the understanding of the JSON format configuration file, the node attribute description is made for each line of code on the right side of the JSON format configuration file. As shown in Table 2, for the structure name of the setting item in the lock, a structure with a child node key value of 0 is constructed. The structure of the child node with key 0 includes a grandchild node with key 0 and a grandchild node with key 1. Among them, the grandchild node with key 0 is used to describe the configuration offset, configuration length and default value corresponding to the configuration name of sound. The grandchild node with key 1 is used to describe the configuration offset, configuration length, default value and byte order attribute corresponding to the configuration name of automatic locking. That is, for each structure name, a grandchild node is constructed for the data structure corresponding to the structure name, and the grandchild node is used as the value of the child node corresponding to the structure name.
[0055]
[0056] Table 2
[0057] Step S102: Create corresponding frame structures for the objects in the JSON format configuration file.
[0058] The frame structure is a sequence in which the key-value pairs included in the object are arranged in a preset order and a preset frame header is added.
[0059] In some embodiments, the object is a child node corresponding to the structure name in a JSON format configuration file; creating a corresponding frame structure for the object in the JSON format configuration file includes: obtaining the child node key-value pairs corresponding to the object and the grandchild node key-value pairs included in the child node; arranging the child node key-value pairs and the grandchild node key-value pairs in a predetermined order to obtain a frame sequence; wherein, the frame sequence with a preset frame header and the total length of the sub-items added is the frame structure corresponding to the object.
[0060] In the above embodiment, the preset frame header may include two bytes, for example, 0x55 and 0xAA. It is worth noting that the preset frame header can be set based on the engineer's experience and is not limited here. This allows the electronic device to easily identify the frame structure corresponding to different objects, thereby facilitating subsequent parsing of the target conversion file.
[0061] In the above embodiment, the frame structure corresponding to a single object may include: a frame header, a structure name, a total length of sub-items, and various variable configurations. A single variable configuration includes: the variable's configuration offset, the variable's configuration length, and the variable's value. It is understood that the variable's configuration offset refers to the configuration offset corresponding to the aforementioned configuration name, the variable's configuration length refers to the configuration length corresponding to the aforementioned configuration name, and the variable's value refers to the default value corresponding to the aforementioned configuration name. The total length of the sub-items is the sum of the configuration lengths of the various variable configurations in a single object.
[0062] It is worth noting that a preset frame header can be added to the beginning of the frame sequence. The total length of the sub-item can be added between the structure name and the variable configuration.
[0063] For example, Table 3 is a schematic diagram of the frame structure corresponding to a structure named "settings." As shown in Table 3, the frame structure includes, in sequence, a 2-byte frame header, with the header being 0x55 and 0xAA. A 1-byte structure name, with the setting item represented by the numeric sequence number 0. A 1-byte total sub-item length, such as Len0. A 1-byte offset for variable 1, which is the sound configuration offset. A 1-byte length for variable 1, which is the sound configuration length. A 1-byte value for variable 1, which is the default sound value. A 1-byte offset for variable 2, which is the auto-lock configuration offset. A 1-byte length for variable 2, which is the auto-lock configuration length. A 1-byte value for variable 2, which is the auto-lock default value. The total length of the sub-item Len0 is equal to 1+1+Len1+1+1+Len2.
[0064]
[0065] Table 3
[0066] Step S103: forming each frame structure into a target conversion file.
[0067] In some embodiments, step S103 may include: adding file header information to a preset buffer, the file header information including data length and a first CRC check value; serializing each frame structure to the buffer to obtain a frame to be processed; calculating a second CRC check value of the frame to be processed; adding the second CRC check value to the end of the frame to be processed to obtain a target conversion file.
[0068] In the above embodiment, the frame structure includes an endian attribute. Serializing each frame structure into a buffer to obtain a frame to be processed includes: if the endian attribute corresponding to the frame structure is big-endian, adjusting the data item corresponding to the frame structure to be stored in big-endian order.
[0069] It can be understood that adjusting the data items from little-endian byte order to big-endian byte order storage is only applicable to multi-byte data. In the frame structure constructed using the configuration file conversion method of the present application, the default value in the frame structure constructed for the structure name is multi-byte data. Therefore, adjusting the data items corresponding to the frame structure to big-endian byte order storage can be to adjust the storage order of the default values in the memory.
[0070] Table 4 shows a schematic diagram of the target conversion file. As shown in Table 4, in order from top to bottom and left to right, the target conversion file begins with the data length (specifically 18), followed by the first CRC checksum. This is followed by the frame headers 0x55 and 0xAA corresponding to the structure name, the structure name with the numerical sequence number 0, the total length of the subitem (specifically 7), the offset of variable 1 (specifically 0), the length of variable 1 (specifically 1), the value of variable 1 (specifically 0x01), the offset of variable 2 (specifically 1), the length of variable 2 (specifically 2), and the value of variable 2 (specifically 0x0F). This is followed by the frame headers 0x55 and 0xAA corresponding to the next structure name, the structure name with the numerical sequence number 1, the total length of the subitem (specifically 7), the offset of variable 1 (specifically 0), the length of variable 1 (specifically 1), and the value of variable 1 (specifically 0x01). Finally, the second CRC checksum is added. Among them, the variables between one frame header and the next frame header are different configuration names corresponding to the structure names described after the previous frame header.
[0071]
[0072] Table 4
[0073] In some embodiments, after forming each frame structure into a target conversion file, the method further includes: writing the target conversion file into a preset computer device.
[0074] It is understandable that the computer device can be an electronic device that needs to store configuration files, such as a smart lock, a smart alarm clock, a smart camera, etc.
[0075] Example 2
[0076] The present application provides a configuration file processing method. Figure 2 As shown, Figure 2 The following is a basic flow chart of the configuration file processing method provided in the embodiment of the present application, including:
[0077] Step S201: receiving a target conversion file.
[0078] The target conversion file is obtained by the method described in the first embodiment.
[0079] In some embodiments, receiving the target conversion file includes: the computer device initializes a reading process and writes the target conversion file into a preset storage space.
[0080] The computer device initialization reading process may include setting a file pointer to a starting position of a target conversion file and preparing a data buffer.
[0081] Step S202: parsing the target conversion file to obtain the structure name and frame length corresponding to each frame structure.
[0082] In some embodiments, before parsing the target conversion file, the method further includes: checking whether the length of the target conversion file is correct; and parsing the target conversion file if the length is correct.
[0083] In an optional manner in the above embodiment, verifying whether the length of the target conversion file is correct may include: reading the length of the to-be-processed frame in the target conversion file; comparing the length with a preset length to determine whether the length is the same; if they are the same, determining that the length of the target conversion file is correct; otherwise, determining that the length of the target conversion file is incorrect.
[0084] In another optional manner in the above embodiment, verifying whether the length of the target conversion file is correct may include: obtaining the data length and a first CRC check value; reading the frame to be processed according to the data length to obtain the frame to be verified; calculating a third CRC check value for the frame to be verified; comparing the third CRC check value with the first CRC check value; if the third CRC check value is the same as the first CRC check value, determining that the length of the target conversion file is correct; otherwise, determining that the length of the target conversion file is incorrect.
[0085] In some embodiments, before parsing the target conversion file, the method further includes: verifying whether the data of the target conversion file is correct; and parsing the target conversion file if the data is correct.
[0086] In an optional manner in the above embodiment, verifying whether the data of the target conversion file is correct may include calculating a fourth CRC check value of the to-be-processed frame in the received target conversion file; comparing the fourth CRC check value with a second CRC check value; and determining that the data of the target conversion file is correct if the fourth CRC check value and the second CRC check value are the same; otherwise, determining that the data of the target conversion file is incorrect.
[0087] In some embodiments, before parsing the target conversion file, the method further includes: checking whether the length of the target conversion file is correct; checking whether the data of the target conversion file is correct; and parsing the target conversion file if the length and the data are correct.
[0088] The specific methods of how to verify whether the length of the target conversion file is correct and how to verify whether the data of the target conversion file is correct are described in the above embodiments and will not be repeated here.
[0089] Step S203: storing the data structure corresponding to the structure name into a preset memory location according to the frame length and the structure name.
[0090] For example, according to the frame length and the structure name, the data structure corresponding to the structure name is stored in a preset memory location, which can refer to the following C language program, for example: uint8_t curAddr = 0;
[0091] uint8_t dataLen = pInBuf[curAddr++];
[0092] while (curAddr <dataLen)
[0093] {
[0094] uint8_t memberOffset = pInBuf[curAddr++];
[0095] uint8_t memberLen = pInBuf[curAddr++];
[0096] if ((memberOffset + memberLen)<= dstLen)
[0097] {
[0098] memcpy(&pOutData[memberOffset],&pInBuf[curAddr], memberLen);
[0099] }
[0100] curAddr += memberLen;
[0101] }
[0102] In this C language program, this code is used to describe the process of parsing data from an input buffer (pInBuf) and copying this data to another output buffer (pOutData).
[0103] Among them, uint8_t curAddr = 0 defines an unsigned 8-bit integer variable curAddr and initializes it to 0. This variable is used to track the current position in pInBuf. uint8_t dataLen defines an unsigned 8-bit integer variable dataLen, which is used to store the data read from pInBuf. uint8_t dataLen = pInBuf[curAddr++], that is, read a byte of data from the current position of pInBuf as the value of dataLen and increment the value of curAddr. while (curAddr<dataLen) {}: This is a loop that executes the code inside the loop when curAddr is less than dataLen. uint8_t memberOffset = pInBuf[curAddr++], that is, read a byte from the current position of pInBuf as the configuration offset (memberOffset) and increment the value of curAddr. uint8_t memberLen= pInBuf[curAddr++], that is, read a byte from the current position of pInBuf as the configuration length (memberLen) and increment the value of curAddr again. if ((memberOffset + memberLen)<= dstLen) {...}, that is, here it is judged whether the sum of memberOffset + memberLen is less than dstLen. If it is less, then use the memcpy function to copy memberLen bytes of data from the current position of pInBuf to the memberOffset position of pOutData. curAddr += memberLen, that is, update curAddr to skip the already copied memberLen bytes of data. Among them, it can be understood that, as shown in Table 4, the data part between the structure type in the input buffer (pInBuf) and the next 0x55 in the frame structure is corresponding. The output buffer (pOutData) corresponds to the memory address of the structure actually used in the embedded system. dataLen represents the total length of the sub-items Len0. memberOffset is the configuration offset, corresponding to the key value of the grandchild node "0ffset" in the JSON structure, and memberLen is the configuration length, corresponding to the key value of the grandchild node "Len" in the JSON structure. dstLen is the preset length of the target structure, and the data copy cannot exceed this length, otherwise there will be data out-of-bounds. By setting dstLen, a layer of protection is made to prevent data anomalies caused by the frame structure length exceeding the length of the target structure.
[0104] Embodiment III
[0105] Combine Figure 3 As shown, an embodiment of the present application provides a conversion device, comprising a processor 1 and a memory 2. Optionally, the device may further comprise a communication interface 3 and a bus 4. The processor 1, the communication interface 3, and the memory 2 may communicate with each other via the bus 4. The communication interface 3 may be used for information transmission. The processor 1 may invoke logic instructions in the memory 2 to execute the configuration file conversion method or configuration file processing method of the above-described embodiment.
[0106] In addition, the logic instructions in the memory 2 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product.
[0107] Memory 2, as a computer-readable storage medium, can be used to store software programs and computer-executable programs, such as program instructions / modules corresponding to the methods in the embodiments of the present application. Processor 1 executes the program instructions / modules stored in memory 2 to execute functional applications and data processing, thereby implementing the configuration file conversion method or configuration file processing method in the above-mentioned embodiments.
[0108] Memory 2 may include a program storage area and a data storage area. The program storage area may store an operating system and at least one application required for a function; the data storage area may store data generated based on the use of the terminal device. Memory 2 may also include high-speed random access memory and non-volatile memory.
[0109] The electronic device executing the configuration file conversion method may be a computer or a server, etc. The electronic device executing the configuration file processing method may be a smart lock, a smart alarm clock, a smart camera, etc., which require configuration files.
[0110] An embodiment of the present application provides a storage medium storing computer-executable instructions, wherein the computer-executable instructions are configured to execute the above-mentioned configuration file conversion method or configuration file processing method.
[0111] An embodiment of the present application provides a computer program product, which includes a computer program stored on a storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the above-mentioned configuration file conversion method or configuration file processing method.
[0112] The aforementioned computer-readable storage medium may be a transient computer-readable storage medium or a non-transitory computer-readable storage medium.
[0113] The technical solutions of the embodiments of the present application can be embodied in the form of a software product, which is stored in a storage medium and includes one or more instructions for causing a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in the embodiments of the present application. The aforementioned storage medium can be a non-transitory storage medium, including: a USB flash drive, a mobile hard disk, a read-only memory, a random access memory, a magnetic disk, an optical disk, and other media that can store program code, or a transient storage medium.
[0114] In the embodiments provided herein, it should be understood that the disclosed devices and methods may be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of the units is merely a logical functional division, and actual implementation may employ other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not implemented.
[0115] The above description is merely an embodiment of the present application and is not intended to limit the scope of protection of the present application. For those skilled in the art, various modifications and variations of the present application are possible. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application. Furthermore, the above embodiments may be combined with each other to form new embodiments, unless they conflict.
Claims
1. A configuration file conversion method, characterized in that: The method comprises: Get the JSON format configuration file to be converted; Creating corresponding frame structures for the objects in the JSON format configuration file; the frame structures are key-value pairs included in the objects arranged in a preset order and having a sequence of preset frame headers added thereto; forming each of the frame structures into a target conversion file; The object is a child node corresponding to the structure name in the JSON format configuration file; creating a corresponding frame structure for the object in the JSON format configuration file includes: Obtain the child node key-value pairs corresponding to the object and the grandchild node key-value pairs included in the child node; Arranging the child node key-value pairs and the grandchild node key-value pairs in a predetermined order to obtain a frame sequence; obtaining only the child node key-value pairs and the grandchild node key-value pairs and constructing them into a frame sequence, so that the frame sequence does not contain key names, quotation marks, commas, and line breaks; The frame sequence with the preset frame header and the total length of the sub-items added is the frame structure corresponding to the object.
2. The method according to claim 1, characterized in that The JSON format configuration file is obtained in the following way: Obtain a preset target structure description table; the target structure description table includes a structure name and a data structure corresponding to the structure name; the data structure includes: a configuration name, a configuration offset, a configuration length, and a default value; For each of the structure names: Constructing a structure for the data structure corresponding to the structure name as a grandchild node; Constructing a structure including the grandchild node as a child node for the structure name; The sub-node is an object in the JSON format configuration file.
3. The method according to claim 1, characterized in that Forming each of the frame structures into a target conversion file includes: Adding file header information in a preset buffer, wherein the file header information includes data length and a first CRC check value; Serializing each of the frame structures into the buffer to obtain frames to be processed; Calculating a second CRC check value of the frame to be processed; The second CRC check value is added to the end of the frame to be processed to obtain a target conversion file.
4. The method according to claim 3, characterized in that The frame structure includes: a byte order attribute; serializing each frame structure into the buffer to obtain a frame to be processed, including: In a case where the byte order attribute corresponding to the frame structure is big-endian byte order, the data items corresponding to the frame structure are adjusted to be stored in big-endian byte order.
5. The method according to any one of claims 1 to 4, characterized in that After forming each of the frame structures into a target conversion file, the method further includes: The target conversion file is written into a preset electronic device.
6. The method according to claim 5, characterized in that The electronic device is a smart lock; the configuration content of the JSON format configuration file includes the lock voice volume, the lock automatic locking time, and the lock type.
7. A configuration file processing method, characterized in that: The method comprises: Receiving the target conversion file; the target conversion file is obtained according to the method according to any one of claims 1 to 4; Parsing the target conversion file to obtain the structure name and frame length corresponding to each frame structure; According to the frame length and the structure name, the data structure corresponding to the structure name is stored in a preset memory location.
8. The method according to claim 7, characterized in that Before parsing the target conversion file, the method further includes: Verify whether the length of the target conversion file is correct; When the length is correct, the target conversion file is parsed.
9. The method according to claim 7 or 8, characterized in that Before parsing the target conversion file, the method further includes: Verifying whether the data of the target conversion file is correct; When the data is correct, the target conversion file is parsed.
10. An electronic device, characterized in that: The invention comprises a processor and a memory, wherein the memory stores computer-executable instructions that can be executed by the processor, and the processor executes the computer-executable instructions to implement the configuration file conversion method according to any one of claims 1 to 6, or implements the configuration file processing method according to any one of claims 7 to 9.
11. A storage medium, characterized in that: The storage medium stores computer-executable instructions. When the computer-executable instructions are called and executed by the processor, the computer-executable instructions enable the processor to implement the configuration file conversion method described in any one of claims 1 to 6, or the configuration file processing method described in any one of claims 7 to 9.
Citation Information
Patent Citations
Data transmission method and device based on JSON configuration file
CN110971704A
Intelligent fire-fighting remote monitoring method and system and safety assessment method
CN112215452A