Ajax/JavaScript解析的实现

This commit is contained in:
皓月 2018-10-19 00:36:32 +08:00
parent 044001e337
commit c49c14c771
7 changed files with 165 additions and 99 deletions

View File

@ -21,11 +21,11 @@ namespace CodeShowWeb
string CodeLang = GetLang();
if (CodeLang == "Java")
{
return GetJavaCode();
return new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
}
else if (CodeLang == "swift")
{
return GetSwiftCode();
return new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
}
return "代码语言尚未支持";
@ -57,17 +57,5 @@ namespace CodeShowWeb
return "代码语言尚未支持";
}
public string GetJavaCode()
{
string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
return JavaCode;
}
public string GetSwiftCode()
{
string SwiftCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
return SwiftCode;
}
}
}

View File

@ -14,7 +14,7 @@
<h2>服务端参数跨语言代码转换引擎 Ver 0.1.0 -- 皓月</h2>
<h4>是一个C#服务端API参数实体类、转换成于各类语言客户端解析参数代码减少客户端开发人员重复工作量</h4>
<h4>请选择目前支持转换到的目标语言:</h4>
<h3><a href="/Index.aspx?lang=Java">Java/Android</a>、<a href="/Index.aspx?lang=swift">Swift/iOS</a>、<a href="/Index.aspx?lang=JavaScript">Ajax/Javascript</a>、<a href="/Index.aspx?lang=PHP">PHP</a>、<a href="/Index.aspx?lang=Golang">Golang</a>、...</h3>
<h3><a href="/Index.aspx?lang=Java">Java/Android</a>、<a href="/Index.aspx?lang=Swift">Swift/iOS</a>、<a href="/Index.aspx?lang=JavaScript">Ajax/Javascript</a>、<a href="/Index.aspx?lang=PHP">PHP</a>、<a href="/Index.aspx?lang=Golang">Golang</a>、...</h3>
<h4>选择你喜欢的代码配色方案<div id="changeStyleSelect"></div></h4>

View File

@ -20,12 +20,17 @@ namespace CodeShowWeb
string CodeLang = GetLang();
if (CodeLang == "Java")
{
return GetJavaCode();
return new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
}
else if (CodeLang == "swift")
else if (CodeLang == "Swift")
{
return GetSwiftCode();
return new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
}
else if (CodeLang == "JavaScript")
{
return new AjaxCodeWrite().ModelToAjaxCode("CoderEngine", "A");
}
return "代码语言尚未支持";
}
@ -48,25 +53,17 @@ namespace CodeShowWeb
{
return "androidstudio.css";
}
else if (CodeLang == "swift")
else if (CodeLang == "Swift")
{
return "xcode.css";
}
else if (CodeLang == "JavaScript")
{
return "codepen-embed.css";
}
return "代码语言尚未支持";
}
public string GetJavaCode()
{
string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
return JavaCode;
}
public string GetSwiftCode()
{
string SwiftCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
return SwiftCode;
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace CoderEngine
@ -24,26 +25,54 @@ namespace CoderEngine
CodeStr += CodeLine + "\n";
}
public string ModelToJavaCode(string _AssemblyName, string _TypeName)
/// <summary>
/// 倍数缩进
/// </summary>
/// <param name="Number"></param>
public string TabstrForNumber(int Number)
{
string str = "";
if (Number < 0)
Number = 0;
for (int i = 0; i < Number; i++)
{
str += tabstr;
}
return str;
}
public string ModelToAjaxCode(string _AssemblyName, string _TypeName)
{
TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm);
ModelToAjaxCodeWriter(tm);
return CodeStr;
}
public void ModelToJavaCodeWriter(TypeMode TopMode)
public void ModelToAjaxCodeWriter(TypeMode TopMode)
{
CoderAddLine("");
CoderAddLine("//生成Ajax请求后解析返回值的js代码");
CoderAddLine("success: function(data) {//请求完成");
CoderAddLine(tabstr + "var ob = JSON.parse(data.Value);//序列化Value字符串为json对象");
//如果有代码嵌套的下级类 在本类外 写入新的类
if (TopMode.TypeList != null && TopMode.TypeList.Count > 0)
{
CoderAddLine(tabstr);
JsCodeDownRecursion(TopMode);
AjaxCodeDownRecursion(TopMode,"",1);
}
CoderAddLine("}");
}
public void JsCodeDownRecursion(TypeMode TopMode)
public void AjaxCodeDownRecursion(TypeMode TopMode,string ParentCode, int TabNumber)
{
if (ParentCode == null || ParentCode == "")
ParentCode = "ob";
string ClassName = "";
//如果自己就是集合类型 则取类型下名字
if (TopMode.MemberType.Name == "List`1")
@ -52,81 +81,130 @@ namespace CoderEngine
//其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型”
var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".")+1);
}
else
ClassName = TopMode.MemberType.Name;
ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1);
CoderAddLine("public class " + ClassName + " {");
CoderAddLine(TabstrForNumber(TabNumber-1) + "//循环取内嵌集合"+ ParentCode+ "数据");
CoderAddLine(TabstrForNumber(TabNumber-1) + "for(i" + TabNumber + " = 0;i" + TabNumber + " < " + ParentCode+ ".lenght;i" + TabNumber + "++){");
foreach (var tl in TopMode.TypeList)
{
CoderAddLine(tabstr + "@SerializedName(value = \"" + tl.Name + "\")");
CoderAddLine(tabstr + "public " + CSharpToJavaScriptTypeName(tl.MemberType) + " " + tl.Name + ";");
}
CoderAddLine("}");
foreach (var tl in TopMode.TypeList)
{
if(tl.MemberType.Name == "List`1")
if (tl.MemberType.Name == "List`1")
{
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType));
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量");
if (tl.TypeList != null && tl.TypeList.Count > 0)
{
CoderAddLine("");
JsCodeDownRecursion(tl);
}
}
}
ParentCode = ParentCode + "[i" + TabNumber + "]";
string ParentCodeStr = "";
if (ParentCode != null)
ParentCodeStr = ParentCode + "." + tl.Name;
else
ParentCodeStr = "ob." + tl.Name;
AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1);
}
/// <summary>
/// 转换成Java代码
/// </summary>
/// <returns></returns>
public string CSharpToJavaScriptTypeName(Type myType)
{
var oneinfo = CSharpToJavaScriptTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault();
if (oneinfo.Value != null)
{
return oneinfo.Value;
}
else
{
if(myType.Name == "List`1")
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i" + TabNumber + "]." + tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType));
}
}
CoderAddLine(TabstrForNumber(TabNumber-1) + "}");
}
else
{
ClassName = TopMode.MemberType.Name;
foreach (var tl in TopMode.TypeList)
{
if (tl.MemberType.Name == "List`1")
{
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType));
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量");
if (tl.TypeList != null && tl.TypeList.Count > 0)
{
CoderAddLine("");
string ParentCodeStr = "";
if (ParentCode != null)
ParentCodeStr = ParentCode + "." + tl.Name;
else
ParentCodeStr = "ob." + tl.Name;
AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1);
}
}
else
{
CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "."+ tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType));
}
}
}
}
/// <summary>
/// 转换成JS代码描述
/// </summary>
/// <returns></returns>
public string CSharpToJavaTypeName(Type myType)
{
var oneinfo = CSharpToJSTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault();
if (oneinfo.Value != null)
{
return "类型为 " + oneinfo.Value;
}
else
{
if (myType.Name == "List`1")
{
return "";
}
else if (myType.Name == "Nullable`1")
{
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.')+1) + ">";
//string var = SonParamName.Substring(SonParamName.LastIndexOf('.') + 1);
string SonParamFromAssemblyName = SonParamName.Substring(0, SonParamName.IndexOf('.'));
//貌似反射加载System是不行的是吗
//Type PerentType = System.GetType(SonParamName);
return "";
}
else
return "string";
return "类型为 " + "string";
}
}
/// <summary>
/// C#和Java代码对照表
/// C#和JS代码对照表
/// </summary>
public System.Collections.Generic.Dictionary<string, string> CSharpToJavaScriptTypeItem =
public System.Collections.Generic.Dictionary<string, string> CSharpToJSTypeItem =
new Dictionary<string, string>() {
{ "int","int"},
{ "Int16","int"},
{ "Int32","int"},
{ "Int64","int"},
{ "int?","int"},
{ "long","long"},
{ "long?","long"},
{ "float","float"},
{ "float?","float"},
{ "double","double"},
{ "double?","double"},
{ "boolean","boolean"},
{ "string","string"},
{ "DateTime","DateTime"},
{ "DateTime?","DateTime"},
{ "DateTime","时间格式形如 1999-99-99 10:10:10"},
};
}
}

View File

@ -39,10 +39,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AjaxCodeWrite.cs" />
<Compile Include="CSharpCodeWrite.cs" />
<Compile Include="GolangCodeWrite.cs" />
<Compile Include="JavaCodeWrite.cs" />
<Compile Include="AjaxCodeWrite.cs" />
<Compile Include="_ModleReader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SwiftCodeWrite.cs" />

View File

@ -99,9 +99,6 @@ namespace CoderEngine
CoderAddLine("}");
foreach (var tl in TopMode.TypeList)
{
if(tl.MemberType.Name == "List`1")

View File

@ -19,18 +19,24 @@ namespace UseEngineConsolo
Console.WriteLine();
Console.WriteLine();
string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
//string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A");
//Console.WriteLine();
//Console.WriteLine("C#实体类动态转Java代码结果");
//Console.WriteLine();
//Console.WriteLine(JavaCode);
//Console.WriteLine();
//Console.WriteLine();
//string CSCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
//Console.WriteLine();
//Console.WriteLine("C#实体类动态转Ajax客户端代码结果");
//Console.WriteLine();
//Console.WriteLine(CSCode);
//Console.ReadLine();
string AjaxCode = new AjaxCodeWrite().ModelToAjaxCode("CoderEngine", "A");
Console.WriteLine();
Console.WriteLine("C#实体类动态转Java代码结果");
Console.WriteLine("C#实体类动态转Ajax客户端代码结果:");
Console.WriteLine();
Console.WriteLine(JavaCode);
Console.WriteLine();
Console.WriteLine();
string CSCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A");
Console.WriteLine();
Console.WriteLine("Swift实体类动态转C#客户端代码结果:");
Console.WriteLine();
Console.WriteLine(CSCode);
Console.WriteLine(AjaxCode);
Console.ReadLine();
}
}