Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
夜猫逐梦
1024程序员开源挑战赛
提交
b284064a
1
1024程序员开源挑战赛
项目概览
夜猫逐梦
/
1024程序员开源挑战赛
与 Fork 源项目一致
Fork自
GitCode / 1024程序员开源挑战赛(10.23-11.14)
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
1
1024程序员开源挑战赛
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b284064a
编写于
10月 15, 2021
作者:
W
wuyudi
提交者:
ninecents
10月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
style:
🎨
use es6 to clean code
上级
adab38b5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
64 addition
and
22 deletion
+64
-22
src/views/tool/library/json2CSharp.js
src/views/tool/library/json2CSharp.js
+51
-0
src/views/tool/library/json2Dart.js
src/views/tool/library/json2Dart.js
+13
-22
未找到文件。
src/views/tool/library/json2CSharp.js
0 → 100644
浏览文件 @
b284064a
export
default
{
_allClass
:
[],
format
(
string
,
...
replace
)
{
return
string
.
replace
(
/
\{(\d
+
)\}
/g
,
(
m
,
i
)
=>
replace
[
i
]
);
},
_genClassCode
(
obj
,
name
,
pag
)
{
let
clas
=
"
// =========
"
+
name
+
`.cs file ========= \nnamespace
${
pag
}
\n{\n`
;
clas
+=
` public class
${
name
}
\n {\n`
;
for
(
let
n
in
obj
)
{
let
v
=
obj
[
n
];
n
=
n
.
trim
();
clas
+=
`
${
this
.
_genComment
(
v
)}
public
${
this
.
_genTypeByProp
(
n
,
v
,
pag
)}
${
n
}
{ get; set; }\n`
;
}
clas
+=
"
}
\n
"
;
clas
+=
"
}
\n
"
;
console
.
log
(
pag
)
this
.
_allClass
.
push
(
clas
);
return
this
.
_allClass
.
join
(
"
\n
"
);
},
_genTypeByProp
(
name
,
val
,
pag
)
{
switch
(
Object
.
prototype
.
toString
.
apply
(
val
))
{
case
"
[object Number]
"
:
{
return
val
.
toString
().
includes
(
"
.
"
)
?
"
double
"
:
"
int
"
;
}
case
"
[object Date]
"
:
{
return
"
DateTime
"
;
}
case
"
[object Object]
"
:
{
name
=
name
.
substring
(
0
,
1
).
toUpperCase
()
+
name
.
substring
(
1
);
this
.
_genClassCode
(
val
,
name
,
pag
);
return
name
;
}
case
"
[object Array]
"
:
{
return
`List <
${
this
.
_genTypeByProp
(
name
+
"
Item
"
,
val
[
0
],
pag
)}
>`
;
}
default
:
{
return
"
string
"
;
}
}
},
_genComment
(
val
)
{
let
commm
=
typeof
(
val
)
==
"
string
"
&&
/.*
[\u
4e00-
\u
9fa5
]
+.*$/
.
test
(
val
)
?
val
:
""
;
return
"
/// <summary>
\n
///
"
+
commm
+
"
\n
/// </summary>
\n
"
;
},
convert
(
jsonObj
,
cls
,
pag
)
{
this
.
_allClass
=
[];
return
this
.
_genClassCode
(
jsonObj
,
cls
,
pag
);
}
}
\ No newline at end of file
src/views/tool/library/json2Dart.js
浏览文件 @
b284064a
// 算法来源 https://github.com/zx1262111739/JsonToDart/blob/main/index.js
let
keywrods
=
"
abstract,dynamic,implements,show,as,else,import,static,assert,enum,in,super,async,export,interface,switch,await,extends,is,sync,break,external,library,this,case,factory,mixin,throw,catch,false,new,true,class,final,null,try,const,finally,on,typedef,continue,for,operator,var,covariant,Function,part,void,default,get,rethrow,while,deferred,hide,return,with,do,if,set,yield,List
"
;
let
keywrodList
=
keywrods
.
split
(
"
,
"
);
const
caption
=
(
s
)
=>
s
[
0
].
toUpperCase
()
+
s
.
substring
(
1
);
// 生成属性名
const
generatePropertyName
=
function
(
name
)
{
const
generatePropertyName
=
(
name
)
=>
{
let
nameParts
=
name
.
split
(
"
_
"
);
let
output
=
nameParts
[
0
];
for
(
let
index
=
1
;
index
<
nameParts
.
length
;
index
++
)
{
output
+=
nameParts
[
index
].
substring
(
0
,
1
).
toUpperCase
()
+
nameParts
[
index
].
substring
(
1
)
output
+=
caption
(
nameParts
[
index
]
)
}
if
(
keywrodList
.
in
dexOf
(
output
)
!=
-
1
)
{
output
=
"
m
"
+
output
.
substring
(
0
,
1
).
toUpperCase
()
+
output
.
substring
(
1
);
if
(
keywrodList
.
in
cludes
(
output
)
)
{
output
=
"
m
"
+
caption
(
output
);
}
return
output
;
}
// 生成类名
const
generateClassName
=
function
(
name
)
{
let
nameParts
=
name
.
split
(
"
_
"
);
let
output
=
""
;
for
(
let
index
=
0
;
index
<
nameParts
.
length
;
index
++
)
{
output
+=
nameParts
[
index
].
substring
(
0
,
1
).
toUpperCase
()
+
nameParts
[
index
].
substring
(
1
)
}
return
output
;
const
generateClassName
=
(
name
)
=>
{
return
name
.
split
(
"
_
"
).
map
(
caption
).
join
(
""
);
}
const
convertObjectToClass
=
function
(
className
,
obj
)
{
const
convertObjectToClass
=
(
className
,
obj
)
=>
{
let
propers
=
[];
let
subClass
=
[];
for
(
let
key
in
obj
)
{
...
...
@@ -91,11 +84,11 @@ const convertObjectToClass = function (className, obj) {
if
(
propertyType
!==
""
)
{
propers
.
push
({
"
key
"
:
key
,
key
,
"
propertyName
"
:
generatePropertyName
(
key
),
"
propertyType
"
:
propertyType
,
"
isSubclass
"
:
isSubclass
,
"
isArray
"
:
isArray
,
propertyType
,
isSubclass
,
isArray
,
});
}
...
...
@@ -200,6 +193,4 @@ const convertObjectToClass = function (className, obj) {
return
output
;
}
export
default
(
json
,
typename
)
=>
{
return
convertObjectToClass
(
typename
,
json
)
}
\ No newline at end of file
export
default
(
json
,
typename
)
=>
convertObjectToClass
(
typename
,
json
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录