File optimization method and device, storage medium, electronic equipment and product
By acquiring and parsing JSON files in the development environment, sorting them according to rules, and saving them, the problem of low development efficiency and debugging difficulties caused by non-standard JSON file formats and disordered key-value pairs is solved, achieving automated file optimization and code quality improvement.
Patent Information
- Application Number
- CN202511543206.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-27
- Publication Date
- 2026-01-16
AI Technical Summary
The development efficiency and debugging difficulties caused by non-standard JSON file format and disordered key-value arrangement are particularly prominent in team collaborations and large-scale projects.
The source file is obtained from the development environment by calling the interface. The text content is converted into structured data objects by a parsing engine for a specific type of file. The data objects are sorted according to preset rules and the results are finally written to the target file.
It enables automated sorting and optimization of JSON files, improving code readability and maintainability, and enhancing development efficiency and debugging convenience.
Smart Images

Figure CN121349965A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer software development, in particular to a file optimization method and device, a storage medium, and an electronic device and product. BACKGROUND
[0002] In the field of software development, especially in web front-end development, JSON (JavaScript Object Notation) files are widely used as a standard format for data exchange. However, due to the unordered nature and strict format requirements of JSON files, manual editing and maintenance of these files often encounter problems such as poor code readability, arbitrary key name ordering, and frequent syntax error checking. These problems are particularly prominent in team collaboration and large-scale projects, increasing development time and maintenance costs. In recent years, JSON5, as an extended version of JSON format, has introduced features such as comments, trailing commas, and single quotes, improving the flexibility and readability of data files. However, the emergence of this format also poses compatibility challenges to existing tools and methods, especially in formatting and automatic ordering. Currently, software engineers often rely on plug-ins of code editors or independent tools for JSON file formatting and ordering, but these tools often only support standard JSON format, with limited compatibility support for JSON5, and are inefficient when handling large numbers of JSON files in projects.
[0003] Therefore, in the related art, the problem of low development efficiency and difficult debugging caused by non-standard format and unordered key-value arrangement of JSON files has not been effectively solved. SUMMARY
[0004] The embodiments of the present application provide a file optimization method and device, a storage medium, and an electronic device and product, to at least solve the problem of low development efficiency and difficult debugging caused by non-standard format and unordered key-value arrangement of JSON files in the related art.
[0005] According to an embodiment of the embodiment of the present application, a file optimization method is provided, comprising: calling a first preset interface to obtain all workspace folders in a development environment, and screening all source files meeting preset extension requirements from files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting first extension requirements and second type files meeting second extension requirements; reading text content of each source file in the source file set, and using a parsing engine corresponding to the second type files in the source file set to convert a string representing the text content into a structured data object, wherein the data object is a data collection containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of multiple elements; performing sorting processing on the data object, and writing a sorting processing result into a target file.
[0006] In an example embodiment, converting the string representing the text content into the structured data object using the parsing engine corresponding to the second type files in the source file set comprises: scanning each character in the string one by one based on a format requirement of the second type files to identify multiple lexical tokens, wherein the multiple lexical tokens include at least one of the following: a brace token representing the start and end of an object, a bracket token representing the start and end of an array, a colon token representing the separation of a key and a value, and a comma token representing the separation of values; and performing syntax analysis on the multiple lexical tokens according to the parsing engine to generate the structured data object.
[0007] In an example embodiment, the sorting processing on the data object comprises: performing sorting processing on the data object according to a preset sorting rule to obtain a first processing result, wherein the sorting rule includes: sorting based on a dictionary order of a key name of the data object, and sorting based on a preset key-value order list; and performing conversion processing on the data object based on the first processing result to obtain a second processing result, wherein the conversion processing is to convert the data object into a standard string.
[0008] In an example embodiment, before the sorting processing on the data object according to the preset sorting rule to obtain the first processing result, the method further comprises: determining whether a data structure of the data object is an array; in a case where the data structure is an array, sorting each element of the array, and performing string comparison on the sorted array elements to arrange the data object in a correct order; and in a case where the data structure is not an array, sorting keys and values corresponding to the keys of the data object.
[0009] In one example embodiment, in the case of a data structure being a non-array, sorting keys of a data object and values corresponding to the keys comprises: traversing all nested levels of the data object and determining all keys in each level object and values corresponding to each key; first sorting all keys in each level object according to a preset dictionary order, and second sorting values corresponding to each key in the sorted all keys.
[0010] In one example embodiment, after the string comparison of the sorted array elements, the method further comprises: determining whether to adjust the order of the array elements according to the comparison result of the strings; in the case that the comparison result indicates that the sorting order of the current array element is inconsistent with the preset sorting order, determining to adjust the order of the array elements; in the case that the comparison result indicates that the sorting order of the current array element is consistent with the preset sorting order, determining not to adjust the order of the array elements.
[0011] According to another aspect of the embodiments of the present application, a file optimization device is further provided, comprising: an acquisition module configured to call a first preset interface to acquire all workspace folders in a development environment, and to filter all source files meeting preset extension requirements from files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting first extension requirements and second type files meeting second extension requirements; a conversion module configured to read text content of each source file in the source file set, and to convert a string representing the text content into a structured data object using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of multiple elements; and a processing module configured to perform sorting processing on the data object, and to write a sorting processing result into a target file.
[0012] According to still another aspect of the embodiments of the present application, a computer readable storage medium is further provided, and the computer readable storage medium stores a computer program, wherein the computer program is configured to execute the above file optimization method when running.
[0013] According to still another aspect of the embodiments of the present application, an electronic device is further provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the above file optimization method through the computer program.
[0014] According to still another aspect of the embodiments of the present application, a computer program product is further provided, comprising a computer program, and the computer program executes the above file optimization method when executed by a processor.
[0015] In the embodiment of the present application, the first preset interface is called to obtain all workspace folders in the development environment, and all source files meeting the preset extension name requirement are filtered from the files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting the first extension name requirement and second type files meeting the second extension name requirement; the text content of each source file in the source file set is read, and the string representing the text content is converted into a structured data object using the parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of multiple elements; the data object is sorted, and the sorting result is written into a target file. By using the above technical solution, the problems of low development efficiency and difficult debugging caused by the non-standard format and unordered key-value arrangement of the JSON file are solved. Further, by calling the interface to collect source files with specified extension names in the development environment, the content of the source files is parsed into structured data objects, and the data objects are sorted according to the rules and saved to the target file, thereby realizing the automatic sorting and optimization processing of the specific type files. BRIEF DESCRIPTION OF DRAWINGS
[0016] The accompanying drawings, which are incorporated herein and form part of the specification, illustrate embodiments consistent with the present application and, together with the description, further serve to explain the principles of the application.
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the accompanying drawings needed to be used in the embodiments or prior art description will be briefly introduced. Obviously, those skilled in the art can obtain other drawings according to these drawings without any creative effort.
[0018] Figure 1 is a hardware environment schematic diagram of a file optimization method according to an embodiment of the present application;
[0019] Figure 2 is a flowchart of a file optimization method according to an embodiment of the present application;
[0020] Figure 3 is a VS Code plug-in implementation flowchart according to an embodiment of the present application;
[0021] Figure 4 is a plug-in logic implementation flowchart according to an embodiment of the present application;
[0022] Figure 5 is a schematic diagram of a JSON file before sorting according to an embodiment of the present application;
[0023] Figure 6is a schematic diagram of a JSON file after sorting according to an embodiment of the present application;
[0024] Figure 7 is a JSON file output flow schematic diagram according to an embodiment of the present application;
[0025] Figure 8 is a structural block diagram of a file optimization device according to an embodiment of the present application. DETAILED DESCRIPTION
[0026] In order to enable persons skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by persons skilled in the art without creative labor should fall within the scope of protection of the present application.
[0027] It should be noted that the terms “first”, “second”, and the like in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily indicate a specific order or a chronological sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms “include” and “have” and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, device, or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but can include other steps or units that are not clearly listed or inherent to these processes, methods, devices, or devices.
[0028] According to an aspect of an embodiment of the present application, a file optimization method is provided. The file optimization method is widely applied to smart home, smart home, smart home device ecology, intelligence house ecology, and other whole-house intelligent digital control application scenarios. Optionally, in the present embodiment, the above-mentioned file optimization method can be applied to the hardware environment composed of a terminal device 102 and a server 104 as shown in the following figure. Figure 1 Figure 1 is a hardware environment schematic diagram of a file optimization method according to an embodiment of the present application, as shown in the following figure. Figure 1 As shown, the server 104 is connected with the terminal device 102 through a network, which can be used to provide services (such as application services, etc.) for the terminal or the client installed on the terminal, a database can be set on the server or independently of the server, which is used to provide data storage services for the server 104, cloud computing and / or edge computing services can be configured on the server or independently of the server, which is used to provide data operation services for the server 104.
[0029] The above network can include but is not limited to at least one of the following: wired network, wireless network. The above wired network can include but is not limited to at least one of the following: wide area network, metropolitan area network, local area network, and the above wireless network can include but is not limited to at least one of the following: WIFI (Wireless Fidelity), Bluetooth. The terminal device 102 can not be limited to PC, mobile phone, tablet computer, smart air conditioner, smart oven, smart refrigerator, smart oven, smart oven, smart washing machine, smart water heater, smart washing equipment, smart dishwasher, smart projection equipment, smart television, smart clothesline, smart curtain, smart audio and video, smart socket, smart sound, smart sound box, smart fresh air equipment, smart kitchen and bathroom equipment, smart bathroom equipment, smart sweeping robot, smart window cleaning robot, smart mopping robot, smart air purification equipment, smart steamer, smart microwave oven, smart kitchen treasure, smart purifier, smart water dispenser, smart door lock, etc.
[0030] In the present embodiment, a file optimization method is provided, which is applied to the above terminal device, Figure 2 is a flowchart of the file optimization method according to the embodiments of the present application, which comprises the following steps:
[0031] Step S202, calling a first preset interface to obtain all workspace folders in the development environment, and filtering all source files meeting the preset extension name requirements from the files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting the first extension name requirements and second type files meeting the second extension name requirements;
[0032] Step S204, reading the text content of each source file in the source file set, and using the parsing engine corresponding to the second type files in the source file set to convert the string representing the text content into a structured data object, wherein the data object is a data collection containing a plurality of data structures, and the plurality of data structures include at least one of the following: a structure composed of key-value pairs, a sequence structure composed of a plurality of elements;
[0033] Step S206, sorting the data object, and writing the sorting result into a target file.
[0034] By the above steps, the first preset interface is called to obtain all workspace folders in the development environment, and all source files meeting the preset extension requirements are screened from the files in each workspace folder to obtain a source file set, wherein the source file set includes first type files meeting first extension requirements and second type files meeting second extension requirements; the text content of each source file in the source file set is read, and a string representing the text content is converted into a structured data object using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection including a plurality of data structures, and the plurality of data structures include at least one of a structure composed of key-value pairs and a sequence structure composed of a plurality of elements; the data object is sorted, and the sorting result is written into a target file. By using the above technical solution, the problems of low development efficiency and difficult debugging caused by the non-standard format and unordered arrangement of key values of JSON files are solved. Further, by calling an interface to collect source files with specified extensions in the development environment, the content of the source files is parsed into structured data objects, the data objects are sorted according to rules and saved to a target file, and the automatic sorting and optimization processing of specific type files are realized.
[0035] In one example embodiment, converting the string representing the text content into a structured data object using the parsing engine corresponding to the second type files in the source file set includes: scanning each character in the string one by one based on the format requirements of the second type files, and identifying a plurality of lexical tokens, wherein the plurality of lexical tokens include at least one of: a brace token representing the start and end of an object, a square bracket token representing the start and end of an array, a colon token representing the separation of key values, and a comma token representing the separation of values; and performing syntax analysis on the plurality of lexical tokens according to the parsing engine to generate a structured data object.
[0036] Optionally, in a typical front-end development project, there is a set of.json5 configuration files, which may contain comments, trailing commas, single-quoted strings, and other JSON5-specific syntax elements. These files are read and converted into JavaScript objects that can be further processed. First, get all the folder paths of the current workspace by calling the workspaceFolders API of VS Code, forming a workspace directory set. Traverse the workspace directory, use the file system (such as the fs module of Node.js) to find all.json5 files, and collect these file paths into a list. For each JSON5 file in the list, use the fs module to read its text content. Then, call the parse method of JSON5, which can intelligently identify and handle JSON5-specific syntax features (comments, single quotes, trailing commas, etc.), and convert the text content into a structured JavaScript object.
[0037] In an example embodiment, the data object is sorted, including: sorting the data object according to a preset sorting rule to obtain a first processing result, wherein the sorting rule includes: sorting based on the dictionary order of the data object key name, sorting based on a preset key value order list; converting the data object based on the first processing result to obtain a second processing result, wherein the conversion processing is to convert the data object into a standard string.
[0038] Optionally, a front-end development project contains multiple.json and.json5 configuration files. These files need to be updated regularly, and in order to maintain code quality and style consistency, the development team decides to use an automated tool to sort the key-value pairs in the files and convert the sorted results into standard string format, which is convenient for later version control and code review. First, define a sorting rule that sorts the key names based on the dictionary order, and define a priority list for important fields with specific semantics (such as id, name, etc.) to ensure that these fields always appear at the beginning of the object. For each read JSON or JSON5 format configuration file, use the above sorting rule to process it to get the first processing result sorted according to the rule. After sorting, use the JSON.stringify() method or the stringify method of the JSON5 library to convert the first processing result into a standard string format. In the conversion process, set appropriate indentation, line break parameters, and escape special characters to generate a second processing result that is both beautiful and standard.
[0039] In an example embodiment, before the data objects are sorted according to the preset sorting rule to obtain the first processing result, the method further comprises: determining whether the data structure of the data objects is an array; in the case that the data structure is an array, sorting each element of the array and performing string comparison on the sorted array elements to arrange the data objects in the correct order; and in the case that the data structure is not an array, sorting the keys of the data objects and the values corresponding to the keys.
[0040] Optionally, it is assumed that a front-end project containing JSON data is being processed, and the JSON file in the project contains various types of data structures, such as arrays and objects. To sort these data structures, first, it is determined whether the data structure type is an array, and in the case that it is an array, each element in the array is sorted, and in the case that the elements in the array are all strings, a string comparison method is used to ensure the accuracy of the sorting. In the case that it is not an array, the keys are sorted according to the key names. Through this sorting method, not only can the elements in the array be effectively sorted to ensure that they are arranged according to the preset rules, but also the key-value pairs of the JSON object can be sorted, so that the overall data structure is more orderly and clear, thereby improving the readability and maintainability of the code.
[0041] In an example embodiment, in the case that the data structure is not an array, the keys of the data objects and the values corresponding to the keys are sorted, which comprises: traversing all nested levels of the data objects and determining all keys in each level object and the values corresponding to each key; performing first sorting on all keys in each level object according to a preset dictionary order, and performing second sorting on the values corresponding to each key in the sorted all keys.
[0042] Optionally, the data structure of the non-array part of the JSON data object is optimized. Specifically, first, each level of the JSON data object is traversed, which ensures that all nested sub-objects are accessed and processed. During the traversal, all key names in each level of the object and the values corresponding to the keys are recorded. Once all the key names of the nested levels are identified, they are sorted according to a predetermined dictionary order. Dictionary order means sorting according to the natural order of letters or characters, such as a preceding b and cat preceding dog in an English environment. This sorting can arrange the key names of the object in order, enhancing the logicality and readability of the data structure. After the first sorting of the keys, the values corresponding to each key name are processed. For each sorted key, if its value is an object, the keys of the sub-object are sorted again using dictionary order; if the value is an array, the elements in the array are sorted according to their types or a predetermined logic. This process is called the second sorting, which aims to ensure that the deep structure of the data also has clear sorting rules, further improving the overall order and manageability of the data.
[0043] In an example embodiment, after the sorted array elements are compared as strings, the above method further comprises: determining whether to adjust the order of the array elements according to the comparison result of the strings; determining to adjust the order of the array elements if the comparison result indicates that the current sorting order of the array elements is inconsistent with the predetermined sorting order; and determining not to adjust the order of the array elements if the comparison result indicates that the current sorting order of the array elements is consistent with the predetermined sorting order.
[0044] Optionally, the relative order of the elements is determined by string comparison, and based on this, it is decided whether the position of the array elements needs to be adjusted. Specifically, first, each element in the array is checked. When the element is an object, one or more keys in the object are selected as the comparison basis, usually those keys that can uniquely or significantly identify the object. For example, if each object in the array has a name key, the value of the name will be used for string comparison. Next, the string comparison method in JavaScript or other programming languages, such as localeCompare, is used to compare the name value of the current element with the name value of the next element (or a specified reference element). This step helps to determine whether the order of the elements meets the predetermined sorting rules, such as dictionary order. Based on the result of the string comparison, it is decided whether the order of the elements needs to be adjusted. If the name value of the current element is in front of the name value of the next element in the dictionary order, the relative position is correct and no adjustment is needed; otherwise, if the name value of the current element is found to be after the name value of the next element after comparison, the positions of the two elements will be exchanged.
[0045] For a better understanding of the process of the above file optimization method, the following will combine optional embodiments to describe the above file optimization method process, but not used to limit the technical solutions of the embodiments of the application.
[0046] In related technologies, there is a lack of plug-ins or tools for comprehensive optimization of JSON and JSON5 files in the VS Code (Visual Studio Code, source code editor) environment, leading to inconsistent formats, poor readability, and low team collaboration efficiency. Current formatting and sorting tools often operate independently and cannot intelligently adapt to the special syntax of JSON5, such as comments, single-quoted strings, and trailing commas, which limits their efficiency and flexibility when processing JSON5 files.
[0047] To solve the above problems, the optional embodiments of the present application propose a JSON file optimization method. This method develops a special plug-in in the VS Code environment, automatically identifies and processes all JSON and JSON5 files in the project directory, realizes deep formatting and intelligent key-value sorting, effectively improves code readability, consistency, and development efficiency, and ensures full compatibility with JSON5 features such as comment support, single quote usage, and trailing comma retention.
[0048] Optionally, JSON (JavaScript Object Notation, JavaScript Object Notation) is a lightweight data exchange format widely used in front-end and back-end data interaction, configuration files, data storage, and other scenarios; JSON5 is a superset of JSON. By introducing some features to extend the syntax of JSON, the JSON format is more flexible and user-friendly in writing, while maintaining compatibility with standard JSON.
[0049] Optionally, the main features of JSON5 compared to JSON include: support for single-line comments ( / / ) and multi-line comments ( / * * / ), which makes the code more readable and maintainable; allows adding a comma after the last element or property of an object or array, which can improve code consistency when multiple people collaborate; not only supports double quotes, but also supports single quotes to wrap strings, increasing flexibility; supports multiple number formats, including octal and hexadecimal literals; can define functions directly in JSON5, which can simplify code in some scenarios.
[0050] Optionally, Figure 3 is a VS Code plug-in implementation process diagram according to an embodiment of the present application, specifically including the following steps:
[0051] Step 1: Initialize the Plugin Project. Initializing the VS Code plugin project is a fundamental setup step, mainly involving determining the plugin's basic information, including key metadata such as project name, description, and publisher. The project name is the plugin's unique identifier, the description concisely summarizes the plugin's functionality and target users, and the publisher information clarifies the plugin's copyright ownership. By correctly configuring this information in the package.json file, you can ensure that the plugin has a clear identity and recognizability during future development, testing, and even release to the market, facilitating communication between developers and users and the plugin's positioning and management.
[0052] Step Two: Install Plugin Dependencies. In VS Code plugin development, installing necessary dependencies is a crucial step to ensure the plugin can be built and run using specific tools and libraries. This includes installing TypeScript, a strongly typed superset of JavaScript, which provides better structure and type safety for plugin code, and `@types / vscode`, the VS Code API (Application Programming Interface) declaration file for TypeScript, enabling plugin developers to enjoy intelligent code completion and error checking within the IDE. Running the command `npm install typescript @types / vscode` automatically adds these dependencies to the plugin project's `package.json` file, supporting subsequent code writing and debugging, and enhancing development efficiency and code quality.
[0053] Step 3: Define Plugin Contribution Points. In VS Code plugin development, "contribution points" refer to the extended or custom functionality that a plugin provides to the editor through specific configuration sections in its package.JSON file. The `contributes` field in package.JSON declares when the plugin is activated when the developer executes a specific command.
[0054] Step 4: Implement the plugin logic, such as... Figure 4 As shown, Figure 4 This is a flowchart of a plug-in logic implementation according to an embodiment of this application, specifically including the following steps:
[0055] Step 1: Obtain all JSON files in the project. Specifically, this includes:
[0056] (1) Get workspace folders using the vscode workspaceFolders API, which allows plugins to access all folder information in the currently open VS Code workspace, including the path and other metadata of the folder.
[0057] (2) Traverse each workspace folder and recursively find files with a JSON (JSON file) or JSON5 (JSON5 file, as there may be JSON5 files in the project) extension and save them to JSONFiles.
[0058] Step 2: Read JSON files and format JSON files. Specifically, it includes:
[0059] (1) Loop through each JSON file (i.e. traverse JSONFiles), denoted as JSONFile.
[0060] (2) Use the fs (File System module) to read the JSON file content, denoted as fileContent.
[0061] (3) Use the parse API of JSON5 to parse the file content. The parse API of JSON5 takes a JSON string as input and returns a JavaScript object.
[0062] Optionally, the parse API is a function interface in a programming language that is used to parse a string of a specific format into a certain data structure (such as an object or array).
[0063] Optionally, the JSON5 library is a JavaScript library that allows parsing and serializing JSON5 files. It is a superset of JSON, adding some features to make it easier to write and maintain by hand, such as comments, trailing commas, and single quotes.
[0064] It should be noted that JSON5 can parse JSON5 and JSON file content. The parse API of the JSON object in JavaScript cannot parse JSON5 file content, because JSON5 file content format is relatively loose (such as containing comments, trailing commas, etc.), so use JSON5 to parse, the parsed content is denoted as parsedObject, which is a JavaScript object, does not contain comments, trailing commas, etc. in JSON5, and is a very JSON standard-compliant content.
[0065] Step 3: Sort the keys of the JSON file. The sorted JSON object is denoted as sortedJSON.
[0066] Optionally, the core logic of sorting defines a function named sortObject that takes an obj parameter (the JSON object to be sorted). The purpose of this function is to sort the incoming JSON object, and the process is as follows:
[0067] (1) First, check if the incoming obj parameter is an array. If it is an array, call the sortObject function on each element of the array (i.e., recursively process the value of each array element), and then use the localeCompare API method to compare the sorted array elements as strings to ensure that the object is arranged in the correct order.
[0068] (2) If the obj parameter is not an array but an object (and not empty), sort the keys of the object, and then iterate through these keys, calling the sortObject function (i.e., recursively) on each key's corresponding value to sort it.
[0069] Optionally, an object is a basic data type in JavaScript that allows the storage of key-value pairs. Key-value pairs are the core component of an object, enabling it to represent complex data structures such as sets, dictionaries, or hash tables. An object is an unordered collection of key-value pairs, where each key is unique and associated with a value. The key is an identifier in the object that uniquely accesses the associated value.
[0070] Optionally, a JSON object is essentially an unordered collection of key-value pairs, which means that the order of properties in a JSON object is not fixed and is not guaranteed to be stored in the order of addition or any specific order. As shown in Figure 5 , the JSON object in Figure 5 is an illustration of a JSON file before sorting according to an embodiment of the present application, as shown in Figure 5 , item, name, and description are keys, and the value of item is an array type. The values of name and description are string types. Figure 5 The content of the JSON object in is unordered, including each item in the array.
[0071] Figure 6 Optionally, the key-value pairs of the JSON object are sorted in dictionary order, with description, item, and name sorted, and each item in the array sorted by discount, id, name, and price. As shown in Figure 6 , the JSON file after sorting according to an embodiment of the present application is illustrated in .
[0072] Step 4: Output JSON file. As shown in Figure 7 , Figure 7 is a JSON file output flowchart according to an embodiment of the present application, which is as follows:
[0073] (1) Create a tmp directory in the workspace to save the processed JSON file, denoted as tempDir.
[0074] (2) Use the stringify API of JSON5 to format the sorted object sortedJSON into a standard JSON string, denoted as sortedJSONString.
[0075] (3) Get the file path of the original JSON file relative to the source directory, denoted as relativePath.
[0076] (4) Construct the target file path according to tempDir and relativePath, denoted as targetFilePath.
[0077] (5) Write the formatted and sorted JSON string (sortedJSONString) to the targetFilePath path.
[0078] Step five: plugin packaging and publishing. The core logic of the plug-in has been developed, and the plug-in is published to the plug-in market. Developers can use this plug-in to achieve configuration file protection by searching and installing this plug-in.
[0079] In summary, by formatting and sorting the JSON file, the present application not only helps to improve code quality, but also improves development efficiency and enhances code maintainability. Specifically, the formatted JSON file has clear structure and reasonable indentation, and the hierarchical relationship of the data can be seen at a glance. The sorted JSON file has properties arranged in alphabetical order or custom order, making it easy to find and modify. The formatted JSON data is easier to find syntax errors and logical errors. The sorted data can more intuitively display the data structure, making it easier to debug. Formatting and sorting can reduce human errors and improve development efficiency. In summary, the present application implements formatting and key sorting protection for JSON files (including JSON5) in engineering, specifically implements JSON file (including JSON5 file) formatting and JSON file (including JSON5 file) key sorting, thereby improving code readability, facilitating debugging and error correction, and improving development efficiency.
[0080] Through the description of the above embodiments, those skilled in the art can clearly understand that the file optimization method according to the above embodiment can be realized by means of software and necessary general hardware platform, and of course, it can also be realized by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software device, which is stored in a storage medium (such as ROM / RAM, disk, optical disk), and includes a plurality of instructions for making a terminal device (which can be a mobile phone, computer, server, or network device, etc.) execute the file optimization method of each embodiment of the present application.
[0081] In the present embodiment, a file optimization device is also provided, which is used to implement the above embodiments and preferred embodiments, and will not be described again. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, hardware, or a combination of software and hardware is also possible and is contemplated.
[0082] Figure 8 is a structural block diagram of a file optimization device according to an embodiment of the present application; as shown in Figure 8 , comprising:
[0083] The acquisition module 82 is configured to call a first preset interface to acquire all workspace folders in a development environment, and filter all source files meeting preset extension name requirements from the files in each workspace folder to obtain a source file set, wherein the source file set includes first type files meeting first extension name requirements and second type files meeting second extension name requirements.
[0084] The conversion module 84 is configured to read text content of each source file in the source file set, and convert a string representing the text content into a structured data object using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection including a plurality of data structures, and the plurality of data structures include at least one of a structure composed of key-value pairs and a sequence structure composed of a plurality of elements.
[0085] The processing module 86 is configured to sort the data object, and write the sorting result into a target file.
[0086] By the above device, the first preset interface is called to obtain all workspace folders in the development environment, and all source files meeting the preset extension name requirement are filtered from the files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting the first extension name requirement and second type files meeting the second extension name requirement; the text content of each source file in the source file set is read, and a string representing the text content is converted into a structured data object by using a parsing engine corresponding to the second type file in the source file set, wherein the data object is a data set containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs and a sequence structure composed of multiple elements; the data object is sorted, and the sorting result is written into a target file. By adopting the above technical solution, the problems of low development efficiency and difficult debugging caused by the non-standard format and unordered key-value arrangement of the JSON file are solved. Further, the source files with specified extensions in the development environment are collected by calling the interface, the content thereof is parsed into a structured data object, and the data object is saved to the target file after being sorted according to the rules, thereby realizing the automatic sorting and optimization processing of the specific type files.
[0087] In an example embodiment, the transformation module is further configured to scan each character in the string one by one based on the format requirement of the second type file, and identify a plurality of lexical tokens, wherein the plurality of lexical tokens include at least one of the following: a brace token representing the start and end of an object, a square bracket token representing the start and end of an array, a colon token representing the separation of a key value, and a comma token representing the separation of a value; and the plurality of lexical tokens are parsed by the parsing engine to generate the structured data object.
[0088] In an example embodiment, the processing module is further configured to sort the data object according to a preset sorting rule to obtain a first processing result, wherein the sorting rule includes: sorting based on the dictionary order of the key name of the data object, and sorting based on a preset key-value order list; and the data object is converted based on the first processing result to obtain a second processing result, wherein the conversion is to convert the data object into a standard string.
[0089] In an example embodiment, the device further includes a determination module configured to, before sorting the data object according to the preset sorting rule to obtain a first processing result, determine whether the data structure of the data object is an array; in the case that the data structure is an array, sort each element of the array and compare the sorted array elements by string to arrange the data object in a correct order; and in the case that the data structure is not an array, sort the keys and the values corresponding to the keys of the data object.
[0090] In an example embodiment, the determining module is further configured to traverse all nested levels of the data object, and determine all keys in each level object and values corresponding to each key; perform first sorting on all keys in each level object according to a preset dictionary order, and perform second sorting on values corresponding to each key in the sorted keys.
[0091] In an example embodiment, the determining module further includes an adjusting unit configured to determine whether to adjust the order of the array elements after the string comparison, according to a comparison result of the strings; determine to adjust the order of the array elements in a case where the comparison result indicates that the sorting order of the current array element is inconsistent with the preset sorting order; and determine not to adjust the order of the array elements in a case where the comparison result indicates that the sorting order of the current array element is consistent with the preset sorting order.
[0092] Embodiments of the present application also provide a storage medium including a stored program, wherein the program performs any of the above file optimization methods when executed.
[0093] Optionally, in the present embodiment, the storage medium can be configured to store program code for performing the following steps:
[0094] S1, calling a first preset interface to obtain all workspace folders in a development environment, and screening all source files meeting preset extension requirements from files in each workspace folder to obtain a source file set, wherein the source file set includes first type files meeting first extension requirements and second type files meeting second extension requirements;
[0095] S2, reading text content of each source file in the source file set, and converting a string representing the text content into a structured data object using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection including multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of multiple elements;
[0096] S3, performing sorting processing on the data object, and writing a sorting processing result into a target file.
[0097] Embodiments of the present application also provide an electronic device including a memory and a processor, the memory storing a computer program, and the processor being configured to run the computer program to perform steps in any of the above method embodiments.
[0098] The embodiment of the present application further provides a computer program product comprising a computer program which, when executed by a processor, performs the steps of any of the method embodiments described above.
[0099] Optionally, the electronic device described above can further comprise a transmission device connected with the processor and an input and output device connected with the processor.
[0100] Optionally, in the embodiment, the processor can be configured to perform the following steps by using a computer program:
[0101] S1, calling a first preset interface to obtain all workspace folders in a development environment, and screening all source files meeting preset extension name requirements from files in each workspace folder to obtain a source file set, wherein the source file set comprises first type files meeting first extension name requirements and second type files meeting second extension name requirements;
[0102] S2, reading text content of each source file in the source file set, and converting a string representing the text content into a structured data object by using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data set comprising a plurality of data structures, and the plurality of data structures comprise at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of a plurality of elements.
[0103] S3, performing sorting processing on the data object, and writing a sorting processing result into a target file.
[0104] Optionally, in the embodiment, the storage medium can comprise, but is not limited to, a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk and various storage program codes.
[0105] Optionally, specific examples in the embodiment can refer to examples described in the above embodiments and optional implementation manners, and the embodiment will not be described here again.
[0106] In the technical solution of the present application, the collection, storage, use, processing, transmission, provision and disclosure of information such as financial data or user data comply with relevant laws and regulations and do not violate public order and good customs.
[0107] It should be noted that in the embodiments of the present application, some software, components, models and other prior art solutions can be mentioned, which should be considered as exemplary, and the purpose is only to illustrate the feasibility of the implementation of the technical solutions of the present application, but it does not mean that the applicant has or will necessarily use the solution.
[0108] Obviously, those skilled in the art should understand that the above-mentioned modules or steps of the present application can be realized by a general computing device, which can be concentrated on a single computing device or distributed on a network composed of multiple computing devices, and optionally, they can be realized by program codes executable by the computing device, so that they can be stored in the storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in different order, or they can be made into individual integrated circuit modules, or multiple modules or steps can be made into a single integrated circuit module. Thus, the present application is not limited to any specific hardware and software combination.
[0109] The above is only the preferred embodiment of the present application, and it should be pointed out that for those skilled in the art, some improvements and refinements can be made without departing from the principles of the present application, and these improvements and refinements should also be considered as the protection scope of the present application.
Claims
1. A method of file optimization, characterized by, The method comprises the following steps: calling a first preset interface to obtain all workspace folders in a development environment, and screening all source files meeting preset extension requirements from files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting first extension requirements and second type files meeting second extension requirements; reading text content of each source file in the source file set, and converting a string representing the text content into a structured data object using a parsing engine corresponding to the second type files in the source file set, wherein the data object is a data collection containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs and a sequence structure composed of multiple elements; sorting the data object and writing a sorting result into a target file.
2. The file optimization method of claim 1, wherein, Converting a string representing the text content into a structured data object using a parsing engine corresponding to the second type files in the source file set comprises: scanning each character in the string one by one based on a format requirement of the second type files to identify a plurality of lexical tokens, wherein the plurality of lexical tokens include at least one of the following: a brace token representing the start and end of an object, a square bracket token representing the start and end of an array, a colon token representing the separation of a key and a value, and a comma token representing the separation of values; performing syntax analysis on the plurality of lexical tokens according to the parsing engine to generate the structured data object.
3. The file optimization method of claim 1, wherein, Sorting the data object comprises: sorting the data object according to a preset sorting rule to obtain a first processing result, wherein the sorting rule includes sorting based on a dictionary order of key names of the data object and sorting based on a preset key-value order list; performing conversion processing on the data object based on the first processing result to obtain a second processing result, wherein the conversion processing is to convert the data object into a standard string.
4. The file optimization method of claim 3, wherein, Before sorting the data object according to a preset sorting rule to obtain a first processing result, the method further comprises: determining whether a data structure of the data object is an array; in a case where the data structure is an array, sorting each element of the array and comparing the sorted array elements as strings to arrange the data object in a correct order; in a case where the data structure is not an array, sorting keys of the data object and values corresponding to the keys.
5. The file optimization method of claim 4, wherein, In a case where the data structure is not an array, sorting the keys of the data object and the values corresponding to the keys comprises: traversing all nested levels of the data object and determining all keys in each level object and values corresponding to each key; performing first sorting on all keys in each level object according to a preset dictionary order and performing second sorting on values corresponding to the sorted keys.
6. The file optimization method of claim 4, wherein, After comparing the sorted array elements as strings, the method further comprises: determining whether to adjust the order of the array elements according to a comparison result of the strings; determining to adjust the order of the array elements in a case where the comparison result indicates that the sorting order of the current array elements is inconsistent with the preset sorting order; determining not to adjust the order of the array elements in a case where the comparison result indicates that the sorting order of the current array elements is consistent with the preset sorting order.
7. A file optimization apparatus characterized by comprising: comprising: an acquisition module, configured to call a first preset interface to acquire all workspace folders in a development environment, and to filter all source files meeting preset extension requirements from files in each workspace folder to obtain a source file set, wherein the source file set contains first type files meeting first extension requirements and second type files meeting second extension requirements; a conversion module, configured to read text content of each source file in the source file set, and to convert strings representing the text content into structured data objects using a parsing engine corresponding to the second type files in the source file set, wherein the data objects are data sets containing multiple data structures, and the multiple data structures include at least one of the following: a structure composed of key-value pairs, and a sequence structure composed of multiple elements; a processing module, configured to perform sorting processing on the data objects, and to write a sorting processing result into a target file.
8. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, wherein the computer program is executed by the processor to implement the steps of the file optimization method in any one of claims 1 to 6.
9. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the computer program to implement the steps of the file optimization method in any one of claims 1 to 6.
10. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the file optimization method in any one of claims 1 to 6.