json解析代码生成器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# 一个类包含:类名,属性列表和内部类指针   
class Clazz   
def initialize(cls_name)   
@cls_name = cls_name   
@attrs=[]   
@lead_cls_names=nil  #前面有几个前导类名   
  
@list_attrs = []  
end   
def cls_name   
@cls_name   
end   
  
  
attr_accessor :attrs, :lead_cls_names ,:list_attrs  
end   
  
  
#属性   
class Attr   
def initialize(type=nil,name=nil)   
@type ,@name=type,name   
end   
attr_accessor :type, :name   
end   
  
  
class Env   
def initialize()   
@clz_list = []        #类的容器   
@cur_clz = nil        #当前类   
@attr = nil      #当前属性   
@attr_index=0         #当前类的第几个属性   
@clz_index=0      #当前属于第几个类   
@cls_print_queue=nil  #类打印队列   
  
@cur_cls_list_attrs_index = 0  
end   
  
attr_accessor :clz_list,:cur_clz,:attr,:attr_index,:clz_index,:cur_cls_list_attrs_index   
end   
  
  
$env = Env.new   
  
  
def print_clz_list              #测试容器数据   
$env.clz_list.each do |clzz|   
lead_cls_names = clzz.lead_cls_names   
print "============"   
lead_cls_names.each do |lead_names|   
print lead_names + "-"   
end   
print "#{clzz.cls_name}============\n"   
  
attrs = clzz.attrs   
attrs.each do |attr|   
puts attr.type + "=" + attr.name   
end   
end   
end   
  
  
def print_cls_head(i)   
clazz = $env.clz_list[i]   
lead_clz_names = clazz.lead_cls_names   
lead_num = lead_clz_names.size   
cls_name = clazz.cls_name   
template = ""   
if(i == 0)   
template += "public class #{cls_name}{\n"   
else   
template += "\n"   
template += "\t" * lead_num   
template += "public static class #{cls_name}{\n"   
end   
  
template += "\n"  
  
template += "\t" * lead_num + "\t"  
  
template += "private #{cls_name}(){}\n"  
  
template   
  
end   
  
  
#返回前导类的个数   
def get_lead_num(i)   
lead_cls_names = $env.clz_list[i].lead_cls_names   
lead_num = 0   
if(lead_cls_names == nil)   
lead_num = 0   
else   
lead_num = lead_cls_names.size   
end   
  
lead_num   
end   
  
  
def print_cls_end(i)   
lead_num = get_lead_num(i)   
template = "\t" * lead_num + "}\n\n"   
template   
end   
  
  
def print_attrs(i)   
lead_type = "\t" * get_lead_num(i) + "\t"   
attrs = $env.clz_list[i].attrs   
  
template = "\n"   
attrs.each do |attr|   
type = attr.type   
name = attr.name   
template += "#{lead_type}private #{type} #{name};\n"    
end   
  
template   
  
end   
  
  
def get_template(i)   
clazz = $env.clz_list[i]   
lead_type = "\t" * get_lead_num(i) + "\t"   
template = "\n"   
clazz.attrs.each do |attr|   
first_cap_name = attr.name[0].upcase + attr.name[1..attr.name.size-1]   
template += "#{lead_type}public #{attr.type} get#{first_cap_name}(){\n"   
template += "#{lead_type}\treturn #{attr.name};\n"   
template += "#{lead_type}}\n\n"   
end   
template   
end   
  
  
def builder_template(i=0)   
clazz = $env.clz_list[i]   
cls_name = clazz.cls_name   
instance_name = cls_name[0].downcase + cls_name[1..cls_name.size-1]   
attrs = clazz.attrs   
  
lead_type = "\t" * get_lead_num(i) + "\t"   
  
template = "#{lead_type}public static class Builder{\n"   
  
template += "#{lead_type}\tprivate #{cls_name} #{instance_name} = new #{cls_name}();\n\n"   
attrs.each do |attr|   
first_cap_name = attr.name[0].upcase + attr.name[1..attr.name.size-1] #首字母大写   
template += "#{lead_type}\tpublic Builder set#{first_cap_name}(#{attr.type} #{attr.name}){\n"   
template += "#{lead_type}\t\t#{instance_name}.#{attr.name} = #{attr.name};\n"   
template += "#{lead_type}\t\treturn this;\n"   
template += "#{lead_type}\t}\n\n"   
end   
  
template += "#{lead_type}\tpublic #{cls_name} get#{cls_name}(){\n"   
template += "#{lead_type}\t\treturn #{instance_name};\n"   
template +="#{lead_type}\t}\n"   
  
template += "#{lead_type}}\n"   
  
  
template   
  
end   
  
  
#逆序排序   
def get_print_cls_done_queue   
cls_list = $env.clz_list   
loc_cls_list=cls_list.sort{|i,j| j.lead_cls_names.size<=>i.lead_cls_names.size}   
loc_cls_list   
end   
  
  
def print_cur_cls_end(cls)   
lead_types="\t"*cls.lead_cls_names.size   
#puts "#{cls.lead_cls_names.size}个tabs"   
lead_types+"}\n"   
end   
  
  
  
  
def genBean   
clazz_list=$env.clz_list   
cls_done_queue=get_print_cls_done_queue   
print_queue_size=cls_done_queue.size   
print_queue_index=0   
0.upto(clazz_list.size-1) do |i|   
puts print_cls_head(i)   
puts print_attrs(i)   
puts get_template(i)   
puts builder_template(i)   
  
#查看是否可以为当前类打印结束标签   
cur_lead_types_num = clazz_list[i].lead_cls_names.size   
print_queue_lead_types_num=cls_done_queue[print_queue_index].lead_cls_names.size   
if(cur_lead_types_num == print_queue_lead_types_num)   
puts print_cur_cls_end(cls_done_queue[print_queue_index])   
print_queue_index += 1   
end   
end   
  
#将剩余的未结束类结束其花括号标签   
print_queue_index.upto(print_queue_size-1) do |i|   
puts print_cur_cls_end(cls_done_queue[i])   
end   
  
=begin   
(clazz_list.size-1).downto(0) do |i|   
puts print_cls_end(i)   
end   
=end   
end   
  
  
  
  
=begin   
$clz_list = []     #类的容器   
$cur_clz = nil     #当前类   
$attr = nil  #当前属性   
$attr_index=0      #当前类的第几个属性   
$clz_index=0  #当前属于第几个类   
=end   
  
  
def json_builder(cls=$env.clz_list[0])  
    cls_name = cls.cls_name  
    if(cls_name == $env.clz_list[0].cls_name)  
        the_outerest_cls = $env.clz_list[0]  
        cls_name = the_outerest_cls.cls_name  
        template = "public static #{cls_name} get#{cls_name}(JSONObject jsonObj){\n"  
        template += "\tif(jsonObj == null) return null;\n"  
  
        template += builder_attrs  
        template += "}\n"  
  
        template << json_get_list  
  
        template  
  
    else  
        template = "private static #{cls_name} get#{cls_name}(JSONObject jsonObj){\n"  
        template += "\tif(jsonObj == null) return null;\n"  
  
        template += builder_attrs(cls)  
        template += "}\n"  
  
        template << json_get_list(cls)  
  
        template  
  
    end  
    end  
  
def builder_attrs(cls = $env.clz_list[0])  
    template = ""  
    cls_name = cls.cls_name  
    attrs = cls.attrs  
    template += "\treturn new #{cls_name}.Builder()\n"  
      
    attrs.each do |attr|  
        type = attr.type  
        name = attr.name  
        cap_name = name.capitalize  
        cap_type = type.capitalize  
  
  
    if /^List/ =~ type  
        list_attrs = cls.list_attrs  
        list_attrs[$env.cur_cls_list_attrs_index] = attr  
  
        template += "\t\t.set#{cap_name}(get#{cap_name}(jsonObj))\n"  
        next  
    end  
  
        template += "\t\t.set#{cap_name}"  
        template += "(jsonObj.opt#{cap_type}(\"#{name}\"))\n"  
    end  
    template = template.slice(0,template.size-1);  
    template << ".get#{cls_name}();\n"  
  
end  
  
def json_get_list(cls=$env.clz_list[0])  
    template = "\n\n"  
    cls_name = cls.cls_name  
  
    list_attrs = cls.list_attrs  
  
    attr = list_attrs[0]  
  
    list_attrs.each do |attr|  
        template << json_get_list_attr(attr)  
    end  
  
    template  
      
end  
  
def json_get_list_attr(attr)  
    template = ""  
    attr_type = attr.type  
    attr_name = attr.name  
  
    cap_attr_name = attr.name.capitalize  
  
    key_word = nil  
    first_word_downcase_kw=nil  
  
    if /^List<(.*?)>/ =~ attr_type  
        key_word = $1  
        first_word_downcase_kw = key_word[0].downcase + key_word[1..key_word.size-1]  
    end  
      
    template << "private static #{attr_type} get#{cap_attr_name}(JSONObject jsonObj){\n"  
  
    template += "\tJSONArray jsonArray = jsonObj.optJSONArray(\"#{first_word_downcase_kw}\");\n"  
  
    template += "\tif(jsonArray == null) return null;\n"  
  
    template += "\tfinal int len = jsonArray.length();\n"  
    template += "\t#{attr_type} result = new ArrayList<#{key_word}>();\n"  
    template += "\tfor(int i = 0; i < len; i++){\n"  
    template += "\t\tJSONObject innerJSONObj = jsonArray.optJSONObject(i);\n"  
    template += "\t\tresult.add(get#{key_word}(innerJSONObj));\n"  
  
    template += "\t}\n"  
  
    template += "\treturn result;\n"  
      
    template += "}\n"  
  
  
    cls = find_cls_from_kw(key_word)  
      
    template <<   json_builder(cls)  
  
    template  
  
end  
  
def find_cls_from_kw(key_word)  
    cls_list = $env.clz_list  
    cls_list.each do |cls|  
        cls_name = cls.cls_name  
        if(cls_name == key_word)  
            return cls  
        end  
    end  
  
    nil  
end  
  
File.open("Test.txt"){|f|   
f.each_line do |line|   
line = line.gsub("\n","") #去除换行符   
  
  
if /^Cls/ =~ line  #碰到以Cls开头,表名以下的属性属于该类   
type_cls_names = line.split(":") #将该行分成两部分,第一部分为Cls,第二部分为类名集合,每个类名之间用-相连   
cls_names = type_cls_names[1].split("-")  #获取类名集合   
  
  
cls_name = cls_names[cls_names.size-1]    
$env.cur_clz = Clazz.new(cls_name)   
  
$env.cur_clz.lead_cls_names = cls_names[0...cls_names.size-1]  #获取前导类名集合   
  
$env.clz_list[$env.clz_index] = $env.cur_clz;    
$env.clz_index += 1   
  
$env.attr_index = 0            #重置attr_index   
  
else               #将该类的属性,填充在该类中   
if $env.cur_clz == nil   
puts "nil"   
else   
attr_type_name = line.split(":")   
attr_type=attr_type_name[0]   
attr_name=attr_type_name[1]   
$env.attr = Attr.new(attr_type,attr_name)   
$env.cur_clz.attrs[$env.attr_index] = $env.attr   
$env.attr_index += 1   
  
  
end   
end   
  
end   
  
=begin   
print_clz_list   
  
puts get_template(0)   
  
puts builder_template   
=end   
  
# -------- generate bean base on builder pattern  
# 将这行注释打开,就会生成相应的bean
#genBean   
  
#-----generate json parser template code 
#将这行注释打开,会想成相应的json解析代码 
puts json_builder   
}  


测试配置文件如下:
Cls:Person
String:name
int:age
int:sex
String:description
List<Des>:deses
Cls:Person-Des
int:salary
List<Lib>:libs
Cls:Person-Des-Lib
int:type
String:shit


生成的json解析代码如下:
public static Person getPerson(JSONObject jsonObj){
	if(jsonObj == null) return null;
	return new Person.Builder()
		.setName(jsonObj.optString("name"))
		.setAge(jsonObj.optInt("age"))
		.setSex(jsonObj.optInt("sex"))
		.setDescription(jsonObj.optString("description"))
		.setDeses(getDeses(jsonObj)).getPerson();
}


private static List<Des> getDeses(JSONObject jsonObj){
	JSONArray jsonArray = jsonObj.optJSONArray("des");
	if(jsonArray == null) return null;
	final int len = jsonArray.length();
	List<Des> result = new ArrayList<Des>();
	for(int i = 0; i < len; i++){
		JSONObject innerJSONObj = jsonArray.optJSONObject(i);
		result.add(getDes(innerJSONObj));
	}
	return result;
}
private static Des getDes(JSONObject jsonObj){
	if(jsonObj == null) return null;
	return new Des.Builder()
		.setSalary(jsonObj.optInt("salary"))
		.setLibs(getLibs(jsonObj)).getDes();
}


private static List<Lib> getLibs(JSONObject jsonObj){
	JSONArray jsonArray = jsonObj.optJSONArray("lib");
	if(jsonArray == null) return null;
	final int len = jsonArray.length();
	List<Lib> result = new ArrayList<Lib>();
	for(int i = 0; i < len; i++){
		JSONObject innerJSONObj = jsonArray.optJSONObject(i);
		result.add(getLib(innerJSONObj));
	}
	return result;
}
private static Lib getLib(JSONObject jsonObj){
	if(jsonObj == null) return null;
	return new Lib.Builder()
		.setType(jsonObj.optInt("type"))
		.setShit(jsonObj.optString("shit")).getLib();
}

生成的JavaBean如下:
public class Person{

	private Person(){}

	private String name;
	private int age;
	private int sex;
	private String description;
	private List<Des> deses;

	public String getName(){
		return name;
	}

	public int getAge(){
		return age;
	}

	public int getSex(){
		return sex;
	}

	public String getDescription(){
		return description;
	}

	public List<Des> getDeses(){
		return deses;
	}

	public static class Builder{
		private Person person = new Person();

		public Builder setName(String name){
			person.name = name;
			return this;
		}

		public Builder setAge(int age){
			person.age = age;
			return this;
		}

		public Builder setSex(int sex){
			person.sex = sex;
			return this;
		}

		public Builder setDescription(String description){
			person.description = description;
			return this;
		}

		public Builder setDeses(List<Des> deses){
			person.deses = deses;
			return this;
		}

		public Person getPerson(){
			return person;
		}
	}

	public static class Des{

		private Des(){}

		private int salary;
		private List<Lib> libs;

		public int getSalary(){
			return salary;
		}

		public List<Lib> getLibs(){
			return libs;
		}

		public static class Builder{
			private Des des = new Des();

			public Builder setSalary(int salary){
				des.salary = salary;
				return this;
			}

			public Builder setLibs(List<Lib> libs){
				des.libs = libs;
				return this;
			}

			public Des getDes(){
				return des;
			}
		}

		public static class Lib{

			private Lib(){}

			private int type;
			private String shit;

			public int getType(){
				return type;
			}

			public String getShit(){
				return shit;
			}

			public static class Builder{
				private Lib lib = new Lib();

				public Builder setType(int type){
					lib.type = type;
					return this;
				}

				public Builder setShit(String shit){
					lib.shit = shit;
					return this;
				}

				public Lib getLib(){
					return lib;
				}
			}
		}
	}
}