Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
夜猫逐梦
1024程序员开源挑战赛
提交
d73bbb5c
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看板
提交
d73bbb5c
编写于
6月 22, 2021
作者:
B
baiy
提交者:
ninecents
10月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
支持Json转Dart #45
上级
d78551a0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
16 deletion
+30
-16
src/views/tool/jsonToObject.vue
src/views/tool/jsonToObject.vue
+8
-3
src/views/tool/library/json2Dart.js
src/views/tool/library/json2Dart.js
+22
-13
未找到文件。
src/views/tool/jsonToObject.vue
浏览文件 @
d73bbb5c
...
...
@@ -25,7 +25,7 @@
<p
slot=
"title"
>
转换结果
</p>
<template
slot=
"extra"
>
<Button
style=
"margin-right: 5px"
size=
"small"
v-for=
"(item,key) in type"
:key=
"key"
type=
"primary"
@
click=
"handle(item)"
>
转
{{
item
}}
实体
@
click=
"handle(item)"
>
{{
item
}}
</Button>
</
template
>
<codemirror
ref=
"output"
v-model=
"current.output"
...
...
@@ -38,10 +38,12 @@
import
json2Go
from
'
./library/json2Go
'
import
json2CSharp
from
'
./library/json2CSharp
'
import
json2Java
from
'
./library/json2Java
'
import
json2Dart
from
'
./library/json2Dart
'
import
{
codemirror
}
from
'
vue-codemirror
'
import
'
codemirror/lib/codemirror.css
'
import
'
codemirror/mode/javascript/javascript.js
'
import
'
codemirror/mode/go/go.js
'
import
'
codemirror/mode/dart/dart
'
import
'
codemirror/mode/clike/clike.js
'
import
'
codemirror/addon/fold/foldcode.js
'
import
'
codemirror/addon/fold/foldgutter.js
'
...
...
@@ -76,9 +78,11 @@ export default {
case
"
Java
"
:
this
.
current
.
output
=
json2Java
(
JSON
.
parse
(
this
.
current
.
json
),
this
.
current
.
class
,
this
.
current
.
package
)
break
;
case
"
Dart
"
:
this
.
current
.
output
=
json2Dart
(
JSON
.
parse
(
this
.
current
.
json
),
this
.
current
.
class
)
break
;
case
"
C#
"
:
this
.
current
.
output
=
json2CSharp
.
convert
(
JSON
.
parse
(
this
.
current
.
json
),
this
.
current
.
class
,
this
.
current
.
package
)
break
;
}
}
catch
(
error
)
{
...
...
@@ -109,10 +113,11 @@ export default {
class
:
"
RootName
"
,
output
:
""
,
},
type
:
[
"
Java
"
,
"
C#
"
,
"
Go
"
],
type
:
[
"
Java
"
,
"
C#
"
,
"
Go
"
,
'
Dart
'
],
codemirrorMode
:
{
"
Json
"
:
"
application/json
"
,
"
Java
"
:
"
text/x-java
"
,
"
Dart
"
:
"
application/dart
"
,
"
C#
"
:
"
text/x-csharp
"
,
"
Go
"
:
"
text/x-go
"
},
...
...
src/views/tool/library/json2Dart.js
浏览文件 @
d73bbb5c
// 算法来源 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
=
(
name
)
=>
{
const
generatePropertyName
=
function
(
name
)
{
let
nameParts
=
name
.
split
(
"
_
"
);
let
output
=
nameParts
[
0
];
for
(
let
index
=
1
;
index
<
nameParts
.
length
;
index
++
)
{
output
+=
caption
(
nameParts
[
index
]
)
output
+=
nameParts
[
index
].
substring
(
0
,
1
).
toUpperCase
()
+
nameParts
[
index
].
substring
(
1
)
}
if
(
keywrodList
.
in
cludes
(
output
)
)
{
output
=
"
m
"
+
caption
(
output
);
if
(
keywrodList
.
in
dexOf
(
output
)
!=
-
1
)
{
output
=
"
m
"
+
output
.
substring
(
0
,
1
).
toUpperCase
()
+
output
.
substring
(
1
);
}
return
output
;
}
// 生成类名
const
generateClassName
=
(
name
)
=>
{
return
name
.
split
(
"
_
"
).
map
(
caption
).
join
(
""
);
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
convertObjectToClass
=
(
className
,
obj
)
=>
{
const
convertObjectToClass
=
function
(
className
,
obj
)
{
let
propers
=
[];
let
subClass
=
[];
for
(
let
key
in
obj
)
{
...
...
@@ -84,11 +91,11 @@ const convertObjectToClass = (className, obj) => {
if
(
propertyType
!==
""
)
{
propers
.
push
({
key
,
"
key
"
:
key
,
"
propertyName
"
:
generatePropertyName
(
key
),
propertyType
,
isSubclass
,
isArray
,
"
propertyType
"
:
propertyType
,
"
isSubclass
"
:
isSubclass
,
"
isArray
"
:
isArray
,
});
}
...
...
@@ -193,4 +200,6 @@ const convertObjectToClass = (className, obj) => {
return
output
;
}
export
default
(
json
,
typename
)
=>
convertObjectToClass
(
typename
,
json
)
export
default
(
json
,
typename
)
=>
{
return
convertObjectToClass
(
typename
,
json
)
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录