C++ parsing code generation method, system, and medium based on JSON data

By analyzing JSON data based on the visitor pattern based on the graph structure, C++ parsing code that is suitable for different data sources is generated, the problem of insufficient flexible configuration in the existing technology is solved, and efficient and automatic generation of C/C++ structured objects is realized, supporting multiple data sources and complex business logic, and detecting changes in JSON file format during the compilation period.

CN120144103BActive Publication Date: 2025-08-22成都赢瑞科技有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510629629.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-05-16
Publication Date
2025-08-22
Estimated Expiration
2045-05-16

AI Technical Summary

Technical Problem

The existing technology lacks flexible configuration capabilities and cannot adapt to the special structure and interaction methods of different data sources. The generated C/C++ code is only suitable for specific JSON data structures, lacks customized capabilities, and cannot meet the needs of complex business logic.

Method used

By creating a graph structure, traversing and parsing the JSON dataset, checking for type conflicts and dynamic nodes using visitor mode, generating C++ classes to store and output parsed code, and providing a code generation system based on HTTP servers and local files.

Benefits of technology

It realizes automatic generation of C/C++ structured object code without manual encoding, improves development efficiency, supports multiple data sources and complex business logic, detects changes in JSON file format during the compilation period, and supports static and dynamic JSON.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120144103B_ABST
    Figure CN120144103B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of software engineering technology, and more specifically, to a C++ parsing code generation method, system, and medium based on JSON data. The present invention provides a tree-based automatic code generation method for converting a general JSON object into a C / C++ structured object. The method primarily includes automatically generating code to convert a JSON object into a C / C++ structured object based on a data structure definition, thereby resolving the problem of converting a JSON object into a C / C++ structured object without the need for manual coding and improving development efficiency. By generating parsing code, the traditional method of accessing JSON attributes through strings is replaced by accessing them through functions, allowing attributes to be selected through automatic code prompts in an IDE without having to memorize strings.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of software engineering technology, and in particular to a C++ parsing code generation method, system, and medium based on JSON data. Background Art

[0002] Referring to the code generation method for converting a general JSON object into a C / C++ structured object disclosed in CN112114793A, first, the data structure definition of the structured object is scanned to obtain the logical structure metadata information tree of the structured object; then, the metadata information tree is deeply traversed; then, according to the traversal node type, simple assignment, nested assignment and array loop assignment codes are created and output according to non-array leaf nodes, non-array non-leaf nodes and array nodes respectively; finally, the aforementioned generated codes are merged and output into a file for development use.

[0003] However, existing technologies lack flexible configuration for different data sources and do not consider the special configuration required to handle different data sources (such as HTTP server interfaces and local files). In practical applications, different data sources have different structures and interaction methods.

[0004] Furthermore, the generated code lacks flexibility, failing to provide flexible configuration options for multiple data sources and varying requirements. The generated C / C++ code only works with specific JSON data structures, lacking customization capabilities and failing to meet the needs of complex business logic. Summary of the Invention

[0005] The purpose of the present invention is to provide a C++ parsing code generation method, system and medium based on JSON data to solve the above-mentioned problems in the prior art.

[0006] The present invention is achieved through the following technical solutions:

[0007] In a first aspect, the present invention provides a C++ parsing code generation method based on JSON data, comprising:

[0008] Read HTTP server data, create a JSON dataset to get UrlEntry, and parse the JSON dataset;

[0009] For each UrlEntry, create a graph, where the nodes of the graph correspond to JSON keys, and create a list of node attributes, which is used to store the type corresponding to the key;

[0010] Use the visitor mode to traverse each graph through TypeVisitor, starting from the first node to check whether there are conflicts among all types stored in the node. If there is a conflict, an error is reported and the traversal is stopped. If there is no conflict, the type corresponding to the node is generated until all nodes are traversed.

[0011] Use the visitor mode to traverse each graph through MergeVisitor, starting from the first node, check whether the node is a dynamic node. If the node is dynamic, mark the node as Merged. If the node is not dynamic, do not mark it until all nodes are traversed.

[0012] Use the visitor mode through CodeVisitor to traverse each graph, starting from the first node, check whether the node is merged, if so, skip it, if not, generate a C++ class for the node, and the class members are the child nodes of the node, until all nodes are traversed, generate C++ code to parse all collected Json data sets, store and output them in the form of files.

[0013] Preferably, creating a JSON data set to obtain a UrlEntry includes:

[0014] Store each JsonText returned by the same HTTP server interface as an entry and store it in a unified collection JsonCollection;

[0015] Store the JsonCollection and several access parameters as a UrlEntry, and store it in the dataset UrlCollection as an entry in the dataset.

[0016] Preferably, parsing the JSON data set includes:

[0017] Traverse each entry UrlEntry in the dataset UrlCollection, and then traverse the JsonCollection in each entry UrlEntry. For each JsonText, use JSON syntax to parse it and get the corresponding data object JsonObject.

[0018] Preferably, the step of checking whether there are conflicts among all types stored in the node includes:

[0019] Determine whether the type of the current node is null. If so, there is no conflict in the type stored in the node.

[0020] If the type of the current node is not null, the type stored in the node is conflicting.

[0021] Preferably, the checking whether the node is a dynamic node includes:

[0022] Sort all child nodes of the node by name, traverse the names and types of all child nodes, and use the resulting string as the structural feature of the node;

[0023] If the structural characteristics of the node are the same as those of one of its parent nodes, then the node is a dynamic node; otherwise, the node is not a dynamic node.

[0024] In a second aspect, the present invention further provides a C++ parsing code generation system based on JSON data, comprising:

[0025] HTTP server, configured to collect JSON sample data;

[0026] The Json collector is configured to grab the response file from the external server according to the preset configuration and create data according to the interface format of the Json dataset;

[0027] The Json dataset module is configured to save the sample data structure of all interactive responses of the server;

[0028] The file collector is configured to create a Json dataset based on the JSON file;

[0029] The CPP code generation module is configured to generate a CPP code file for accessing the dataset based on the collected Json dataset.

[0030] Preferably, the Json collector includes:

[0031] The CollectionCreator module is configured to create a Json dataset and add the response text obtained by the HTTP client to the Json collector;

[0032] HTTP client, configured to interact with HTTP server.

[0033] Preferably, the Json dataset module includes:

[0034] The UrlCollection module is configured as a Map container to store all UrlEntry;

[0035] The UrlEntry module is configured to store all sample access data for a URL;

[0036] Url module, which is configured to access a URL;

[0037] The JsonCollection module is configured as a List container to store all sample access data of a certain URL;

[0038] The JsonText module is configured to access data in a sample, in string format;

[0039] The Option module is configured to set the configuration options used when generating URL parsing code.

[0040] Preferably, the CPP code generation module includes:

[0041] The JsonReader module is configured to parse JSON files and generate corresponding JsonObjects;

[0042] The JsonObject module is configured as a tree-structured in-memory representation of a JSON file;

[0043] The JsonNode module is configured as a node in the JsonObject tree structure, corresponding to the attribute node in the JSON file;

[0044] The GraphCreator module is configured to convert a JsonObject into a graph structure;

[0045] The Graph module is configured to represent a JsonObject. One Graph corresponds to one JsonObject.

[0046] The Vertex module is configured to represent the vertices of the graph structure, and one vertex corresponds to one JsonNode;

[0047] Edge modules are configured to represent parent-child relationships between vertices;

[0048] The TypeCollection module is configured to save the DataType types collected from different sample data for the same attribute;

[0049] The DataType module is configured to store the type of the JSON attribute;

[0050] The TypeVisitor module is configured to traverse the Graph vertices, analyze the TypeCollection collected at each vertex, and determine whether there is a type conflict in the TypeCollection;

[0051] The MergeVisitor module is configured to traverse the Graph vertices and make decisions based on the dynamic node configuration of each vertex Option, including checking whether the node is dynamic. If the node is dynamic, it marks the node as Merged; if the node is not dynamic, it does not mark it;

[0052] The CodeVisitor module is configured to traverse the Graph vertices and generate CPP code based on the name, type, and whether the vertex is merged.

[0053] In a third aspect, the present invention further provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the above-mentioned C++ parsing code generation method based on JSON data.

[0054] The technical solution of the present invention has at least the following advantages and beneficial effects:

[0055] 1. The tree-based automatic code generation method for converting a general JSON object into a C / C++ structured object provided by the present invention mainly includes automatically generating code for converting a JSON object into a C / C++ structured object according to a data structure definition, solving the problem of converting a JSON object into a C / C++ structured object without manual coding, thereby improving development efficiency.

[0056] 2. By generating parsing code, the traditional way of accessing JSON properties through strings is changed to accessing through functions. In this way, you can select properties through the IDE's automatic code prompts without having to remember strings.

[0057] 3. Detect changes in the JSON file format at compile time. Traditionally, when accessing via string names, if the JSON file format changes, such as a property name change, the compiler can only detect that the property name does not exist at runtime. When accessing via generated parsing code, if the JSON file format changes, such as a property name change, the compiler will prompt that the function does not exist because each property name is accessed via a function with the same name.

[0058] 4. Support JSON fusion. Some properties of the same server interface may be optional, or only part of the JSON information may be returned each time due to incomplete data, or for other design reasons such as saving communication data volume. By putting different return information into a JsonCollection, complete C++ access code can be generated. BRIEF DESCRIPTION OF THE DRAWINGS

[0059] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. It should be understood that the following drawings only illustrate certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without paying any creative work.

[0060] Figure 1 A schematic diagram of the software control logic of the present invention;

[0061] Figure 2 This is a flow chart of generating C++ parsing code for an HTTP server JSON interface according to the present invention;

[0062] Figure 3 It is a code prompt schematic diagram of the present invention. DETAILED DESCRIPTION

[0063] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions of the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings herein can be arranged and designed in various different configurations.

[0064] The division of modules in this application is a logical division. In actual application, there may be other division methods. For example, multiple modules can be combined or integrated into another system, or some features can be ignored or not executed.

[0065] In addition, the connection, coupling or communication in this application may be a direct connection, coupling or communication between related objects, or an indirect connection, coupling or communication through other devices. In addition, the connection, coupling or communication between objects may be electrical or other similar forms, which are not limited in this application.

[0066] Independently described modules or submodules may or may not be physically separate; they may be implemented in software or hardware. Some modules or submodules may be implemented in software, with the processor invoking the software to implement the functionality of these modules or submodules, while other modules or submodules may be implemented in hardware, such as hardware circuits. Furthermore, some or all of the modules may be selected based on actual needs to achieve the objectives of the present application.

[0067] JSON (JavaScript Object Notation) is a lightweight data exchange format based on a subset of JavaScript. It represents data objects in a text format that is easy for humans to read and write, and is also easy for machines to parse and generate. It is widely used in modern network applications and data interaction.

[0068] Schema is a tool for describing the structure of JSON data. It helps verify whether JSON data conforms to the expected format. It constrains JSON data by defining rules such as property type, mandatory, and allowed value range.

[0069] Parsing: Parsing is the process of converting JSON-formatted data (usually a string) into a data structure (such as an object or array) that a programming language can understand and process. This process allows programs to read information from JSON data, such as obtaining specific key-value pairs and iterating over array elements.

[0070] C++: C++ is a statically typed, compiled, general-purpose, case-sensitive, and informal programming language that supports procedural, object-oriented, and generic programming. Developed by Bjarne Stroustrup at Bell Labs in the early 1980s, it was originally developed as an extension of the C language to provide more powerful programming features and better code organization. C++ is widely used in systems software, game development, embedded systems, high-performance computing, and other fields.

[0071] Please refer to Figure 1-Figure 2 The present invention provides a method for generating C++ parsing code based on JSON data. In this embodiment, there are two ways to generate parsing code and send it to the user. One method is to generate C++ parsing code for the HTTP server JSON interface, including:

[0072] Configure interface information: mainly configure the URL address and parameters of the external server to be collected, and set whether it is a dynamic node;

[0073] Collect sample data: Based on the URL and access parameters in the configured interface information, call the external server interfaces one by one, record the server responses, and create a JSON data set.

[0074] Generate code: Generate CPP code based on sample data and configuration.

[0075] The specific process is as follows:

[0076] S101: Read HTTP server data, create a JSON data set to obtain UrlEntry, and parse the JSON data set;

[0077] Access a data server interface through an HTTP client. The access method is based on a predefined configuration, including the interface address (URL), interface parameters, and whether it is a dynamic interface (Option). The same interface may have multiple configurations, and each access will be performed according to a configuration. Therefore, one interface may have multiple access results.

[0078] S102: For each UrlEntry, create a graph, where the nodes of the graph correspond to JSON keys, and create a list including the attributes of the nodes, where the list is used to store the type corresponding to the key;

[0079] Types include int, string, bool, null, object, and list. The edges of the graph represent the containment relationships between keys. All JsonObjects under the UrlEntry are then added to the graph based on the aforementioned correspondence. Depending on the level of detail returned by the server response, each JsonObject may correspond to all or part of the graph's nodes.

[0080] S103: Use the visitor mode to traverse each graph through TypeVisitor, starting from the first node to check whether there is any conflict in all types stored in the node. If there is a conflict, an error is reported and the traversal is stopped. If there is no conflict, the type corresponding to the node is generated until all nodes are traversed;

[0081] S104: Use the visitor mode to traverse each graph through MergeVisitor, starting from the first node, and check whether the node is a dynamic node. If the node is dynamic, mark the node as Merged. If the node is not a dynamic node, do not mark it until all nodes are traversed;

[0082] S105: Use the visitor mode through CodeVisitor to traverse each graph, starting from the first node, check whether the node is merged, if so, skip it, if not, generate a C++ class for the node, and the class members are the child nodes of the node, until all nodes are traversed, generate C++ code for parsing all collected Json data sets, store and output them in the form of files.

[0083] The other is to generate parsing code for local JSON files. Generating C++ parsing code for local JSON files is mainly used when the server is unavailable or the interaction is complex (for example, many pre-authentication authentications are required). It generates interface code based on offline file data, including:

[0084] Configuration file information: mainly configures local JSON disk files that have been collected through other means or written by yourself, including the file path and whether it is a dynamic node;

[0085] Load sample data: Load the local file according to the configured file information and create a Json dataset.

[0086] Generate code: Generate CPP code based on sample data and configuration.

[0087] The specific process includes:

[0088] Create a JSON dataset: put all local JSON files with the same JSON structure or a substructure of the same structure into the same folder. Each folder is equivalent to a server interface. The content of each file, JsonText (JSON format), is stored as an entry in the collection JsonCollection.

[0089] The subsequent steps are the same as step S102, step S103, step S104 and step S105.

[0090] The tree-based automatic code generation method for converting a general JSON object into a C / C++ structured object provided by the present invention mainly includes automatically generating code for converting a JSON object into a C / C++ structured object according to a data structure definition, solving the problem of converting a JSON object into a C / C++ structured object without manual coding, thereby improving development efficiency.

[0091] In an exemplary embodiment of the present invention, creating a JSON dataset to obtain a UrlEntry includes:

[0092] Store each JsonText returned by the same interface of the HTTP server as an entry and store it in a unified collection JsonCollection;

[0093] Store the JsonCollection and several access parameters as a UrlEntry, and store it in the dataset UrlCollection as an entry in the dataset.

[0094] In an exemplary embodiment of the present invention, parsing a JSON dataset includes:

[0095] Traverse each entry UrlEntry in the dataset UrlCollection, and then traverse the JsonCollection in each entry. For each JsonText, use JSON syntax to parse it and get the corresponding data object JsonObject.

[0096] In an exemplary embodiment of the present invention, checking whether all types stored in the node have conflicts includes:

[0097] Determine whether the type of the current node is null. If so, the type stored in the node has no conflict; if the type of the current node is not null, the type stored in the node has a conflict.

[0098] The type generation rules are as follows: if there is only null, then it is null; if there are both null and other types, then it is other types; if there is only other types except null, then it is other types.

[0099] In an exemplary embodiment of the present invention, checking whether the node is a dynamic node includes:

[0100] Sort all child nodes of the node by name, traverse the names and types of all child nodes, and use the obtained string as the structural feature of the node; if the structural feature of the node is the same as the structural feature of one of its parent nodes, then the node is a dynamic node, otherwise, the node is not a dynamic node.

[0101] In a second aspect, the present invention further provides a C++ parsing code generation system based on JSON data, comprising:

[0102] The HTTP server is configured to collect JSON sample data. The server interface must return data in JSON format.

[0103] The Json collector is configured to grab the response file from the external server according to the preset configuration and create data according to the interface format of the Json dataset;

[0104] The Json dataset module is configured to save the sample data structure of all interactive responses of the server;

[0105] The file collector is configured to create a Json dataset based on a JSON file. The Json file is a locally saved or manually written Json file that will be used to construct the Json dataset.

[0106] The CPP code generation module is configured to generate a CPP code file for accessing the dataset based on the collected Json dataset.

[0107] In an exemplary embodiment of the present invention, the Json collector includes:

[0108] The CollectionCreator module is configured to create a Json dataset and add the response text obtained by the HTTP client to the Json collector;

[0109] HTTP client, configured to interact with HTTP server, including:

[0110] GET: GET requests are mainly used to obtain resources from the server;

[0111] POST: POST requests are mainly used to submit data to the server, usually to create, update, or delete resources on the server.

[0112] In an exemplary embodiment of the present invention, the Json dataset module includes:

[0113] The UrlCollection module is configured as a Map container to store all UrlEntry;

[0114] The UrlEntry module is configured to store all sample access data for a URL;

[0115] Url module, which is configured to access a URL;

[0116] The JsonCollection module is configured as a List container to store all sample access data of a certain URL;

[0117] The JsonText module is configured to access data in a sample, in string format;

[0118] The Option module is configured to set the configuration options used when generating URL parsing code.

[0119] In an exemplary embodiment of the present invention, the CPP code generation module includes:

[0120] The JsonReader module is configured to parse JSON files and generate corresponding JsonObjects;

[0121] The JsonObject module is configured as a tree-structured memory representation of a JSON file. One JsonObject corresponds to one JSON file.

[0122] The JsonNode module is configured as a node in the JsonObject tree structure, corresponding to the attribute node in the JSON file;

[0123] The GraphCreator module is configured to convert a JsonObject into a graph structure;

[0124] The Graph module is configured to represent a JsonObject. One Graph corresponds to one JsonObject.

[0125] The Vertex module is configured to represent the vertices of the graph structure, and one vertex corresponds to one JsonNode;

[0126] Edge modules are configured to represent parent-child relationships between vertices;

[0127] The TypeCollection module is configured to save the DataType types collected from different sample data for the same attribute;

[0128] The DataType module is configured to store the type of the JSON attribute;

[0129] The TypeVisitor module is configured to traverse the Graph vertices, analyze the TypeCollection collected at each vertex, and determine whether there is a type conflict in the TypeCollection;

[0130] For example, if a vertex collects both int and string types, a type conflict will occur. However, if a vertex collects both int and null, no conflict will occur. If there is no conflict, the appropriate data type is selected for the vertex. If there is a conflict, an error is reported.

[0131] The MergeVisitor module is configured to traverse the Graph vertices and make decisions based on the dynamic node configuration of each vertex Option, including checking whether the node is dynamic. If the node is dynamic, it marks the node as Merged; if the node is not dynamic, it does not mark it;

[0132] Specifically, if the node is dynamic, all child nodes with the same name and type are marked as Merged. When generating code, nodes with Merged as true do not generate type code but directly reference the type of the dynamic node. If the node is not dynamic, the node remains unchanged.

[0133] The CodeVisitor module is configured to traverse the Graph vertices and generate CPP code based on the name, type, and whether each vertex is merged. The C++ code that can parse all collected Json data sets is finally generated by the CPP code and exists in the form of a file.

[0134] The above-mentioned solution of the present invention can bring the following effects:

[0135] 1. By generating parsing code, the traditional way of accessing JSON properties through strings is changed to accessing through functions. In this way, you can select properties through the IDE's automatic code prompts without having to remember strings.

[0136] For example, the following JSON response file:

[0137] {

[0138] "timestamp": 1733295008882

[0139] "code": 200,

[0140] "msg": "success",

[0141] "data": null,

[0142] }

[0143] Access in string form:

[0144] ison data["code"]

[0145] Access with the generated parsing code:

[0146] ison data.code()

[0147] Such as Figure 3 shown to generate code hints.

[0148] 2. Detect changes in the JSON file format during compilation.

[0149] For traditional access by string name, when the JSON file format changes, such as a change in an attribute name, it can only be detected at runtime that the attribute name does not exist.

[0150] For access with the generated parsing code, when the JSON file format changes, such as a change in an attribute name, since each attribute name is accessed through a function with the same name, the compiler will prompt that the function does not exist.

[0151] 3. Support JSON fusion.

[0152] For the same interface of the server, some attributes may be optional, or due to incomplete data, or for other design reasons such as saving communication data volume, only partial JSON information is returned each time. By putting different return information into a JsonCollection, complete C++ access code can be generated.

[0153] For example, when accessing getPersonInfo?name=张三, the returned

[0154] {

[0155] "name": "张三",

[0156] "age":18

[0157] }

[0158] Access getPersonInfo?name=Zhang San and return.

[0159] {

[0160] "name":"Li Si",

[0161] "height":170

[0162] }

[0163] The fused Json:

[0164] {

[0165] "name":"Zhang San",

[0166] "age":18,

[0167] "height":170

[0168] }

[0169] 4. Support both static JSON and dynamic JSON.

[0170] Static JSON means that the level and number of node structures in a JSON file no longer change.

[0171] For example,

[0172] {

[0173] "timestamp":1733295008882,

[0174] "code": 200,

[0175] "msg": "success",

[0176] "node":{

[0177] "name": "aa?"

[0178] },

[0179] }

[0180] Dynamic nodes refer to nodes in a JSON file whose number and level may change. For example, when representing a tree structure, there are recursive nodes. The second-level children in the following example are dynamic nodes because they can have third, fourth, or even infinite levels of children:

[0181] {

[0182] "timestamp":1733295008882,

[0183] "code": 200,

[0184] "msg":"success",

[0185] "node": {

[0186] "name":"aaa",

[0187] "children": [

[0188] {

[0189] "name": "bbb",

[0190] "children": [ ]

[0191] } ]

[0193] }.

[0194] In addition, the functional units in the various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0195] If the integrated unit is implemented as a software functional unit and sold or used as a standalone product, it can be stored on a computer-readable storage medium. This computer software product, stored on a storage medium, includes instructions for causing a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the steps of the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0196] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention. Those skilled in the art will readily appreciate that various modifications and variations of the present invention are possible. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.

Claims

1. A C++ parsing code generation method based on JSON data, characterized in that: include: Read HTTP server data, create a JSON dataset to get UrlEntry, and parse the JSON dataset; For each UrlEntry, create a graph, where the nodes of the graph correspond to JSON keys, and create a list of node attributes, which is used to store the type corresponding to the key; Use the visitor mode to traverse each graph through TypeVisitor, starting from the first node to check whether there are conflicts among all types stored in the node. If there is a conflict, an error is reported and the traversal is stopped. If there is no conflict, the type corresponding to the node is generated until all nodes are traversed. Use the visitor mode to traverse each graph through MergeVisitor, starting from the first node, check whether the node is a dynamic node. If the node is dynamic, mark the node as Merged. If the node is not dynamic, do not mark it until all nodes are traversed. Use CodeVisitor to traverse each graph using the visitor mode, starting from the first node, and check whether the node is merged. If so, skip it. If not, generate a C++ class for the node, and the class members are the child nodes of the node. After traversing all nodes, generate C++ code to parse all collected Json data sets, store them in a file, and output them; Creating a JSON dataset to obtain a UrlEntry includes: Store the JSON data returned by each response of the HTTP server to the same interface as an independent entry into a unified collection JsonCollection; Store the JsonCollection and access parameters as a UrlEntry, and store the UrlEntry in the dataset UrlCollection as an entry in the dataset; Parsing the JSON dataset includes: Traverse each entry UrlEntry in the dataset UrlCollection, and then traverse the JsonCollection in each entry UrlEntry. For each JsonText, use JSON syntax to parse it and get the corresponding data object JsonObject.

2. A C++ parsing code generation method based on JSON data according to claim 1, characterized in that: The checking of whether all types stored in the node have conflicts includes: Determine whether the type of the current node is null. If so, there is no conflict in the type stored in the node. If the type of the current node is not null, the type stored in the node is conflicting.

3. A C++ parsing code generation method based on JSON data according to claim 2, characterized in that: The checking whether the node is a dynamic node includes: Sort all child nodes of the node by name, traverse the names and types of all child nodes, and use the resulting string as the structural feature of the node; If the structural characteristics of the node are the same as those of one of its parent nodes, then the node is a dynamic node; otherwise, the node is not a dynamic node.

4. A C++ parsing code generation system based on JSON data, characterized in that: include: HTTP server, configured to collect JSON sample data; The Json collector is configured to grab the response file from the external server according to the preset configuration and create data according to the interface format of the Json dataset; The Json dataset module is configured to save the sample data structure of all interactive responses of the server; The file collector is configured to create a Json dataset based on the JSON file; The CPP code generation module is configured to generate a CPP code file for accessing the collected Json dataset; The Json collector includes: The CollectionCreator module is configured to create a Json dataset and add the response text obtained by the HTTP client to the Json collector; HTTP client, configured to interact with HTTP server; The Json dataset module includes: The UrlCollection module is configured as a Map container to store all UrlEntry; The UrlEntry module is configured to store all sample access data for a URL; Url module, which is configured to access a URL; The JsonCollection module is configured as a List container to store all sample access data of a certain URL; The JsonText module is configured to access data in a sample, in string format; The Option module is configured to set the configuration options used when generating the URL parsing code; The CPP code generation module includes: The JsonReader module is configured to parse JSON files and generate corresponding JsonObjects; The JsonObject module is configured as a tree-structured in-memory representation of a JSON file; The JsonNode module is configured as a node in the JsonObject tree structure, corresponding to the attribute node in the JSON file; The GraphCreator module is configured to convert a JsonObject into a graph structure; The Graph module is configured to represent a JsonObject. One Graph corresponds to one JsonObject. The Vertex module is configured to represent the vertices of the graph structure, and one vertex corresponds to one JsonNode; Edge modules are configured to represent parent-child relationships between vertices; The TypeCollection module is configured to save the DataType types collected from different sample data for the same attribute; The DataType module is configured to store the type of the JSON attribute; The TypeVisitor module is configured to traverse the Graph vertices, analyze the TypeCollection collected at each vertex, and determine whether there is a type conflict in the TypeCollection; The MergeVisitor module is configured to traverse the Graph vertices and make decisions based on the dynamic node configuration of each vertex Option, including checking whether the node is dynamic. If the node is dynamic, it marks the node as Merged; if the node is not dynamic, it does not mark it; The CodeVisitor module is configured to traverse the Graph vertices and generate CPP code based on the name, type, and whether the vertex is Merged.

5. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements a C++ parsing code generation method based on JSON data according to any one of claims 1 to 3.

Citation Information

Patent Citations

  • Code generation method and system for converting universal JSON object into C / C++ structured object

    CN112114793A

  • Template code generation method and device, computer equipment and storage medium

    CN117891453A