CN112114793A - 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
CN112114793A
CN112114793A CN202010961237.1A CN202010961237A CN112114793A CN 112114793 A CN112114793 A CN 112114793A CN 202010961237 A CN202010961237 A CN 202010961237A CN 112114793 A CN112114793 A CN 112114793A
Authority
CN
China
Prior art keywords
node
json
array
json object
converting
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.)
Granted
Application number
CN202010961237.1A
Other languages
Chinese (zh)
Other versions
CN112114793B (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

Images

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)
  • General Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Devices For Executing Special Programs (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (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 logic structure metadata information tree of the structured object; then, the metadata information tree is deeply traversed; then, respectively creating output simple assignment, nested assignment and array circulation assignment codes according to the traversal node type and the non-array leaf node, the non-array non-leaf node and the array node; and finally, merging and outputting the generated codes to a file for development and use. The main advantages of the invention are: the method for automatically generating the codes for converting the general JSON object into the C/C + + structured object based on the tree is provided, the codes for converting the JSON object into the C/C + + structured object are automatically generated according to the data structure definition, 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 demand for distributed application or cross-application integration is increasing, but due to technical needs or historical reasons, each application is often developed by adopting different programming languages, depends on different software environments, and runs on different hardware platforms, so that data exchange in heterogeneous environments needs to be performed among the applications. JSON (JavaScript Object Notation) is a lightweight data exchange format, stores and represents data in a text format completely independent of a programming language, has a simple and clear hierarchical structure, is easy to read and write, is easy to analyze and generate by a machine, has high network transmission efficiency, and is an ideal data exchange format in a heterogeneous environment. C/C + + is used as a flexible and efficient development language and is widely applied to the field of software development. When an application based on the JSON format is developed or accessed by utilizing C/C + +, the problem that JSON objects are converted into C/C + + internal structured objects must be solved.
The traditional solution is to manually write codes for each JSON object which needs to be converted into a C/C + + structured object according to the data structure definition of the C/C + + structured object, and convert each member of the C/C + + structured object one by one. According to the processing method, firstly, customized coding is required to be carried out according to different structured objects, and the processing method has no universality; secondly, a large amount of conversion coding work needs to be carried out manually, the automation degree is low, and the application development efficiency is influenced; finally, once the data structure of the structured object is changed, the conversion code modification needs to be manually carried out again, and the software maintenance and reconstruction are very inconvenient.
Disclosure of Invention
Aiming at the problems in the prior art, the invention provides a code automatic generation method for converting a general JSON object into a C/C + + structured object based on a tree, thereby realizing automatic code generation.
To achieve the above object, according to an aspect of the present invention, there is provided a method for automatically generating a code for converting a tree-based general JSON object into a C/C + + structured object, including:
step S1: reading and analyzing the data structure definition source code file of the C/C + + structured object, generating a structured object logical structure metadata information tree, and turning to step S2.
Step S2: and (4) creating a root node blank JSON object (the object variable name is JSON object1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3.
Step S3: the metadata information tree is deeply traversed (first-ordered) from the root node (the root node traversal sequence number is set to 1), and the process proceeds to step S4.
Step S4: and judging whether the traversal is finished or not, and if the traversal is finished, switching to the 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 going 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 the 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, and if so, turning to the step S8. If not, the current traversal node is of the complex type, and the process proceeds to step S9.
Step S8: the output leaf node translation code is constructed, and the process proceeds to step S4.
Step S9: the construct output non-leaf node translation code proceeds to step S4.
Step S10: and constructing an output array member node conversion code and transferring to the step S4.
Step S11: and combining and outputting all codes, and finishing the code generation.
Further, the output leaf node translation code is constructed in step S8, and the process is as follows:
the method comprises the steps of obtaining the shortest path from a root node of a metadata information tree to a current node, splicing the names of all node members on the shortest path by using a 'right', replacing the member names of the root node by the variable name of a C/C + + structured object to be converted, and generating the complete reference name of the current node member to be used as a left variable of an assignment statement. If a certain 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, and the shape of the array reference part is as [ 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;
taking the member name of the current node as a key, acquiring a value corresponding to the key from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a data type of a node member (the type of a leaf node member is a basic data type, such as short, int, long, float, double, string and the like, and it needs to be noted that the basic data types of different compilers are different) and taking the data type 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.
Further, the step S9 is to construct an output non-leaf node translation code, which includes the following steps:
creating a new JSON object variable (the name of the variable is like 'JSON object X', wherein X is the traversal sequence number of the current node) as an assignment left variable;
taking the member name of the current node as a key, acquiring a corresponding value from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a JSON object type, and taking the JSON object type as an assignment right value;
constructing an output assignment statement according to 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, the step S10 constructs the output array member node translation code, which includes the following steps:
setting an array as an N dimension (N is a positive integer), subscripts are respectively Index1, Index2, Index3,. IndexN, taking N array subscripts (Index1, Index2, Index3,. IndexN) as loop control ranges respectively, and using i1_ X, i2_ X, i3_ X.. iN _ X as loop control variables, wherein X is the traversal sequence number of a current node, constructing a loop statement (such as a for loop statement), and outputting N layers of nested loop control codes;
before the layer 1 loop body, the member name of the current node is used as a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the parent node of the current node, and the JSON object is converted into a JSON array object which is used as the JSON array object corresponding to the layer 1 loop body. If N is not less than 2, before the M (2< ═ M < ═ N) th-layer loop body, the value corresponding to the index in the JSON array object corresponding to the M-1 th-layer loop body is acquired using the control variable of the M-1 th-layer loop body as the index, and the JSON array object is converted to the JSON array object corresponding to the M-1 th-layer loop body. The JSON array object variable name corresponding to each layer of loop body is like 'jsonnarayiP _ X', wherein P is 1,2, 3.. N, and X is the traversal sequence number of the current node;
in the N-layer cycle body, if the type of the node member is a simple type, using a control variable of the N-layer cycle body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer cycle body, converting the value into the simple type, assigning the simple type to the member of the current node (using a node member complete reference name), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the name of the variable is as 'JSON object X', wherein X is the traversal sequence number of the current node) is created, the control variable of the loop body at the Nth layer is used as an index, the value corresponding to the index in the JSON array object corresponding to the loop body at the Nth layer is obtained, the JSON object is converted into a JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And the newly created JSON object variable is used 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; and the number of the first and second groups,
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
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 to a C/C + + structured object as described above.
Generally, compared with the prior art, the above technical solution conceived by the present invention has the following beneficial effects:
(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) the method automatically generates the codes for converting the JSON object into the C/C + + structured object according to the metadata information tree, reduces artificial hard coding, reduces development workload and improves the automation degree of software development.
Drawings
FIG. 1 is a flowchart of a method for automatically generating codes by converting a general JSON object into a C/C + + structured object based on a tree in an embodiment of the present invention;
FIG. 2 is a diagram illustrating a data structure definition of a Person structured object according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of an embodiment of the present invention, where a metadata information tree is generated after a data structure definition of a Person structured object is read and parsed;
FIG. 4 is a diagram illustrating traversal sequence numbers of nodes after a metadata information tree is deeply traversed according to an embodiment of the present invention;
fig. 5 is a schematic diagram of the whole function code of the embodiment of the present invention in which the merge output converts JSON object into structured object Person.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention. In addition, the technical features involved in the embodiments of the present invention described below may be combined with each other as long as they do not conflict 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 into a C/C + + structured object based on a tree, which changes the current mode of converting the JSON object into the C/C + + structured object by depending on artificial coding, generates a metadata information tree according to the structure definition of the C/C + + structured object, and further automatically generates a conversion code according to the metadata information tree without artificial coding, thereby improving the automation level and the development efficiency of software development; in addition, the method is suitable for any C/C + + structured object and has good universality.
As shown in fig. 1, the present invention provides a method for automatically generating a code for converting a general JSON object into a C/C + + structured object based on a tree, including:
step S1: reading and analyzing the data structure definition source code file of the C/C + + structured object, generating a structured object logical structure metadata information tree, and turning to step S2.
Step S2: and (4) creating a root node blank JSON object (the object variable name is JSON object1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3.
Step S3: the metadata information tree is deeply traversed (first-ordered) from the root node (the root node traversal sequence number is set to 1), and the process proceeds to step S4.
Step S4: and judging whether the traversal is finished or not, and if the traversal is finished, switching to the 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 going 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 the 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, and if so, turning to the step S8. If not, the current traversal node is of the complex type, and the process proceeds to step S9.
Step S8: the output leaf node translation code is constructed, and the process proceeds to step S4.
Specifically, an output leaf node translation code is constructed, which comprises the following processes:
the method comprises the steps of obtaining the shortest path from a root node of a metadata information tree to a current node, splicing the names of all node members on the shortest path by using a 'right', replacing the member names of the root node by the variable name of a C/C + + structured object to be converted, and generating the complete reference name of the current node member to be used as a left variable of an assignment statement. If a certain 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, and the shape of the array reference part is as [ 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;
taking the member name of the current node as a key, acquiring a value corresponding to the key from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a data type of a node member (the type of a leaf node member is a basic data type, such as short, int, long, float, double, string and the like) and taking the data type 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 construct output non-leaf node translation code proceeds to step S4.
Specifically, an output non-leaf node translation code is constructed, which comprises the following processes:
creating a new JSON object variable (the name of the variable is like 'JSON object X', wherein X is the traversal sequence number of the current node) as an assignment left variable;
taking the member name of the current node as a key, acquiring a corresponding value from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a JSON object type, and taking the JSON object type as an assignment right value;
constructing an output assignment statement according to 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 an output array member node conversion code and transferring to the step S4.
Specifically, the output array member node conversion code is constructed by the following process:
setting an array as an N dimension (N is a positive integer), subscripts are respectively Index1, Index2, Index3,. IndexN, and N array subscripts (Index1, Index2, Index3,. IndexN) are respectively used as loop control ranges, and i1_ X, i2_ X, i3_ X.. iN _ X are used as loop control variables, wherein N is the array dimension of a current node, and X is the traversal sequence number of the current node, a loop statement (such as a for loop statement) is constructed, and N layers of nested loop control codes are output;
before the layer 1 loop body, the member name of the current node is used as a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the parent node of the current node, and the JSON object is converted into a JSON array object which is used as the JSON array object corresponding to the layer 1 loop body. If N is not less than 2, before the M (2< ═ M < ═ N) th-layer loop body, the value corresponding to the index in the JSON array object corresponding to the M-1 th-layer loop body is acquired using the control variable of the M-1 th-layer loop body as the index, and the JSON array object is converted to the JSON array object corresponding to the M-1 th-layer loop body. The JSON array object variable name corresponding to each layer of loop body is like 'jsonnarayiP _ X', wherein P is 1,2, 3.. N, and X is the traversal sequence number of the current node;
in the N-layer cycle body, if the type of the node member is a simple type, using a control variable of the N-layer cycle body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer cycle body, converting the value into the simple type, assigning the simple type to the member of the current node (using a node member complete reference name), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the name of the variable is as 'JSON object X', wherein X is the traversal sequence number of the current node) is created, the control variable of the loop body at the Nth layer is used as an index, the value corresponding to the index in the JSON array object corresponding to the loop body at the Nth layer is obtained, the JSON object is converted into a JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And the newly created JSON object variable is used as the JSON object reference corresponding to the current node.
Step S11: and combining and outputting all codes, and finishing the code generation.
The method is clearly illustrated below with reference to a specific example, which is not intended to limit the invention. In this example, the qjson document object of QT is used to perform JSON object basic operation (other libraries supporting JSON operation may be used as well), the name of the Person object variable to be converted is cObjectVar, the name of the JSON object variable to be used is 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 into a C/C + + structured object based on a tree, which comprises the following steps:
step S1: reading and analyzing the data structure definition source code file of the C/C + + structured object, generating a structured object logical structure metadata information tree, and turning to step S2.
The detailed generation process of the C/C + + structured object metadata information tree is as follows:
the application example tool (or the customized compiler) scans the code file, performs lexical and syntactic analysis on the data structure definition of the structured object, and generates the metadata information tree of the C/C + + structured object. The root node of the metadata information tree represents the definition of the data structure 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 the name, type name and array subscript sequence (non-array is represented by 0) of each member (including internal substructure members) of the data structure, wherein the type of the leaf node member is a simple type (compiler built-in type), and the type of the non-leaf node member is a complex type (user-defined type). The format of each node (excluding the root node) on the tree is as follows: MemberName, TypeName, [ Index1, Index 2.. IndexN ], where MemberName represents a member name, TypeName represents a member type name, and the parenthetical intra-Index 1, Index 2.. IndexN represents a member array subscript.
The metadata information tree generated after reading and parsing the data structure definition of the Person structured object in the aforementioned step S1 is shown in fig. 3.
Step S2: and (4) creating a root node blank JSON object (the object variable name is JSON object1), assigning the JSON object to be converted to the newly created root node JSON object, and turning to S3. For example, in the previous example, a JSON object corresponding to the root node is created and assigned a statement "QJsonObject jsonObjectVar 1 ═ jsonObjectVar".
Step S3: the metadata information tree is deeply traversed (first-ordered) from the root node, the node traversal sequence number is set to 1, and the process proceeds to step S4.
Step S4: and judging whether the traversal is finished or not, and if the traversal is finished, switching to the 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 the 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, and if so, turning to the step S8. If not, the current traversal node is of the complex type, and the process proceeds to step S9.
Step S8: the output leaf node translation code is constructed, and the process proceeds to step S4.
Step S8.1: the method comprises the steps of obtaining the shortest path from a root node of a metadata information tree to a current node, splicing the names of all node members on the shortest path by using a 'right', replacing the member names of the root node by the variable name of a C/C + + structured object to be converted, and generating the complete reference name of the current node member to be used as a left variable of an assignment statement. If a certain 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, and the shape of the array reference part is as [ 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. houses [ i1_8]. area".
Step S8.2: and taking the member name of the current node as a key, acquiring a value corresponding to the key from a key value pair contained in a JSON object corresponding to a parent node of the current node, 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) and taking the data type as a right value of the assignment statement. For example, the right value of the leaf node area corresponding construct is "jsonobject 8.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 constructed corresponding to the leaf node area is "cObjectVar. hosts [ i1_8]. area ═ jsonObject8.value (" area "). toDouble (); ".
Step S9: the construct output non-leaf node translation code proceeds to step S4.
Step S9.1: a new JSON object variable (variable name shape such as "JSON object X", where X is the traversal sequence number of the current node) is created as the assignment left variable. For example, the JSON object variable code that creates the non-leaf node birthday in the metadata information tree is "QJsonObject jsonObject 2".
Step S9.2: and taking the member name of the current node as a key, acquiring a corresponding value from a key value pair contained in the JSON object corresponding to the parent node of the current node, converting the value into a JSON object type, and taking the JSON object type as an assignment right value. For example, the assigned right value code of the non-leaf node birthday in the metadata information tree is obtained as "jsonobject 1.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 assigned code of the non-leaf node birthday in the metadata information tree is "QJsonObject jsonObject2 ═ 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 "JSON object 2".
Step S10: and constructing an output array member node conversion code and transferring to the step S4.
Step S10.1: setting an array as an N dimension (N is a positive integer), subscripts are respectively Index1, Index2, Index3,. IndexN, taking N array subscripts (Index1, Index2, Index3,. IndexN) as loop control ranges, using i1_ X, i2_ X, i3_ X.. iN _ X as loop control variables (X is the traversal sequence number of the current node), constructing a loop statement (such as a for loop statement), and outputting N layers of nested loop control codes. For example, the house 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 loop body, the member name of the current node is used as a KEY (KEY), the value corresponding to the KEY is obtained from the JSON object corresponding to the parent node of the current node, and the JSON object is converted into a JSON array object which is used as the JSON array object corresponding to the layer 1 loop body. If N is not less than 2, before the M (2< ═ M < ═ N) th-layer loop body, the value corresponding to the index in the JSON array object corresponding to the M-1 th-layer loop body is acquired using the control variable of the M-1 th-layer loop body as the index, and the JSON array object is converted to the JSON array object corresponding to the M-1 th-layer loop body. The JSON array object variable name form corresponding to each layer of loop body is like 'jsonnarayiP _ X', wherein P is 1,2, 3. For example, the hosts node of the metadata information tree is a 1-way array node, and the corresponding JSON array object code statement is "QJsonArray jsonArrayi1_8 ═ JSON object1.value (" hosts "). toArray (); "
Step S10.3: in the N-layer cycle body, if the type of the node member is a simple type, using a control variable of the N-layer cycle body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer cycle body, converting the value into the simple type, assigning the simple type to the member of the current node (using a node member complete reference name), and outputting an assignment statement; if the node type is a complex type, a new JSON object variable (the name of the variable is as 'JSON object X', wherein X is the traversal sequence number of the current node) is created, the control variable of the loop body at the Nth layer is used as an index, the value corresponding to the index in the JSON array object corresponding to the loop body at the Nth layer is obtained, the JSON object is converted into a JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output. And the newly created JSON object variable is used as the JSON object reference corresponding to the current node. For example, an assignment code corresponding to a house node of the metadata information tree is "qjsonnobject jsonnobject 8 ═ jsonnoraryi 1_8.at (i1_8). toObject (); ".
Step S11: and combining and outputting all codes, and finishing the code generation. For example, a 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 parenthesis 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 a software implementation level, the present invention further provides an automatic code generation software system for implementing the conversion of a tree-based generic JSON object into a C/C + + structured object according to the present invention, specifically, the system includes:
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 syntactic analysis. The tool is utilized to generate metadata information trees of all structured objects in the application for the code generation module to use.
2. Code generation module for converting JSON object into structured object
And according to the conversion requirement, deeply traversing the metadata information tree corresponding to the target structured object, and generating a code for converting the JSON object into the structured object.
3. Code saving module
And saving the generated function code to a file for use.
Further, the present invention also provides a code generation system for converting a general JSON object into a C/C + + structured object, including:
at least one processor; and the number of the first and second groups,
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
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 the above-described generic JSON object to a C/C + + structured object.
It will be understood by those skilled in the art that the foregoing is only a preferred embodiment of the present invention, and is not intended to limit the invention, and that any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (10)

1. A code generation method for converting a general JSON object into a C/C + + structured object is characterized by comprising the following steps:
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 switching to the 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 switching to S3;
step S3: the metadata information tree is deeply traversed from the root node, the node traversal sequence number is set to be 1, and the step S4 is carried out;
step S4: judging whether the traversal is finished or not, and if the traversal is finished, turning to the step S11; if the traversal is not finished, the step S5 is executed;
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, if not, turning to the step S7; if the array is included, go to step S10;
step S7: judging whether the current traversal node is a leaf node, if so, turning to the step S8; if not, 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;
step S9: constructing and outputting a non-leaf node conversion code, and turning to step S4;
step S10: constructing and outputting the conversion codes of the member nodes of the array, and turning to the step S4;
step S11: and combining and outputting all codes, and finishing the code generation.
2. The method for generating code for converting a generic JSON object into a C/C + + structured object according to claim 1, wherein the step S8 of constructing an output leaf node conversion code includes:
acquiring a shortest path from a root node of a metadata information tree to a current node, splicing the names of all node members on the shortest path by using a 'right', replacing the member name of the root node by a variable name of a C/C + + structured object to be converted, and generating a complete reference name of the current node member as a left variable of an assignment statement;
taking the member name of the current node as a key, acquiring a value corresponding to the key from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a data type of a node member, and taking the data type as a right value of an assignment statement;
and constructing an output assignment statement according to the assignment left variable and the assignment right value.
3. The method for generating code to convert a generic JSON object into a C/C + + structured object as claimed in claim 2, wherein if a node on the shortest path contains an array, when the full reference name is spliced, the array reference part of the node is added after the name of the node member, such as "[ 1_ 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.
4. The method for generating code according to claim 2 or 3, wherein the data types of the leaf node members are basic data types.
5. The method for generating code for converting a generic JSON object to a C/C + + structured object as claimed in claim 1, wherein said step S9 of constructing output non-leaf node converted code comprises:
creating a new JSON object variable as an assignment left variable, wherein the name of the newly created JSON object variable is as 'JSON ObjectX', and X is the traversal sequence number of the current node;
taking the member name of the current node as a key, acquiring a corresponding value from a key value pair contained in a JSON object corresponding to a parent node of the current node, converting the value into a JSON object type, and taking the JSON object type as an assignment right value;
constructing an output assignment statement according to 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.
6. The method for generating code for converting a generic JSON object to a C/C + + structured object as claimed in claim 1, wherein the step S10 of constructing output array member node conversion code comprises:
setting an array as an N dimension, wherein N is a positive integer, subscripts are Index1, Index2, Index3,. IndexN respectively, taking the N array subscripts as loop control ranges respectively, using i1_ X, i2_ X, i3_ X.. iN _ X as loop control variables, and X is a traversal sequence number of a current node, constructing a loop statement, and outputting N layers of nested loop control codes;
before the layer 1 cycle body, taking the member name of the current node as a key, acquiring the value corresponding to the key from the JSON object corresponding to the parent node of the current node, converting the value into a JSON array object, and taking the JSON array object as the JSON array object corresponding to the layer 1 cycle body; if N is not less than 2, before the mth-layer loop body, in the mth-1-layer loop body, using a control variable of the mth-1-layer loop body as an index, obtaining a value corresponding to the index in a JSON array object corresponding to the mth-1-layer loop body, converting the value into the JSON array object as the JSON array object corresponding to the mth-layer loop body, wherein 2< ═ M < ═ N, and the name of a JSON array object variable corresponding to each layer of loop body is "jsonArrayiP _ X", wherein P is 1,2,3,. N, and X is a traversal number of the current node;
in the N-layer cycle body, if the type of the node member is a simple type, using a control variable of the N-layer cycle body as an index, acquiring a value corresponding to the index in a JSON array object corresponding to the N-layer cycle 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 name of the variable is as 'JSON object X', wherein X is the traversal sequence number of the current node, the control variable of the loop body at the Nth layer is used as an index, the value corresponding to the index in the JSON array object corresponding to the loop body at the Nth layer is obtained, the value is converted into a JSON object, the JSON object variable is assigned to the newly created JSON object variable, and an assignment statement is output; and the newly created JSON object variable is used as the JSON object reference corresponding to the current node.
7. The method for code generation of converting a generic JSON object into a C/C + + structured object according to claim 6, wherein the loop statement is a for loop statement, or a while loop statement, or a do while loop statement, or a foreach loop statement, or a do nothing loop statement.
8. The method for code generation of conversion of a generic JSON object to a C/C + + structured object as claimed in claim 1, wherein the root node of the metadata information tree represents the definition of the data structure of the parsed structured object, and the 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 group subscript sequence of each member of the data structure, wherein the type of a leaf node member is a simple type, the type of a non-leaf node member is a complex type, each node of the metadata information tree does not comprise a root node, each member of the data structure comprises an internal sub-structure member, the simple type is a compiler built-in type, and the complex type is a user-defined type.
9. The code generation method of converting a generic JSON object into a C/C + + structured object of claim 8, wherein the nodes on the tree are in the format of MemberName, TypeName, [ Index1, Index 2.. IndexN ], where MemberName represents a member name, TypeName represents a member type name, and parenthesized-inside Index1, Index 2.. IndexN represents a member array subscript.
10. A code generation system for converting a generic JSON object into a C/C + + structured object, comprising:
at least one processor; and the number of the first and second groups,
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
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 a generic JSON object to a C/C + + structured object as claimed in any of claims 1-9.
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 true CN112114793A (en) 2020-12-22
CN112114793B 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 (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150363176A1 (en) * 2013-12-25 2015-12-17 Jie Sheng Method and apparatus for code conversion
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

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150363176A1 (en) * 2013-12-25 2015-12-17 Jie Sheng Method and apparatus for code conversion
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
况立群;熊风光;韩燮;: "一种基于前缀编码的树生成算法", 小型微型计算机系统, no. 05 *
刘立;赵建军;张锦鹏;余进;: "JSON与XML动态转换研究及应用", 软件导刊, no. 11 *

Also Published As

Publication number Publication date
CN112114793B (en) 2023-09-22

Similar Documents

Publication Publication Date Title
Schordan et al. A source-to-source architecture for user-defined optimizations
CN101334728B (en) Interface creating method and platform based on XML document description
CN107844294B (en) High-availability contract execution method and system
CN111309757B (en) SQL interpreter and optimization method of HBase
CN101464799A (en) MPI parallel programming system based on visual modeling and automatic skeleton code generation method
EP2330504A1 (en) Lambda expressions
CN107016071B (en) A kind of method and system using simple path characteristic optimization tree data
CN101841515A (en) Target variable protocol data unit codec code automatic generation implementation method
CN113722218B (en) Software defect prediction model construction method based on compiler intermediate representation
CN113778449B (en) Avionic interface data adaptation conversion system
CN112114792A (en) Code generation method and system for converting universal C/C + + structured object into JSON object
US8209340B2 (en) Efficient functional representation of result shaping
CN112379917A (en) Browser compatibility improving method, device, equipment and storage medium
CN103235724A (en) Atomic operation semantic description based integrated translation method for multisource binary codes
CN112765209A (en) SQL statement syntax migration method and system between databases
CN115202626A (en) Low-code front-end development method supporting multi-technology stack components
CN115562642A (en) Code generating method based on database and Mustache template engine
CN112114793A (en) Code generation method and system for converting general JSON object into C/C + + structured object
Wenzel PIDE as front-end technology for Coq
CN107203406B (en) Processing method for distributed storage structure
CN1560763B (en) Method for translating expandable mark language path inquiry into structure inquiry
Pascoe et al. Construction of interfaces for the exchange of geographic data
CN114116779A (en) Deep learning-based power grid regulation and control field information retrieval method, system and medium
Yang et al. M2Coder: A Fully Automated Translator from Matlab M-functions to C/C++ Codes for ACS Motion Controllers
Ohori et al. A calculus with partially dynamic records for typeful manipulation of JSON objects

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