提交 d73bbb5c 编写于 作者: B baiy 提交者: ninecents

支持Json转Dart #45

上级 d78551a0
......@@ -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"
},
......
// 算法来源 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.includes(output)) {
output = "m" + caption(output);
if (keywrodList.indexOf(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.
先完成此消息的编辑!
想要评论请 注册