Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
夜猫逐梦
1024程序员开源挑战赛
提交
d24bbd9d
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,发现更多精彩内容 >>
提交
d24bbd9d
编写于
6月 22, 2021
作者:
B
baiy
提交者:
ninecents
10月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
支持Json转Dart #45
上级
6d49be86
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
213 addition
and
3 deletion
+213
-3
src/views/tool/jsonToObject.vue
src/views/tool/jsonToObject.vue
+8
-3
src/views/tool/library/json2Dart.js
src/views/tool/library/json2Dart.js
+205
-0
未找到文件。
src/views/tool/jsonToObject.vue
浏览文件 @
d24bbd9d
...
...
@@ -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
0 → 100644
浏览文件 @
d24bbd9d
// 算法来源 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
generatePropertyName
=
function
(
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
)
}
if
(
keywrodList
.
indexOf
(
output
)
!=
-
1
)
{
output
=
"
m
"
+
output
.
substring
(
0
,
1
).
toUpperCase
()
+
output
.
substring
(
1
);
}
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
convertObjectToClass
=
function
(
className
,
obj
)
{
let
propers
=
[];
let
subClass
=
[];
for
(
let
key
in
obj
)
{
if
(
!
obj
.
hasOwnProperty
(
key
))
{
continue
;
}
let
propertyType
=
""
;
let
isArray
=
false
;
let
isSubclass
=
false
;
switch
(
typeof
obj
[
key
])
{
case
"
number
"
:
propertyType
=
"
double
"
;
break
;
case
"
string
"
:
propertyType
=
"
String
"
;
break
;
case
"
boolean
"
:
propertyType
=
"
bool
"
;
break
;
case
"
object
"
:
if
(
Array
.
isArray
(
obj
[
key
]))
{
isArray
=
true
;
if
(
obj
[
key
].
length
>
0
)
{
let
subObj
=
obj
[
key
][
0
];
switch
(
typeof
subObj
)
{
case
"
number
"
:
propertyType
=
"
double
"
;
break
;
case
"
string
"
:
propertyType
=
"
String
"
;
break
;
case
"
boolean
"
:
propertyType
=
"
bool
"
;
break
;
case
"
object
"
:
if
(
Array
.
isArray
(
subObj
))
{
propertyType
=
"
dynamic
"
;
}
else
{
isSubclass
=
true
;
propertyType
=
className
+
generateClassName
(
key
);
subClass
.
push
(
convertObjectToClass
(
propertyType
,
subObj
));
}
break
;
}
}
else
{
propertyType
=
"
dynamic
"
;
}
}
else
{
isSubclass
=
true
;
propertyType
=
className
+
generateClassName
(
key
);
subClass
.
push
(
convertObjectToClass
(
propertyType
,
obj
[
key
]));
}
break
;
default
:
break
;
}
if
(
propertyType
!==
""
)
{
propers
.
push
({
"
key
"
:
key
,
"
propertyName
"
:
generatePropertyName
(
key
),
"
propertyType
"
:
propertyType
,
"
isSubclass
"
:
isSubclass
,
"
isArray
"
:
isArray
,
});
}
}
let
output
=
`class
${
className
}
{\n\n`
;
// -- 生成属性
for
(
let
idx
in
propers
)
{
let
prop
=
propers
[
idx
];
if
(
prop
.
isArray
)
{
output
+=
` List<
${
prop
.
propertyType
}
>
${
prop
.
propertyName
}
= List<
${
prop
.
propertyType
}
>();\n\n`
;
}
else
{
switch
(
prop
.
propertyType
)
{
case
"
double
"
:
output
+=
`
${
prop
.
propertyType
}
${
prop
.
propertyName
}
= 0;\n\n`
;
break
;
case
"
String
"
:
output
+=
`
${
prop
.
propertyType
}
${
prop
.
propertyName
}
= "";\n\n`
;
break
;
case
"
bool
"
:
output
+=
`
${
prop
.
propertyType
}
${
prop
.
propertyName
}
= false;\n\n`
;
break
;
default
:
output
+=
`
${
prop
.
propertyType
}
${
prop
.
propertyName
}
=
${
prop
.
propertyType
}
();\n\n`
;
break
;
}
}
}
// -- 生成默认构造方法
output
+=
`
${
className
}
();\n\n`
// -- 生成FromJson方法
output
+=
`
${
className
}
.fromJson(Map<String, dynamic> json) {\n\n`
;
for
(
let
idx
in
propers
)
{
let
prop
=
propers
[
idx
];
// -- 类型检测
if
(
prop
.
isArray
)
{
output
+=
` if (json["
${
prop
.
key
}
"] != null && json["
${
prop
.
key
}
"] is List) {\n`
}
else
if
(
prop
.
isSubclass
)
{
output
+=
` if (json["
${
prop
.
key
}
"] != null && json["
${
prop
.
key
}
"] is Map) {\n`
}
else
if
(
prop
.
propertyType
==
"
double
"
)
{
output
+=
` if (json["
${
prop
.
key
}
"] != null && (json["
${
prop
.
key
}
"] is int || json["
${
prop
.
key
}
"] is double)) {\n`
}
else
{
output
+=
` if (json["
${
prop
.
key
}
"] != null && json["
${
prop
.
key
}
"] is
${
prop
.
propertyType
}
) {\n`
}
if
(
prop
.
isArray
)
{
if
(
prop
.
isSubclass
)
{
output
+=
` final objs = List<
${
prop
.
propertyType
}
>();\n`
;
output
+=
` for (var item in json["
${
prop
.
key
}
"]) {\n`
;
output
+=
` objs.add(
${
prop
.
propertyType
}
.fromJson(item));\n`
;
output
+=
` }\n`
;
output
+=
` this.
${
prop
.
propertyName
}
= objs;\n`
}
else
{
output
+=
` final objs = List<
${
prop
.
propertyType
}
>();\n`
;
output
+=
` for (var item in json["
${
prop
.
key
}
"]) {\n`
;
output
+=
` objs.add(item);\n`
;
output
+=
` }\n`
;
output
+=
` this.
${
prop
.
propertyName
}
= objs;\n`
}
}
else
if
(
prop
.
isSubclass
)
{
output
+=
` this.
${
prop
.
propertyName
}
=
${
prop
.
propertyType
}
.fromJson(json["
${
prop
.
key
}
"]);\n`
;
}
else
if
(
prop
.
propertyType
==
"
double
"
)
{
output
+=
` this.
${
prop
.
propertyName
}
= json["
${
prop
.
key
}
"].toDouble();\n`
}
else
{
output
+=
` this.
${
prop
.
propertyName
}
= json["
${
prop
.
key
}
"];\n`
}
output
+=
` }\n`
}
output
+=
` }\n\n`
// -- 生成ToJson方法
output
+=
` Map<String, dynamic> toJson() {\n`
;
output
+=
` final map = Map<String, dynamic>();\n`
for
(
let
idx
in
propers
)
{
let
prop
=
propers
[
idx
];
output
+=
` if (this.
${
prop
.
propertyName
}
!= null) {\n`
if
(
prop
.
isArray
&&
prop
.
isSubclass
)
{
output
+=
` map["
${
prop
.
key
}
"] = this.
${
prop
.
propertyName
}
.map((e) => e.toJson()).toList();\n`
;
}
else
if
(
prop
.
isSubclass
)
{
output
+=
` map["
${
prop
.
key
}
"] = this.
${
prop
.
propertyName
}
.toJson();\n`
}
else
{
output
+=
` map["
${
prop
.
key
}
"] = this.
${
prop
.
propertyName
}
;\n`
}
output
+=
` }\n`
}
output
+=
` return map;\n`
output
+=
"
}
\n\n
"
;
output
+=
"
}
\n\n
"
for
(
let
idx
in
subClass
)
{
output
+=
subClass
[
idx
];
}
return
output
;
}
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录