CN112114793B - Code generation method and system for converting general JSON object into C/C++ structured object - Google Patents

Code generation method and system for converting general JSON object into C/C++ structured object Download PDF

Info

Publication number
CN112114793B
CN112114793B CN202010961237.1A CN202010961237A CN112114793B CN 112114793 B CN112114793 B CN 112114793B CN 202010961237 A CN202010961237 A CN 202010961237A CN 112114793 B CN112114793 B CN 112114793B
Authority
CN
China
Prior art keywords
node
json
array
variable
value
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010961237.1A
Other languages
Chinese (zh)
Other versions
CN112114793A (en
Inventor
王军利
张利武
王振杰
田华臣
李宁安
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
709th Research Institute of CSIC
Original Assignee
709th Research Institute of CSIC
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 709th Research Institute of CSIC filed Critical 709th Research Institute of CSIC
Priority to CN202010961237.1A priority Critical patent/CN112114793B/en
Publication of CN112114793A publication Critical patent/CN112114793A/en
Application granted granted Critical
Publication of CN112114793B publication Critical patent/CN112114793B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention provides a code generation method for converting a general JSON object into a C/C++ structured object, which comprises the following steps: firstly, scanning a data structure definition of a structured object to obtain a logical structure metadata information tree of the structured object; then, traversing the metadata information tree deeply; then, according to the traversal node type, respectively creating and outputting simple assignment, nested assignment and multi-set cyclic assignment codes according to non-array leaf nodes, non-array non-leaf nodes and array nodes; and finally, merging and outputting the generated codes into a file for development and use. The invention has the main advantages that: the code automatic generation method for converting the general JSON object into the C/C++ structured object based on the tree is provided, the code for converting the JSON object into the C/C++ structured object is automatically generated according to the definition of the data structure, the problem that the JSON object is converted into the C/C++ structured object is solved, manual coding is not needed, and the development efficiency is improved. The invention also provides a code generation system for converting the corresponding general JSON object into the C/C++ structured object.

Description

Code generation method and system for converting general JSON object into C/C++ structured object
Technical Field
The invention belongs to the technical field of software development automation and data exchange, and particularly relates to a code generation method and system for converting a general JSON object into a C/C++ structured object.
Background
With the development of computer software technology and network technology, the demands of distributed applications or cross-application integration are increasing, but due to technical needs or history reasons, each application is often developed by adopting different programming languages and depends on different software environments to run on different hardware platforms, so that data exchange under heterogeneous environments is needed among each application. JSON (JavaScript Object Notation, JS object numbered musical notation) is a lightweight data exchange format, which stores and represents data in a text format completely independent of programming language, has a concise and clear hierarchical structure, is easy to read and write, is easy to parse and generate by a machine, has high network transmission efficiency, and is an ideal data exchange format in heterogeneous environments. C/C++ is used as a flexible and efficient development language and is widely applied in the field of software development. When developing or accessing applications based on JSON format using C/c++, the problem of converting JSON objects into C/c++ internally structured objects must be solved.
The traditional solution is to manually write codes according to the data structure definition of the C/C++ structured object for each JSON object to be converted into the C/C++ structured object, and convert each member of the C/C++ structured object one by one. The processing method firstly needs to carry out customized coding according to different structured objects, and has no universality; secondly, a large amount of conversion coding work needs to be manually carried out, the degree of automation is not high, and the application development efficiency is affected; finally, once the data structure of the structured object is changed, the code conversion modification needs to be manually carried out again, and the software maintenance and reconstruction are very inconvenient.
Disclosure of Invention
Aiming at the problems existing in the prior art, the invention provides a code automatic generation method for converting a general JSON object based on a tree into a C/C++ structured object, thereby realizing automatic code generation.
To achieve the above object, according to one aspect of the present invention, there is provided a code auto-generation method for converting a tree-based general JSON object into a C/c++ structured object, including:
step S1: and reading and analyzing a data structure definition source code file of the C/C++ structured object, generating a logical structure metadata information tree of the structured object, and turning to step S2.
Step S2: creating a root node blank JSON object (the object variable name is jsonObject 1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3.
Step S3: the deep traversal (first order) metadata information tree (the root node traversal sequence number is set to 1) starts from the root node, and the process proceeds to step S4.
Step S4: whether the traversal is ended is judged, and if the traversal is ended, the process proceeds to step S11. If the traversal is not finished, the process proceeds to step S5.
Step S5: acquiring the next traversal node as the current traversal node (the traversal sequence number is added with 1), and turning to step S6;
step S6: and judging whether the current traversal node comprises an array (whether the array subscript of the node is 0), and if not, turning to step S7. If the array is included, the process proceeds to step S10.
Step S7: and judging whether the current traversal node is a leaf node or not, and if so, turning to step S8. If not, the current traversal node is of a complex type, and the step goes to step S9.
Step S8: and constructing an output leaf node conversion code, and proceeding to step S4.
Step S9: the construction output non-leaf node conversion code proceeds to step S4.
Step S10: and constructing and outputting an array member node conversion code, and turning to step S4.
Step S11: and merging and outputting all codes, and ending the code generation.
Further, in the step S8, an output leaf node conversion code is constructed, and the process is as follows:
and acquiring a shortest path from the metadata information tree root node to the current node, splicing the names of all node members on the shortest path by using' and the name of each node member on the shortest path, replacing the name of each member of the root node by the variable name of the C/C++ structured object to be converted, and generating the complete reference name of the current node member as the left variable of the assignment statement. If a node on the shortest path contains an array, when the complete reference name is spliced, adding an array reference part of the node after the name of the node member, wherein Q is the array dimension of the node member, and Y is the traversal sequence number of the node;
the member name of the current node is used as a key, a value corresponding to the key is obtained from a key value pair contained in a JSON object corresponding to a father node of the current node, and the value is converted into a data type of the node member (the type of the leaf node member is a basic data type, such as short, int, long, float, double, string and the like, and the basic data types of different compilers are different) to be used as the right value of an assignment statement;
and constructing an output assignment statement by using the assignment left variable and the assignment right value.
Further, the step S9 constructs and outputs a non-leaf node conversion code, which includes the following procedures:
creating a new JSON object variable (variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node) as an assigned left variable;
the member name of the current node is used as a key, a corresponding value is obtained from a key value pair contained in a JSON object corresponding to a father node of the current node, and the value is converted into a JSON object type and is used as a right assigned value;
constructing an output assignment statement by using the assignment left variable and the assignment right value;
and taking the newly created JSON object variable as a JSON object reference corresponding to the current node.
Further, in the step S10, an output array member node conversion code is constructed, and the process is as follows:
setting an array as N dimensions (N is a positive integer), wherein subscripts are Index1, index2 and Index3 respectively,. IndexN, taking N array subscripts (Index 1, index2 and Index3,) as a circulation control range respectively, and using i1_X, i2_X and i3_X.iN_X as a circulation control variable, wherein X is the traversal sequence number of a current node, constructing a circulation statement (such as for circulation statement), and outputting N layers of nested circulation control codes;
before the layer 1 cyclic body, the member name of the current node is a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the father node of the current node, and the JSON object is converted into the JSON array object which is used as the JSON array object corresponding to the layer 1 cyclic body. If N is not less than 2, before the M (2 < =m < =n) th layer cyclic body, in the M-1 th layer cyclic body, using the control variable of the M-1 th layer cyclic body as an index, obtaining a value corresponding to the index in the JSON array object corresponding to the M-1 th layer cyclic body, and converting the value into the JSON array object as the JSON array object corresponding to the M-th layer cyclic body. The JSON array object variable name corresponding to each layer of circulating body is as 'JSOnARRAyiP_X', wherein P is 1,2,3,..N, X is the traversal sequence number of the current node;
in the N-layer circulation body, if the type of the node member is a simple type, using a control variable of the N-layer circulation body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer circulation body, converting the value into the simple type, assigning the simple type to the member of the current node (using a complete reference name of the node member), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node), a control variable of the loop body of the nth layer is used as an index, a value corresponding to the index in the JSON array object corresponding to the loop body of the nth layer is obtained, the value is converted into the JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And taking the newly created JSON object variable as the JSON object reference corresponding to the current node.
According to another aspect of the present invention, there is also provided a code generation system for converting a general JSON object into a C/c++ structured object, including:
at least one processor; the method comprises the steps of,
a memory communicatively coupled to the at least one processor; wherein,,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the code generation method of converting a generic JSON object into a C/c++ structured object described above.
In general, the above technical solutions conceived by the present invention have the following beneficial effects compared with the prior art:
(1) The metadata information tree is generated according to the data structure definition of the C/C++ structured object, is suitable for any C/C++ structured object, and has good universality;
(2) According to the method, codes for converting the JSON object into the C/C++ structured object are automatically generated according to the metadata information tree, so that manual hard coding is reduced, development workload is reduced, and the degree of automation of software development is improved.
Drawings
FIG. 1 is a flow chart of a method for automatically generating codes by converting a general JSON object based on a tree into a C/C++ structured object in an embodiment of the invention;
FIG. 2 is a schematic diagram of a definition of a data structure of a structured object Person in an embodiment of the present invention;
FIG. 3 is a schematic diagram of a metadata information tree generated after reading and parsing a data structure definition of a Person structured object according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of a traversal sequence number of each node after a deep traversal of the metadata information tree according to an embodiment of the invention;
FIG. 5 is a schematic diagram of the total function code of the JSON object converted to the structured object Person by merging output in an embodiment of the present invention.
Detailed Description
The present invention will be described in further detail with reference to the drawings and examples, in order to make the objects, technical solutions and advantages of the present invention more apparent. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention. In addition, the technical features of the embodiments of the present invention described below may be combined with each other as long as they do not collide with each other.
Aiming at the problems in the prior art, the invention provides a code automatic generation method for converting a general JSON object based on a tree into a C/C++ structured object, which changes the current mode of converting the JSON object into the C/C++ structured object by means of manual coding, generates a metadata information tree according to the structural definition of the C/C++ structured object, further automatically generates a conversion code according to the metadata information tree, does not need manual coding, and improves the automation level and development efficiency of software development; in addition, the method is suitable for any C/C++ structured object and has better universality.
As shown in FIG. 1, the invention provides a code automatic generation method for converting a general JSON object based on a tree into a C/C++ structured object, which comprises the following steps:
step S1: and reading and analyzing a data structure definition source code file of the C/C++ structured object, generating a logical structure metadata information tree of the structured object, and turning to step S2.
Step S2: creating a root node blank JSON object (the object variable name is jsonObject 1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3.
Step S3: the deep traversal (first order) metadata information tree (the root node traversal sequence number is set to 1) starts from the root node, and the process proceeds to step S4.
Step S4: whether the traversal is ended is judged, and if the traversal is ended, the process proceeds to step S11. If the traversal is not finished, the process proceeds to step S5.
Step S5: acquiring the next traversal node as the current traversal node (the traversal sequence number is added with 1), and turning to step S6;
step S6: and judging whether the current traversal node comprises an array (whether the array subscript of the node is 0), and if not, turning to step S7. If the array is included, the process proceeds to step S10.
Step S7: and judging whether the current traversal node is a leaf node or not, and if so, turning to step S8. If not, the current traversal node is of a complex type, and the step goes to step S9.
Step S8: and constructing an output leaf node conversion code, and proceeding to step S4.
Specifically, an output leaf node translation code is constructed, which is as follows:
and acquiring a shortest path from the metadata information tree root node to the current node, splicing the names of all node members on the shortest path by using' and the name of each node member on the shortest path, replacing the name of each member of the root node by the variable name of the C/C++ structured object to be converted, and generating the complete reference name of the current node member as the left variable of the assignment statement. If a node on the shortest path contains an array, when the complete reference name is spliced, adding an array reference part of the node after the name of the node member, wherein Q is the array dimension of the node member, and Y is the traversal sequence number of the node;
the member name of the current node is used as a key, a value corresponding to the key is obtained from a key value pair contained in a JSON object corresponding to a father node of the current node, and the value is converted into a data type of the node member (the type of the leaf node member is a basic data type, such as short, int, long, float, double, string and the like) and is used as a right value of an assignment statement;
and constructing an output assignment statement by using the assignment left variable and the assignment right value.
Step S9: the construction output non-leaf node conversion code proceeds to step S4.
Specifically, the output non-leaf node translation code is constructed as follows:
creating a new JSON object variable (variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node) as an assigned left variable;
the member name of the current node is used as a key, a corresponding value is obtained from a key value pair contained in a JSON object corresponding to a father node of the current node, and the value is converted into a JSON object type and is used as a right assigned value;
constructing an output assignment statement by using the assignment left variable and the assignment right value;
and taking the newly created JSON object variable as a JSON object reference corresponding to the current node.
Step S10: and constructing and outputting an array member node conversion code, and turning to step S4.
Specifically, an output array member node conversion code is constructed, and the process is as follows:
setting an array as N dimensions (N is a positive integer), wherein subscripts are Index1, index2 and Index3 respectively,. IndexN, taking N array subscripts (Index 1, index2 and Index3,) as a circulation control range respectively, and using i1_X, i2_X, i3_X..iN_X as a circulation control variable, wherein N is the array dimension of a current node, X is the traversal sequence number of the current node, constructing a circulation statement (such as for circulation statement), and outputting N layers of nested circulation control codes;
before the layer 1 cyclic body, the member name of the current node is a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the father node of the current node, and the JSON object is converted into the JSON array object which is used as the JSON array object corresponding to the layer 1 cyclic body. If N is not less than 2, before the M (2 < =m < =n) th layer cyclic body, in the M-1 th layer cyclic body, using the control variable of the M-1 th layer cyclic body as an index, obtaining a value corresponding to the index in the JSON array object corresponding to the M-1 th layer cyclic body, and converting the value into the JSON array object as the JSON array object corresponding to the M-th layer cyclic body. The JSON array object variable name corresponding to each layer of circulating body is as 'JSOnARRAyiP_X', wherein P is 1,2,3,..N, X is the traversal sequence number of the current node;
in the N-layer circulation body, if the type of the node member is a simple type, using a control variable of the N-layer circulation body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer circulation body, converting the value into the simple type, assigning the simple type to the member of the current node (using a complete reference name of the node member), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node), a control variable of the loop body of the nth layer is used as an index, a value corresponding to the index in the JSON array object corresponding to the loop body of the nth layer is obtained, the value is converted into the JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And taking the newly created JSON object variable as the JSON object reference corresponding to the current node.
Step S11: and merging and outputting all codes, and ending the code generation.
The method is clearly illustrated below in connection with a specific example, which is not intended to limit the invention. The present example uses QT qjson document object to perform JSON object basic operation (other libraries supporting JSON operation may be used), let the Person object variable to be converted be clobjectvar, and the JSON object variable to be used be JSON objectvar, and the data structure definition of the structured object Person is shown in fig. 2.
The invention provides a code automatic generation method for converting a general JSON object based on a tree into a C/C++ structured object, which comprises the following steps:
step S1: and reading and analyzing a data structure definition source code file of the C/C++ structured object, generating a logical structure metadata information tree of the structured object, and turning to step S2.
The detailed generation process of the C/C++ structured object metadata information tree is as follows:
the application instance tool (or custom compiler) scans the code file, performs lexical and grammatical analysis on the data structure definition of the structured object, and generates a metadata information tree of the C/C++ structured object. The metadata information tree root node represents the data structure definition of the analyzed structured object, and the parent-child relationship among the nodes on the tree represents the nested inclusion relationship among the members of the data structure. Each node (excluding the root node) of the metadata information tree contains information such as names, type names, and sequences of index of groups (non-groups are denoted by 0) of each member of the data structure (including the inner substructure members), wherein the types of the leaf node members are simple types (built-in types of compilers), and the types of the non-leaf node members are complex types (user-defined types). The format of each node (excluding the root node) on the tree is: memberName, typeName, [ Index1, index2,..IndexN ], wherein Member name represents a member name, typeName represents a member type name, index1, index2 in brackets,..IndexN represents a member array subscript.
The metadata information tree is generated after the data structure definition of the Person structured object is read and analyzed by adopting the step S1, and is shown in fig. 3.
Step S2: creating a root node blank JSON object (the object variable name is jsonObject 1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3. For example, the statement that creates a JSON object corresponding to the root node and assigns a value is "QJsonObject jsonObject 1=jsonobjectvar".
Step S3: and (4) deeply traversing the metadata information tree from the root node, setting the node traversing sequence number as 1, and turning to step S4.
Step S4: whether the traversal is ended is judged, and if the traversal is ended, the process proceeds to step S11. If the traversal is not finished, the process proceeds to step S5.
Step S5: acquiring the next traversal node as the current traversal node, adding 1 to the traversal sequence number, and turning to step S6;
step S6: and judging whether the current traversal node comprises an array (whether the array subscript of the node is 0), and if not, turning to step S7. If the array is included, the process proceeds to step S10.
Step S7: and judging whether the current traversal node is a leaf node or not, and if so, turning to step S8. If not, the current traversal node is of a complex type, and the step goes to step S9.
Step S8: and constructing an output leaf node conversion code, and proceeding to step S4.
Step S8.1: and acquiring a shortest path from the metadata information tree root node to the current node, splicing the names of all node members on the shortest path by using' and the name of each node member on the shortest path, replacing the name of each member of the root node by the variable name of the C/C++ structured object to be converted, and generating the complete reference name of the current node member as the left variable of the assignment statement. If a node on the shortest path contains an array, when the complete reference name is spliced, the array reference part of the node is added after the name of the node member, which is in the form of "[ i1_Y ] [ i2_Y ] … [ iQ_Y ]", wherein Q is the array dimension of the node member, and Y is the traversal sequence number of the node. For example, the complete reference name of the leaf node area in the metadata information tree of the structured object Person is "cObjectVar.
Step S8.2: and acquiring a value corresponding to the key from a key value pair contained in a JSON object corresponding to a father node of the current node by using the name of the member of the current node as a key, and converting the value into a data type of the node member (the type of the leaf node member is a basic data type, such as short, int, long, float, double, string and the like) as a right value of an assignment statement. For example, the right value of the leaf node area corresponding structure is "jsonObject8.Value (" area "). ToDouble (); ".
Step S8.3: and constructing an output assignment statement by using the assignment left variable and the assignment right value. For example, the assignment statement code of the leaf node area corresponding construction is "cObjectVar. Houses [ i1_8]. Area=jsonObject8. Value (" area "). Tosouble (); ".
Step S9: the construction output non-leaf node conversion code proceeds to step S4.
Step S9.1: a new JSON object variable (variable name like "jsonObjectX", where X is the traversal sequence number of the current node) is created as the assigned left variable. For example, the JSON object variable code of the non-leaf node birthday in the metadata information tree is created to be "QJsonObject jsonObject2".
Step S9.2: and obtaining a corresponding value from a key value pair contained in the JSON object corresponding to the father node of the current node by using the member name of the current node as a key, and converting the value into the JSON object type to be used as a right assigned value. For example, the assigned right value code of the non-leaf node birthday in the metadata information tree is "jsonObject1.Value (" birthday "). ToObject (); "
Step S9.3: and constructing an output assignment statement by using the assignment left variable and the assignment right value. For example, the corresponding assignment code of the non-leaf node birthday in the metadata information tree is "QJsonObject jsonObject 2=jsonobject1.value (" birthday "). ToObject (); "
Step S9.4: and taking the newly created JSON object variable as a JSON object reference corresponding to the current node. For example, the corresponding JSON object of the non-leaf node birthday in the metadata information tree is "jsonObject2".
Step S10: and constructing and outputting an array member node conversion code, and turning to step S4.
Step S10.1: let the array be N-dimensional (N is a positive integer), the subscripts are Index1, index2, index3, respectively,. Index N, take the N-number array subscripts (Index 1, index2, index3,. Index N) as the loop control ranges, use i1_x, i2_x, i3_x..in_x as the loop control variable (X is the traversal sequence number of the current node), construct a loop sentence (e.g., for loop sentence), output N-layer nested loop control code. For example, the holes node of the metadata information tree is a 1-dimensional array, and the corresponding loop control statement is "for (int i1_8=0; i1_8<2; i1_8++).
Step S10.2: before the layer 1 cyclic body, the member name of the current node is a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the father node of the current node, and the JSON object is converted into the JSON array object which is used as the JSON array object corresponding to the layer 1 cyclic body. If N is not less than 2, before the M (2 < =m < =n) th layer cyclic body, in the M-1 th layer cyclic body, using the control variable of the M-1 th layer cyclic body as an index, obtaining a value corresponding to the index in the JSON array object corresponding to the M-1 th layer cyclic body, and converting the value into the JSON array object as the JSON array object corresponding to the M-th layer cyclic body. The JSON array object variable name corresponding to each layer of cyclic body is as "jsonarrayip_x", wherein P is 1,2,3,..n, X is the traversal sequence number of the current node. For example, the holes node of the metadata information tree is a 1-weft array node, and the corresponding JSON array object code statement is "QJsonArray jsonArrayi1 —8=jsonobject1.value (" holes "). Toparay (); "
Step S10.3: in the N-layer circulation body, if the type of the node member is a simple type, using a control variable of the N-layer circulation body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer circulation body, converting the value into the simple type, assigning the simple type to the member of the current node (using a complete reference name of the node member), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node), a control variable of the loop body of the nth layer is used as an index, a value corresponding to the index in the JSON array object corresponding to the loop body of the nth layer is obtained, the value is converted into the JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And taking the newly created JSON object variable as the JSON object reference corresponding to the current node. For example, the assignment code corresponding to the holes node of the metadata information tree is "QJsonObject jsonObject8 =jsonarrayi1_8.at (i1_8). ToObject (); ".
Step S11: and merging and outputting all codes, and ending the code generation. For example, the deep traversal of the metadata information tree of the structured object Person is completed, the traversal sequence number of each node is shown as the number in the bracket below each node in fig. 4, and the code for converting the JSON object into the structured object Person is generated as shown in fig. 5.
Further, from the software implementation level, the invention also provides a code automatic generation software system for realizing the conversion of the tree-based general JSON object into the C/C++ structured object, and specifically, the system comprises:
1. structured object metadata information tree generation module
The tool reads the data structure definition file and generates a corresponding metadata information tree through lexical and grammatical analysis. The metadata information tree of all the structured objects in the application is generated by using the tool and is used by a code generation module.
2. Code generation module for converting JSON object into structured object
And traversing the metadata information tree corresponding to the target structured object deeply according to the conversion requirement, and generating codes for converting the JSON object into the structured object.
3. Code saving module
The generated function code is saved to a file for use.
Further, the invention also provides a code generation system for converting the general JSON object into the C/C++ structured object, which comprises the following steps:
at least one processor; the method comprises the steps of,
a memory communicatively coupled to the at least one processor; wherein,,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform a code generation method of converting the generic JSON object into a C/c++ structured object as described above.
It will be readily appreciated by those skilled in the art that the foregoing description is merely a preferred embodiment of the invention and is not intended to limit the invention, but any modifications, equivalents, improvements or alternatives falling within the spirit and principles of the invention are intended to be included within the scope of the invention.

Claims (7)

1. A code generation method for converting a universal JSON object into a C/c++ structured object, comprising:
step S1: reading and analyzing a data structure definition source code file of the C/C++ structured object, generating a structured object logic structure metadata information tree, and turning to step S2;
step S2: creating a root node blank JSON object, assigning the JSON object to be converted to the newly created root node JSON object, and transferring to S3;
step S3: deeply traversing the metadata information tree from the root node, setting the node traversing sequence number as 1, and turning to step S4;
step S4: judging whether the traversal is finished, if so, turning to step S11; if the traversal is not finished, turning to step S5;
step S5: acquiring the next traversal node as the current traversal node, adding 1 to the traversal sequence number, and turning to step S6;
step S6: judging whether the current traversal node comprises an array or not, if not, turning to step S7; if the array is included, the step S10 is carried out;
step S7: judging whether the current traversing node is a leaf node or not, if so, turning to step S8; if the node is not a leaf node, the current traversal node is of a complex type, and the step S9 is carried out;
step S8: constructing an output leaf node conversion code, and turning to step S4; the step S8 of constructing an output leaf node conversion code includes: acquiring a shortest path from a metadata information tree root node to a current node, splicing the names of all node members on the shortest path by using 'three', replacing the member names of the root node by the variable names of the C/C++ structured objects to be converted, and generating a complete reference name of the current node member as a left variable of an assignment statement; the member name of the current node is used as a key, a value corresponding to the key is obtained from a key value pair contained in a JSON object corresponding to the father node of the current node, and the value is converted into the data type of the node member and is used as the right value of an assignment statement; constructing an output assignment statement according to the assignment left variable and the assignment right value;
step S9: constructing and outputting a non-leaf node conversion code, and turning to step S4; the step S9 of constructing and outputting a non-leaf node conversion code includes: creating a new JSON object variable which is used as an assignment left variable, wherein the name of the newly created JSON object variable is like "jsonObjectX", and X is the traversal sequence number of the current node; the member name of the current node is used as a key, a corresponding value is obtained from a key value pair contained in a JSON object corresponding to a father node of the current node, and the value is converted into a JSON object type and is used as a right assigned value; constructing an output assignment statement by using the assignment left variable and the assignment right value; taking the newly created JSON object variable as a JSON object reference corresponding to the current node;
step S10: constructing and outputting an array member node conversion code, and turning to step S4; the step S10 of constructing an output array member node conversion code includes: setting an array as N dimensions, wherein N is a positive integer, subscripts are Index1, index2 and Index3 respectively,. IndexN, using N array subscripts as a circulation control range respectively, using i1_X, i2_X, i3_X and iN_X as circulation control variables, and X is a traversal sequence number of a current node, constructing a circulation statement and outputting N layers of nested circulation control codes; before the layer 1 cyclic body, acquiring a value corresponding to a key from a JSON object corresponding to a father node of the current node by using the member name of the current node as a key, and converting the value into a JSON array object which is used as the JSON array object corresponding to the layer 1 cyclic body; in addition, if N is not less than 2, in the M-1 layer cycle body, using a control variable of the M-1 layer cycle body as an index in the M-1 layer cycle body, obtaining a value corresponding to the index in the JSON array object corresponding to the M-1 layer cycle body, and converting the value into the JSON array object as the JSON array object corresponding to the M layer cycle body, wherein 2< = M < = N, and the variable name of the JSON array object corresponding to each layer cycle body is "jsonarrayip_x", wherein P is 1,2, 3. In the N-layer circulation body, if the type of the node member is a simple type, using a control variable of the N-layer circulation body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer circulation body, converting the value into the simple type, assigning the simple type to the member of the current node, and outputting an assignment statement; if the node type is a complex type, a new JSON object variable is created, the variable name is "jsonObjectX", wherein X is the traversal sequence number of the current node, a control variable of an N-th layer loop body is used as an index, a value corresponding to the index in the JSON array object corresponding to the N-th layer loop body is obtained, the value is converted into the JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output; and taking the newly created JSON object variable as a JSON object reference corresponding to the current node;
step S11: and merging and outputting all codes, and ending the code generation.
2. The code generation method of claim 1, wherein if a node on the shortest path contains an array, the node's array reference part is added after the node member name when the full reference name is spliced, in the form of "[ i1_y ] [ i2_y ] … [ iq_y ]", where Q is the array dimension of the node member and Y is the traversal sequence number of the node.
3. A code generation method for converting a generic JSON object into a C/c++ structured object as claimed in claim 1 or 2, wherein the data type of the leaf node member is a basic data type.
4. The code generation method for converting a generic JSON object into a C/c++ structured object according to claim 1, wherein the loop sentence is a for loop sentence, or a while loop sentence, or a do while loop sentence, or a for ach loop sentence, or a do until loop sentence.
5. The code generation method of converting a generic JSON object into a C/c++ structured object as set forth in claim 1, wherein the metadata information tree root node represents a data structure definition of the structured object analyzed, and parent-child relationships between nodes on the tree represent nested containment relationships between members of the data structure; each node of the metadata information tree comprises a name, a type name and a plurality of subscript sequences of each member of the data structure, wherein the types of the members of the leaf nodes are simple types, the types of the members of the non-leaf nodes are complex types, each node of the metadata information tree does not comprise a root node, each member of the data structure comprises an internal substructure member, the simple types are built-in types of a compiler, and the complex types are user-defined types.
6. The code generation method of converting a generic JSON object to a C/c++ structured object as recited in claim 5, wherein each node on the tree has a format of MemberName, typeName, [ Index1, index2, ], index, wherein MemberName represents a member name, typeName represents a member type name, index1, index2 in brackets, ], index represents a member array Index.
7. A code generation system for converting a generic JSON object into a C/c++ structured object, comprising:
at least one processor; the method comprises the steps of,
a memory communicatively coupled to the at least one processor; wherein,,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the code generation method of converting a generic JSON object of any one of claims 1-6 into a C/c++ structured object.
CN202010961237.1A 2020-09-14 2020-09-14 Code generation method and system for converting general JSON object into C/C++ structured object Active CN112114793B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010961237.1A CN112114793B (en) 2020-09-14 2020-09-14 Code generation method and system for converting general JSON object into C/C++ structured object

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010961237.1A CN112114793B (en) 2020-09-14 2020-09-14 Code generation method and system for converting general JSON object into C/C++ structured object

Publications (2)

Publication Number Publication Date
CN112114793A CN112114793A (en) 2020-12-22
CN112114793B true CN112114793B (en) 2023-09-22

Family

ID=73802640

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010961237.1A Active CN112114793B (en) 2020-09-14 2020-09-14 Code generation method and system for converting general JSON object into C/C++ structured object

Country Status (1)

Country Link
CN (1) CN112114793B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106940654A (en) * 2017-02-15 2017-07-11 南京航空航天大学 The automatic detection and localization method of EMS memory error in source code
CN107562763A (en) * 2016-07-01 2018-01-09 阿里巴巴集团控股有限公司 The display methods and device of data variation
CN108038796A (en) * 2017-12-11 2018-05-15 厦门亿力吉奥信息科技有限公司 GIS service operation method, storage medium based on C++
CN110187885A (en) * 2019-06-10 2019-08-30 合肥本源量子计算科技有限责任公司 A kind of the intermediate code generation method and device of the compiling of quantum program

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103645931B (en) * 2013-12-25 2016-06-22 盛杰 The method of code conversion and device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107562763A (en) * 2016-07-01 2018-01-09 阿里巴巴集团控股有限公司 The display methods and device of data variation
CN106940654A (en) * 2017-02-15 2017-07-11 南京航空航天大学 The automatic detection and localization method of EMS memory error in source code
CN108038796A (en) * 2017-12-11 2018-05-15 厦门亿力吉奥信息科技有限公司 GIS service operation method, storage medium based on C++
CN110187885A (en) * 2019-06-10 2019-08-30 合肥本源量子计算科技有限责任公司 A kind of the intermediate code generation method and device of the compiling of quantum program

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
JSON与XML动态转换研究及应用;刘立;赵建军;张锦鹏;余进;;软件导刊(第11期);全文 *
一种基于前缀编码的树生成算法;况立群;熊风光;韩燮;;小型微型计算机系统(第05期);全文 *

Also Published As

Publication number Publication date
CN112114793A (en) 2020-12-22

Similar Documents

Publication Publication Date Title
US9753709B2 (en) Cobol to bytecode translation
CN111736840B (en) Method for compiling applet application, method for running applet application, storage medium and electronic device
CN112114792B (en) Code generation method and system for converting general C/C++ structured object into JSON object
CN101464799A (en) MPI parallel programming system based on visual modeling and automatic skeleton code generation method
EP2330504A1 (en) Lambda expressions
CN113722218B (en) Software defect prediction model construction method based on compiler intermediate representation
CN112379917A (en) Browser compatibility improving method, device, equipment and storage medium
CN115796190B (en) Front-end internationalization multi-language conversion method and system based on vue and weback
CN114780100B (en) Compiling method, electronic device and storage medium
CN110866028A (en) SQL instruction generation method and system
CN115202626A (en) Low-code front-end development method supporting multi-technology stack components
CN117971236B (en) Operator analysis method, device, equipment and medium based on lexical and grammatical analysis
Gontrum et al. Alto: Rapid prototyping for parsing and translation
WO2014040766A1 (en) Computer-implemented method for computer program translation
CN107203406B (en) Processing method for distributed storage structure
EP3996009A1 (en) Method and apparatus of converting schema in deep learning freamwork, and computer storage medium
CN112114793B (en) Code generation method and system for converting general JSON object into C/C++ structured object
Wilson et al. An overview of the SUIF compiler system
CN117289938A (en) Intelligent auxiliary system for software development
CN115700492A (en) Program semantic representation learning and prediction method based on inter-graph neural network
CN112860233B (en) Method for generating target grammar tree and related equipment
CN115357251A (en) Method for mobile terminal iOS to develop data conversion model
CN108549531A (en) Complex type data automatic generation method, device, electronic equipment, storage medium
CN114116779A (en) Deep learning-based power grid regulation and control field information retrieval method, system and medium
CN114444032A (en) Method and device for self-defining rule confusion code

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant