[go: up one dir, main page]

CN111736842B - Method and device for realizing rapid conversion of JSON into JavaBean - Google Patents

Method and device for realizing rapid conversion of JSON into JavaBean Download PDF

Info

Publication number
CN111736842B
CN111736842B CN202010470185.8A CN202010470185A CN111736842B CN 111736842 B CN111736842 B CN 111736842B CN 202010470185 A CN202010470185 A CN 202010470185A CN 111736842 B CN111736842 B CN 111736842B
Authority
CN
China
Prior art keywords
javabean
code
json
conversion
class
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010470185.8A
Other languages
Chinese (zh)
Other versions
CN111736842A (en
Inventor
高瑞胜
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Suzhou Metabrain Intelligent Technology Co Ltd
Original Assignee
Suzhou Inspur Intelligent Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Suzhou Inspur Intelligent Technology Co Ltd filed Critical Suzhou Inspur Intelligent Technology Co Ltd
Priority to CN202010470185.8A priority Critical patent/CN111736842B/en
Publication of CN111736842A publication Critical patent/CN111736842A/en
Application granted granted Critical
Publication of CN111736842B publication Critical patent/CN111736842B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)
  • Stored Programmes (AREA)

Abstract

The invention provides a method and a device for realizing rapid conversion of JSON into JavaBean.A compiling and processing program carries out pretreatment before compiling and packaging the program, automatically generates a JSON conversion function for each JavaBean, and rapidly converts the JSON into the JavaBean; the method specifically comprises the following steps: newly building a JsonUtils code file; reading the class name corresponding to each JavaBean according to the configuration file; traversing each JavaBean class name, and generating a code of a JSON conversion function for each JavaBean; and adding the code of the JSON conversion function into a newly-built JsonUtils class code file to serve as a member function of the JsonUtils class.

Description

一种实现JSON快速转化为JavaBean方法、装置A method and device for quickly converting JSON into JavaBean

技术领域technical field

本发明涉及节约内存的存储管理软件缓存数据技术领域,具体涉及实现一种JSON快速转化为JavaBean方法、装置。The invention relates to the technical field of memory-saving storage management software caching data, in particular to a method and a device for quickly converting JSON into JavaBean.

背景技术Background technique

当今社会互联网高速发展,使得WEB应用非常广泛,涉及我们生活的各个方面,据不完全统计,80%的WEB应用使用JSON作为前端与后端Java 的交互数据。The rapid development of the Internet in today's society makes WEB applications very extensive, involving all aspects of our lives. According to incomplete statistics, 80% of WEB applications use JSON as the interactive data between front-end and back-end Java.

当前JSON转化为JavaBean的方法一般是根据JSON对象各个字段名,找到对应的Setter方法,利用Java反射机制,调用对应的Setter方法,达到为JavaBean字段赋值的目的。The current method of converting JSON to JavaBean is generally to find the corresponding Setter method based on each field name of the JSON object, and use the Java reflection mechanism to call the corresponding Setter method to achieve the purpose of assigning a value to the JavaBean field.

然而,利用Java反射机制调用Setter方法性能较差,与直接调用Setter 方法相比,甚至可能会慢几十倍,这就导致JSON转JavaBean性能降低。However, the performance of using the Java reflection mechanism to call the Setter method is poor, and it may even be dozens of times slower than calling the Setter method directly, which leads to a decrease in the performance of converting JSON to JavaBean.

发明内容Contents of the invention

针对利用Java反射机制调用Setter方法性能较差,与直接调用Setter 方法相比,甚至可能会慢几十倍,这就导致JSON转JavaBean性能降低的问题,本发明提供一种实现JSON快速转化为JavaBean方法、装置。Aiming at the poor performance of using the Java reflection mechanism to call the Setter method, compared with directly calling the Setter method, it may even be several tens of times slower, which leads to the problem that the performance of converting JSON to JavaBean is reduced. The present invention provides a method for quickly converting JSON into JavaBean method, device.

本发明的技术方案包括:Technical scheme of the present invention comprises:

一方面,本发明技术方案提供一种实现JSON快速转化为JavaBean方法,编写与处理程序在程序编译打包之前做预处理,为每个JavaBean自动生成JSON转化函数,将JSON快速转化为JavaBean;具体包括如下步骤:On the one hand, the technical solution of the present invention provides a method for quickly converting JSON into JavaBean, writing and processing programs are preprocessed before the program is compiled and packaged, and a JSON conversion function is automatically generated for each JavaBean, and JSON is quickly converted into JavaBean; specifically includes Follow the steps below:

新建JsonUtils类代码文件;Create a new JsonUtils class code file;

根据配置文件,读取每个JavaBean对应的类名;According to the configuration file, read the class name corresponding to each JavaBean;

遍历每一个JavaBean类名,为每个JavaBean生成JSON转化函数的代码;Traverse each JavaBean class name, and generate the code of the JSON conversion function for each JavaBean;

将JSON转化函数的代码添加到新建JsonUtils类代码文件,使其作为 JsonUtils类的成员函数。Add the code of the JSON conversion function to the new JsonUtils class code file, making it a member function of the JsonUtils class.

进一步的,所述的遍历每一个JavaBean类名,为每个JavaBean生成 JSON转化函数的代码的步骤包括:Further, the steps of traversing each JavaBean class name and generating the code of the JSON conversion function for each JavaBean include:

检查是否存在未处理的JavaBean,若否,则结束;否则,每次取一个未处理的JavaBean,利用Java反射技术,使用JavaBean类名,生成对应的 Class对象;Check whether there is an unprocessed JavaBean, if not, end; otherwise, take an unprocessed JavaBean each time, use Java reflection technology, use the JavaBean class name, and generate the corresponding Class object;

使用Class对象,生成JSON转化为对应JavaBean的函数代码;Use the Class object to generate JSON and convert it into the function code corresponding to JavaBean;

直至将未处理的JavaBean处理完成。Until the unprocessed JavaBean is processed.

进一步的,根据配置文件,读取每个JavaBean对应的类名的步骤包括:Further, according to the configuration file, the steps of reading the class name corresponding to each JavaBean include:

检测JavaBean所处位置,若JavaBean均在设定的目录下,将设定的目录写入配置文件,通过遍历这些目录下的所有JavaBean文件即可获取 JavaBean类名;否则,将JavaBean对应的类名写入配置文件。Detect the location of the JavaBean, if the JavaBean is in the set directory, write the set directory into the configuration file, and get the JavaBean class name by traversing all the JavaBean files in these directories; otherwise, write the class name corresponding to the JavaBean Write configuration file.

进一步的,所述的遍历每一个JavaBean类名,为每个JavaBean生成 JSON转化函数的代码的步骤中,生成JSON转化函数的代码的步骤包括:Further, in the step of traversing each JavaBean class name and generating the code of the JSON conversion function for each JavaBean, the step of generating the code of the JSON conversion function includes:

Step51:将JSON转化为Map对象的代码添加到第一函数体中;Step51: Add the code for converting JSON into a Map object to the first function body;

Step52:将新建空第一对象的代码添加到第一函数体中;Step52: Add the code for creating a new empty first object to the body of the first function;

Step53:利用Java反射,获取第一对象的所有成员变量及成员变量类型;Step53: Use Java reflection to obtain all member variables and member variable types of the first object;

Step54:检查是否存在未处理过的成员变量,如果不存在,则结束;否则获取一个成员变量及其类型,转到下一步;Step54: Check whether there is an unprocessed member variable, if not, end; otherwise get a member variable and its type, go to the next step;

Step55:根据成员变量类型生成转换代码并将代码添加到第一函数体中,执行步骤Step54。Step55: Generate conversion code according to the type of the member variable and add the code to the first function body, and execute Step54.

进一步的,Step55的步骤具体包括:Further, the steps of Step55 specifically include:

Step551:若成员变量类型为基本类型或其封装类,则将如下代码添加到第一函数体中:obj.set${member}(to${type}(jsonMap.get(${member})));Step551: If the member variable type is a basic type or its encapsulation class, add the following code to the first function body: obj.set${member}(to${type}(jsonMap.get(${member})) );

其中${member}为成员变量名,${type}为其类型名;转到下一步;Among them, ${member} is the member variable name, and ${type} is the type name; go to the next step;

Step552:若成员变量类型为JavaBean,则将如下代码添加到第一函数体中:jsonTo${type}((jsonMap.get(${member}));转到下一步;Step552: If the member variable type is JavaBean, add the following code to the first function body: jsonTo${type}((jsonMap.get(${member})); go to the next step;

Step553:若成员变量类型是数组或ArrayList,则将数组或ArrayList的每个成员转化为基本类型或JavaBean,组成新数组或ArrayList,然后调用成员变量的Setter方法进行赋值,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到下一步;Step553: If the type of the member variable is an array or ArrayList, convert each member of the array or ArrayList into a basic type or JavaBean to form a new array or ArrayList, and then call the Setter method of the member variable to assign a value, refer to Step551 and Step552 to generate the conversion code, and add it to the body of the first function; go to the next step;

Step554:若成员变量类型是HashMap,则分别将key与value的JSON 转化为基本类型或JavaBean,将其加入新的HashMap,然后调用成员变量的Setter方法进行赋值,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到下一步;Step554: If the type of the member variable is HashMap, convert the JSON of the key and value into the basic type or JavaBean respectively, add it to the new HashMap, and then call the Setter method of the member variable to assign the value, refer to Step551 and Step552 to generate the conversion code, and added to the first function body; go to the next step;

Step555:若成员变量是其它类型,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到Step54。Step555: If the member variable is of other types, refer to Step551 and Step552 to generate conversion code, and add it to the first function body; go to Step54.

进一步的,所述的第一函数体包括jsonTo${JavaBean}函数体;Further, the first function body includes the jsonTo${JavaBean} function body;

第一对象包括${JavaBean}对象。The first object includes the ${JavaBean} object.

另一方面,本发明技术方案提供一种实现JSON快速转化为JavaBean 装置,包括文件建立模块、类名读取模块、JSON转化函数的代码生成模块、代码文件生成模块;On the other hand, the technical scheme of the present invention provides a kind of realization JSON and is converted into JavaBean device rapidly, comprises the code generation module of file establishment module, class name reading module, JSON conversion function, code file generation module;

文件建立模块,用于新建JsonUtils类代码文件;File creation module, used to create new JsonUtils class code files;

类名读取模块,用于根据配置文件,读取每个JavaBean对应的类名;The class name reading module is used to read the class name corresponding to each JavaBean according to the configuration file;

JSON转化函数的代码生成模块,用于遍历每一个JavaBean类名,为每个JavaBean生成JSON转化函数的代码;The code generation module of the JSON conversion function is used to traverse each JavaBean class name and generate the code of the JSON conversion function for each JavaBean;

代码文件生成模块,用于将JSON转化函数的代码添加到新建JsonUtils 类代码文件,使其作为JsonUtils类的成员函数。The code file generation module is used to add the code of the JSON conversion function to the newly created JsonUtils class code file, making it a member function of the JsonUtils class.

进一步的,该装置还包括检查模块和Class对象生成模块;Further, the device also includes a check module and a Class object generation module;

所述检查模块,用于检查是否存在未处理的JavaBean,若否,则结束;The checking module is used to check whether there are unprocessed JavaBeans, if not, then end;

Class对象生成模块,利用Java反射技术,使用JavaBean类名,生成对应的Class对象;The Class object generation module uses Java reflection technology to use the JavaBean class name to generate the corresponding Class object;

JSON转化函数的代码生成模块,用于使用Class对象,生成JSON转化为对应JavaBean的函数代码。The code generation module of the JSON conversion function is used to use Class objects to generate JSON conversion function codes corresponding to JavaBean.

进一步的,类名读取模块,具体用于检测JavaBean所处位置,若 JavaBean均在设定的目录下,将设定的目录写入配置文件,通过遍历这些目录下的所有JavaBean文件即可获取JavaBean类名;否则,将JavaBean 对应的类名写入配置文件。Further, the class name reading module is specifically used to detect the location of the JavaBean. If the JavaBean is in the set directory, write the set directory into the configuration file, and it can be obtained by traversing all the JavaBean files in these directories. JavaBean class name; otherwise, write the class name corresponding to JavaBean into the configuration file.

进一步的,JSON转化函数的代码生成模块包括转化单元、添加单元、获取单元、检查单元、转换代码生成单元;Further, the code generation module of the JSON conversion function includes a conversion unit, an addition unit, an acquisition unit, a check unit, and a conversion code generation unit;

转化单元,用于将JSON转化为Map对象的代码添加到第一函数体中;A conversion unit, for adding the code for converting JSON into a Map object into the first function body;

添加单元,用于将新建空第一对象的代码添加到第一函数体中;Adding a unit for adding the code for creating a new empty first object into the first function body;

获取单元,用于利用Java反射,获取第一对象的所有成员变量及成员变量类型;The obtaining unit is used to obtain all member variables and member variable types of the first object by using Java reflection;

检查单元,用于检查是否存在未处理过的成员变量,如果不存在,则结束;否则获取一个成员变量及其类型,转到下一步;The checking unit is used to check whether there is an unprocessed member variable, if not, then end; otherwise, get a member variable and its type, and go to the next step;

转换代码生成单元,用于根据成员变量类型生成转换代码并将代码添加到第一函数体中。A conversion code generating unit, configured to generate conversion codes according to the type of the member variable and add the codes to the first function body.

本发明在编译前利用Java反射机制,自动生成JSON转JavaBean方法,在方法中直接调用JavaBean各成员的Setter方法达到JSON转JavaBean 的目的,实现了一种JSON快速转化为JavaBean方法。The invention utilizes the Java reflection mechanism before compiling to automatically generate a JSON-to-JavaBean method, directly calls the Setter method of each member of the JavaBean in the method to achieve the purpose of converting JSON to JavaBean, and realizes a method for quickly converting JSON to JavaBean.

从以上技术方案可以看出,本发明具有以下优点:在编译前利用Java反射机制,自动生成JSON转JavaBean方法,在方法中直接调用JavaBean 各成员的Setter方法达到JSON转JavaBean的目的;因为客户使用的产品均是编译后的,因此占用编译时间,对用户没有任何影响。As can be seen from the above technical scheme, the present invention has the following advantages: before compiling, the Java reflection mechanism is used to automatically generate the JSON to JavaBean method, and the Setter method of each member of the JavaBean is directly called in the method to achieve the purpose of JSON to JavaBean; because the client uses All products are compiled, so it takes up compilation time and has no impact on users.

此外,本发明设计原理可靠,结构简单,具有非常广泛的应用前景。In addition, the design principle of the present invention is reliable, the structure is simple, and has very wide application prospects.

由此可见,本发明与现有技术相比,具有突出的实质性特点和显著地进步,其实施的有益效果也是显而易见的。It can be seen that, compared with the prior art, the present invention has outstanding substantive features and remarkable progress, and the beneficial effects of its implementation are also obvious.

附图说明Description of drawings

为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,对于本领域普通技术人员而言,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings that need to be used in the description of the embodiments or the prior art. Obviously, for those of ordinary skill in the art, In other words, other drawings can also be obtained from these drawings on the premise of not paying creative work.

图1是本发明实施例提供的方法的示意性流程图。Fig. 1 is a schematic flowchart of a method provided by an embodiment of the present invention.

图2是本发明实施例提供的方法的另一个示意性流程图。Fig. 2 is another schematic flowchart of the method provided by the embodiment of the present invention.

具体实施方式Detailed ways

为了使本技术领域的人员更好地理解本发明中的技术方案,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都应当属于本发明保护的范围。In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described The embodiments are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by persons of ordinary skill in the art without making creative efforts shall fall within the protection scope of the present invention.

如图1所示,本发明实施例提供一种实现JSON快速转化为JavaBean 方法,编写与处理程序在程序编译打包之前做预处理,为每个JavaBean自动生成JSON转化函数,将JSON快速转化为JavaBean;具体包括如下步骤:As shown in Fig. 1, the embodiment of the present invention provides a kind of method that realizes that JSON is quickly transformed into JavaBean, writes and handles program and does preprocessing before program compiling and packaging, automatically generates JSON transformation function for each JavaBean, and JSON is rapidly transformed into JavaBean ; Concretely include the following steps:

Step1:新建JsonUtils类代码文件JsonUtils.java;新建JsonUtils.java, 定义JsonUtils类,可以没有任何成员或函数,也可根据具体应用或程序自由发挥;Step1: Create a new JsonUtils class code file JsonUtils.java; create a new JsonUtils.java, and define the JsonUtils class, which may not have any members or functions, or can be used freely according to specific applications or programs;

Step2:根据配置文件,读取每个JavaBean对应的类名;例如 com.gui.TestBean;在Step2中,如果JavaBean所处位置比较集中,在均在某些目录下,则可将这些目录写入配置文件,通过遍历这些目录下的所有 JavaBean文件即可获取JavaBean类名;如果JavaBean所处位置比较分散,可将JavaBean对应的类名写入配置文件。在实际应用中,一般为第一种情况;Step2: According to the configuration file, read the class name corresponding to each JavaBean; for example, com.gui.TestBean; in Step2, if the location of the JavaBean is relatively concentrated, all in certain directories, you can write these directories Configuration file, the JavaBean class name can be obtained by traversing all the JavaBean files in these directories; if the location of the JavaBean is scattered, the class name corresponding to the JavaBean can be written into the configuration file. In practical applications, it is generally the first case;

Step3:遍历每一个JavaBean类名,为每个JavaBean生成JSON转化函数的代码,包括如下步骤:Step3: traverse each JavaBean class name, and generate the code of the JSON conversion function for each JavaBean, including the following steps:

Step31:检查是否存在未处理的JavaBean,若否,则结束;否则,每次取一个未处理的JavaBean,转到Step32;Step31: Check whether there is an unprocessed JavaBean, if not, end; otherwise, take an unprocessed JavaBean each time and go to Step32;

Step32:利用Java反射技术,使用JavaBean类名,生成对应的Class 对象;Step32: Using Java reflection technology, use the JavaBean class name to generate the corresponding Class object;

Step33:使用Class对象,生成JSON转化为对应JavaBean的函数代码;使用Class对象,生成JSON转化为此JavaBean的函数“public static ${JavaBean}jsonTo${JavaBean}(String json)”的代码,其中${JavaBean}表示不带包路径的JavaBean类名。例如类名“TestBean”,JSON转化为此 JavaBean的函数为“public static TestBeanjsonToTestBean(String json)”。转到下一步Step4;Step33: Use the Class object to generate JSON and convert it to the function code of the corresponding JavaBean; use the Class object to generate JSON and convert it to the code of the function "public static ${JavaBean}jsonTo${JavaBean}(String json)" of this JavaBean, where $ {JavaBean} represents the JavaBean class name without the package path. For example, if the class name is "TestBean", the function to convert JSON to this JavaBean is "public static TestBeanjsonToTestBean(String json)". Go to the next step Step4;

Step4:将JSON转化函数的代码添加到新建JsonUtils类代码文件,使其作为JsonUtils类的成员函数。将上一步生成的JSON转化为此JavaBean 函数的代码写入JsonUtils.java,使其作为JsonUtils的成员函数。转到Step31。Step4: Add the code of the JSON conversion function to the newly created JsonUtils class code file, making it a member function of the JsonUtils class. Convert the JSON generated in the previous step into the code of this JavaBean function and write it into JsonUtils.java, making it a member function of JsonUtils. Go to Step31.

需要生成函数“jsonTo${JavaBean}”的代码,具体生成过程如下:The code of the function "jsonTo${JavaBean}" needs to be generated, and the specific generation process is as follows:

Step51:将JSON转化为Map对象的代码添加到第一函数体中;将“json 转化为Map对象”的代码添加到“jsonTo${JavaBean}”函数体中,记Map对象变量名为jsonMap。json转化为Map是容易的,本实施例不具体说明;Step51: Add the code for converting JSON into a Map object to the first function body; add the code for "converting json into a Map object" into the "jsonTo${JavaBean}" function body, and record the Map object variable name as jsonMap. It is easy to convert json into Map, which is not specified in this embodiment;

Step52:将新建空第一对象的代码添加到第一函数体中;需要说明的是,将“新建空${JavaBean}对象”的代码添加到“jsonTo${JavaBean}”函数体中;Step52: Add the code of creating a new empty first object to the first function body; it should be noted that the code of "creating a new empty ${JavaBean} object" is added to the "jsonTo${JavaBean}" function body;

Step53:利用Java反射,获取${JavaBean}对象的所有成员变量及成员变量类型;Step53: Use Java reflection to obtain all member variables and member variable types of the ${JavaBean} object;

Step54:检查是否存在未处理过的成员变量,如果不存在,则结束;否则获取一个成员变量及其类型,转到下一步;Step54: Check whether there is an unprocessed member variable, if not, end; otherwise get a member variable and its type, go to the next step;

Step55:根据成员变量类型生成转换代码并将代码添加到第一函数体中,执行步骤Step54。需要说明的是,本步骤具体包括:Step55: Generate conversion code according to the type of the member variable and add the code to the first function body, and execute Step54. It should be noted that this step specifically includes:

Step551:若成员变量类型为基本类型或其封装类,boolea、int、float、 double、char、Boolean、Integer、Float、Double、Character、String,则将如下代码添加到“jsonTo${JavaBean}”函数体中:Step551: If the member variable type is a basic type or its encapsulation class, boolea, int, float, double, char, Boolean, Integer, Float, Double, Character, String, add the following code to the "jsonTo${JavaBean}" function In the body:

obj.set${member}(to${type}(jsonMap.get(${member})));obj.set${member}(to${type}(jsonMap.get(${member})));

其中${member}为成员变量名,${type}为其类型名;to${type}是容易实现的通过现有技术,本实施例不再具体描述;转到下一步;Among them, ${member} is the member variable name, ${type} is the type name; to${type} is easy to implement through the existing technology, and this embodiment will not describe it in detail; go to the next step;

Step552:若成员变量类型为JavaBean,则将如下代码添加到“jsonTo${JavaBean}”函数体中:Step552: If the member variable type is JavaBean, add the following code to the "jsonTo${JavaBean}" function body:

jsonTo${type}((jsonMap.get(${member}));转到下一步;jsonTo${type}((jsonMap.get(${member})); Go to the next step;

Step553:若成员变量类型是数组或ArrayList,则将数组或ArrayList的每个成员转化为基本类型或JavaBean,组成新数组或ArrayList,然后调用成员变量的Setter方法进行赋值,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到下一步;Step553: If the type of the member variable is an array or ArrayList, convert each member of the array or ArrayList into a basic type or JavaBean to form a new array or ArrayList, and then call the Setter method of the member variable to assign a value, refer to Step551 and Step552 to generate the conversion code, and add it to the body of the first function; go to the next step;

Step554:若成员变量类型是HashMap,则分别将key与value的JSON 转化为基本类型或JavaBean,将其加入新的HashMap,然后调用成员变量的Setter方法进行赋值,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到下一步;Step554: If the type of the member variable is HashMap, convert the JSON of the key and value into the basic type or JavaBean respectively, add it to the new HashMap, and then call the Setter method of the member variable to assign the value, refer to Step551 and Step552 to generate the conversion code, and added to the first function body; go to the next step;

Step555:若成员变量是其它类型,参照步骤Step551和Step552生成转化代码,并添加到第一函数体中;转到Step54。Step555: If the member variable is of other types, refer to Step551 and Step552 to generate conversion code, and add it to the first function body; go to Step54.

本实施例给出了“jsonTo${JavaBean}”函数的一个简写示例,未包含异常处理;This embodiment gives a shorthand example of the "jsonTo${JavaBean}" function, which does not include exception handling;

Figure BDA0002514056690000101
Figure BDA0002514056690000101

Figure BDA0002514056690000111
Figure BDA0002514056690000111

Figure BDA0002514056690000121
Figure BDA0002514056690000121

Figure BDA0002514056690000131
Figure BDA0002514056690000131

本发明实施例还提供一种实现JSON快速转化为JavaBean装置,包括文件建立模块、类名读取模块、JSON转化函数的代码生成模块、代码文件生成模块、检查模块和Class对象生成模块;The embodiment of the present invention also provides a kind of realization JSON and is converted into JavaBean device rapidly, comprises the code generation module of file creation module, class name reading module, JSON conversion function, code file generation module, inspection module and Class object generation module;

文件建立模块,用于新建JsonUtils类代码文件;File creation module, used to create new JsonUtils class code files;

类名读取模块,用于根据配置文件,读取每个JavaBean对应的类名;具体用于检测JavaBean所处位置,若JavaBean均在设定的目录下,将设定的目录写入配置文件,通过遍历这些目录下的所有JavaBean文件即可获取 JavaBean类名;否则,将JavaBean对应的类名写入配置文件。The class name reading module is used to read the class name corresponding to each JavaBean according to the configuration file; it is specifically used to detect the location of the JavaBean, and if the JavaBean is in the set directory, write the set directory into the configuration file , the JavaBean class name can be obtained by traversing all the JavaBean files in these directories; otherwise, write the class name corresponding to the JavaBean into the configuration file.

所述检查模块,用于检查是否存在未处理的JavaBean,若否,则结束;The checking module is used to check whether there are unprocessed JavaBeans, if not, then end;

Class对象生成模块,利用Java反射技术,使用JavaBean类名,生成对应的Class对象;The Class object generation module uses Java reflection technology to use the JavaBean class name to generate the corresponding Class object;

JSON转化函数的代码生成模块,用于使用Class对象,生成JSON转化为对应JavaBean的函数代码,需要说明的是,JSON转化函数的代码生成模块包括转化单元、添加单元、获取单元、检查单元、转换代码生成单元;The code generation module of the JSON conversion function is used to use the Class object to generate the JSON conversion function code corresponding to the JavaBean. It should be noted that the code generation module of the JSON conversion function includes the conversion unit, the addition unit, the acquisition unit, the inspection unit, and the conversion code generation unit;

转化单元,用于将JSON转化为Map对象的代码添加到第一函数体中;A conversion unit, for adding the code for converting JSON into a Map object into the first function body;

添加单元,用于将新建空第一对象的代码添加到第一函数体中;Adding a unit for adding the code for creating a new empty first object into the first function body;

获取单元,用于利用Java反射,获取第一对象的所有成员变量及成员变量类型;The obtaining unit is used to obtain all member variables and member variable types of the first object by using Java reflection;

检查单元,用于检查是否存在未处理过的成员变量,如果不存在,则结束;否则获取一个成员变量及其类型,转到下一步;The checking unit is used to check whether there is an unprocessed member variable, if not, then end; otherwise, get a member variable and its type, and go to the next step;

转换代码生成单元,用于根据成员变量类型生成转换代码并将代码添加到第一函数体中。A conversion code generating unit, configured to generate conversion codes according to the type of the member variable and add the codes to the first function body.

代码文件生成模块,用于将JSON转化函数的代码添加到新建JsonUtils 类代码文件,使其作为JsonUtils类的成员函数。The code file generation module is used to add the code of the JSON conversion function to the newly created JsonUtils class code file, making it a member function of the JsonUtils class.

尽管通过参考附图并结合优选实施例的方式对本发明进行了详细描述,但本发明并不限于此。在不脱离本发明的精神和实质的前提下,本领域普通技术人员可以对本发明的实施例进行各种等效的修改或替换,而这些修改或替换都应在本发明的涵盖范围内/任何熟悉本技术领域的技术人员在本发明揭露的技术范围内,可轻易想到变化或替换,都应涵盖在本发明的保护范围之内。因此,本发明的保护范围应以所述权利要求的保护范围为准。Although the present invention has been described in detail in conjunction with preferred embodiments with reference to the accompanying drawings, the present invention is not limited thereto. Without departing from the spirit and essence of the present invention, those skilled in the art can make various equivalent modifications or replacements to the embodiments of the present invention, and these modifications or replacements should be within the scope of the present invention/any Those skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present invention, and all should be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention should be determined by the protection scope of the claims.

Claims (8)

1. A method for realizing rapid conversion of JSON into JavaBean is characterized in that a compiling and processing program carries out preprocessing before compiling and packaging the program, a JSON conversion function is automatically generated for each JavaBean, and JSON is rapidly converted into JavaBean; the method specifically comprises the following steps:
newly building a JsonUtils code file;
reading the class name corresponding to each JavaBean according to the configuration file;
traversing each JavaBean class name, and generating a code of a JSON conversion function for each JavaBean; the method comprises the following steps: step51: adding a code for converting JSON into a Map object into a first function body; step52: adding the code of the new empty first object into the first function body; step53: acquiring all member variables and member variable types of the first object by using Java reflection; step54: checking whether the unprocessed member variable exists or not, and if not, ending; otherwise, acquiring a member variable and the type thereof, and turning to the next step; step55: generating a conversion code according to the member variable type, adding the code into the first function body, and executing Step54; and adding the code of the JSON conversion function into a newly-built JsonUtils class code file to be used as a member function of the JsonUtils class.
2. The method as claimed in claim 1, wherein the step of traversing each JavaBean class name and generating the code of the JSON conversion function for each JavaBean comprises:
checking whether unprocessed JavaBean exists or not, and if not, ending; otherwise, one unprocessed JavaBean is taken each time, and a corresponding Class object is generated by using the Java reflection technology and the JavaBean Class name;
generating a JSON (Java object notation) by using a Class object, and converting the JSON into a function code corresponding to the JavaBean;
until the unprocessed JavaBean processing is completed.
3. The method for realizing the rapid JSON conversion into JavaBean according to claim 1, wherein the step of reading the class name corresponding to each JavaBean according to the configuration file comprises:
detecting the position of the JavaBean, writing the set directory into a configuration file if the JavaBean is under the set directory, and traversing all JavaBean files under the directories to obtain the name of the JavaBean class; otherwise, writing the class name corresponding to the JavaBean into the configuration file.
4. The method for realizing the rapid conversion of JSON into JavaBean according to claim 1, wherein the Step of Step55 specifically comprises:
step551: if the member variable type is a basic type or an encapsulation type thereof, adding the following codes into the first function body: set $ { member } (to $ { type } (json map. Get ($ { member })))));
wherein $ { member } is the member variable name, $ { type } is its type name; turning to the next step;
step552: if the member variable type is JavaBean, the following code is added to the first function body: json $ { type } ((json map. Get ($ { member }))))); turning to the next step;
step553: if the member variable type is an array or an ArrayList, converting each member of the array or the ArrayList into a basic type or a JavaBean to form a new array or the ArrayList, calling a Setter method of the member variable to assign a value, generating a conversion code by referring to the steps of Step551 and Step552, and adding the conversion code into the first function body; turning to the next step;
step554: if the member variable type is HashMap, JSON of key and value is converted into a basic type or JavaBean, the JSON is added into a new HashMap, then a Setter method of the member variable is called for assignment, a conversion code is generated by referring to the steps of Step551 and Step552, and the conversion code is added into the first function body; turning to the next step;
step555: if the member variable is of other types, generating a conversion code according to the steps 551 and 552, and adding the conversion code into the first function body; go to Step54.
5. The method as claimed in claim 4, wherein the first function body includes JSON $ { JavaBean } function body;
the first object comprises a $ { JavaBean } object.
6. A device for realizing rapid conversion of JSON into JavaBean is characterized by comprising a file establishing module, a class name reading module, a code generating module of JSON conversion function and a code file generating module;
the file establishing module is used for establishing a JsonUtils code file;
the class name reading module is used for reading the class name corresponding to each JavaBean according to the configuration file;
the JSON conversion function code generation module is used for traversing each JavaBean class name and generating a JSON conversion function code for each JavaBean; the code generation module of the JSON conversion function comprises a conversion unit, an adding unit, an obtaining unit, a checking unit and a conversion code generation unit; the conversion unit is used for converting the JSON into the code of the Map object and adding the code into the first function body; the adding unit is used for adding the code of the newly-built empty first object into the first function body; the acquisition unit is used for acquiring all member variables and member variable types of the first object by using Java reflection; a checking unit for checking whether there is an unprocessed member variable, and if not, ending; otherwise, acquiring a member variable and the type thereof, and turning to the next step; a conversion code generation unit for generating a conversion code according to the member variable type and adding the code to the first function body;
and the code file generation module is used for adding the code of the JSON conversion function into the newly-built JsonUtils class code file to enable the JsonUtils class code file to be used as a member function of the JsonUtils class.
7. The apparatus for realizing the rapid conversion of JSON into JavaBean according to claim 6, characterized in that, the apparatus also includes a checking module and a Class object generation module;
the checking module is used for checking whether unprocessed JavaBean exists or not, and if not, ending;
the Class object generating module generates a corresponding Class object by using a Java reflection technology and a JavaBean Class name;
and the code generation module of the JSON conversion function is used for generating a function code which is converted into a corresponding JavaBean by using the Class object.
8. The device for realizing JSON (Java Server object notation) quick conversion into JavaBean according to claim 6, wherein the class name reading module is specifically configured to detect the position of the JavaBean, write the set directory into the configuration file if the JavaBean is in the set directory, and obtain the JavaBean class name by traversing all JavaBean files in the directories; otherwise, writing the class name corresponding to the JavaBean into the configuration file.
CN202010470185.8A 2020-05-28 2020-05-28 Method and device for realizing rapid conversion of JSON into JavaBean Active CN111736842B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010470185.8A CN111736842B (en) 2020-05-28 2020-05-28 Method and device for realizing rapid conversion of JSON into JavaBean

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010470185.8A CN111736842B (en) 2020-05-28 2020-05-28 Method and device for realizing rapid conversion of JSON into JavaBean

Publications (2)

Publication Number Publication Date
CN111736842A CN111736842A (en) 2020-10-02
CN111736842B true CN111736842B (en) 2022-11-11

Family

ID=72646693

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010470185.8A Active CN111736842B (en) 2020-05-28 2020-05-28 Method and device for realizing rapid conversion of JSON into JavaBean

Country Status (1)

Country Link
CN (1) CN111736842B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115729967A (en) * 2022-11-23 2023-03-03 中国银行股份有限公司 Report query method and device
CN115586904B (en) * 2022-12-12 2023-04-07 云筑信息科技(成都)有限公司 JSON data format description-based dynamic generation method for Java beans

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109684224A (en) * 2018-12-29 2019-04-26 深圳点猫科技有限公司 The test method and device of the conversion process of python code and building blocks
CN110554857A (en) * 2018-05-30 2019-12-10 马上消费金融股份有限公司 JavaBean generation method, device and system and readable storage medium

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7284039B2 (en) * 2002-12-17 2007-10-16 International Business Machines Corporation Apparatus and method for flexible web service deployment

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110554857A (en) * 2018-05-30 2019-12-10 马上消费金融股份有限公司 JavaBean generation method, device and system and readable storage medium
CN109684224A (en) * 2018-12-29 2019-04-26 深圳点猫科技有限公司 The test method and device of the conversion process of python code and building blocks

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
JSON与XML动态转换研究及应用;刘立等;《软件导刊》;20180905(第11期);全文 *

Also Published As

Publication number Publication date
CN111736842A (en) 2020-10-02

Similar Documents

Publication Publication Date Title
CN111625224B (en) Code generation method, device, equipment and storage medium
CN113010181B (en) Deployment method and device of operators in deep learning framework and electronic equipment
US8336033B2 (en) Method and system for generating a hierarchical tree representing stack traces
CN102187313B (en) Caching runtime generated code
CN112100054A (en) A program static analysis method and system for data management and control
CN115951890B (en) Code conversion method, system and device between different front-end frameworks
CN111736842B (en) Method and device for realizing rapid conversion of JSON into JavaBean
US10120655B2 (en) Seamless high performance interoperability between different type graphs that share a garbage collector
CN111475150B (en) Cross-language binding method, device, equipment and storage medium
WO2023098043A1 (en) Interface parameter checking method and apparatus
US20190187965A1 (en) Reduced Memory Consumption of Compiler-Transformed Asynchronous Methods
CN115022312A (en) Implementation method, device, electronic device and storage medium of multi-smart contract engine
CN117910588A (en) A quantum computing program compilation method and device
US11922151B2 (en) Compiler-generated asynchronous enumerable object
US10656926B2 (en) Compact type layouts
CN117472910B (en) Homomorphic compressed data processing method and system
CN113312038B (en) Implementation method for generating Java code class relay structure
US11106522B1 (en) Process memory resurrection: running code in-process after death
CN116302852A (en) An information system call chain analysis and result graphical display method
CN116719531A (en) Object conversion method, system, media and equipment based on runtime bytecode editing
CN116107585A (en) A method and device for generating xml during server-side compilation
CN111857678B (en) Code generation method, device, electronic equipment and computer storage medium
CN119861914A (en) Object conversion method, system, electronic device and storage medium
CN115268856A (en) Implementation method and device, electronic device and storage medium of OpenCL CUB library
CN117492727A (en) JAVA-based attribute copy method, device, storage medium and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
CP03 Change of name, title or address
CP03 Change of name, title or address

Address after: Building 9, No.1, guanpu Road, Guoxiang street, Wuzhong Economic Development Zone, Wuzhong District, Suzhou City, Jiangsu Province

Patentee after: Suzhou Yuannao Intelligent Technology Co.,Ltd.

Country or region after: China

Address before: Building 9, No.1, guanpu Road, Guoxiang street, Wuzhong Economic Development Zone, Wuzhong District, Suzhou City, Jiangsu Province

Patentee before: SUZHOU LANGCHAO INTELLIGENT TECHNOLOGY Co.,Ltd.

Country or region before: China