Skip to content

Commit

Permalink
添加json转实体
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerwzy committed Oct 20, 2017
1 parent a84a620 commit a7c76ba
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
53 changes: 53 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"agreementUuid": "c6e32f8e-c9da-4aa9-bed7-4bccc21ada02",
"agreementApplyUuid": "4ea7cdc2-b45e-4285-a397-192759daa1ed",
"serialNumber": "20171009110559",
"expertId": "ffebd7d9-eb8d-44cb-95fc-956ddcc56295000",
"expertUserId": 123123145883,
"expertName": "龚雨程医",
"hospitalId": "ED25EA3F3F52A102E040A8C00F01221B000",
"hospitalName": "浙江萧山医院",
"provinceId": 24,
"provinceName": "浙江",
"cityId": 552,
"cityName": "杭州",
"districtId": 3395,
"districtName": "萧山区",
"patientUserId": 2016811232127682,
"patientId": 75086705,
"patientName": "雨程患六",
"patientIdCard": "330104198010090024",
"patientBirthday": 339868800000,
"patientSex": 2,
"patientMobile": "15800000006",
"patientImg": "",
"patientCardType": 1,
"patientCardNo": "1234567890",
"patientProvinceId": 24,
"patientProvinceName": "浙江",
"patientCityId": 552,
"patientCityName": "杭州",
"patientDistrictId": 3395,
"patientDistrictName": "萧山区",
"patientAddress": "雨程患六常驻地址",
"patientSignature": "",
"serviceYear": "364个月",
"serviceCount": 26,
"nextSignNoticeDays": 0,
"signTime": 1507518415000,
"effectiveTime": 1483200000000,
"invalidTime": 1514649600000,
"isFollow": 1,
"isDefault": 1,
"remark": "",
"sourceId": "460",
"status": 1,
"gmtCreated": 1507518415000,
"gmtModified": 1508298163000,
"businessType": 1,
"censusRegisterAddress": "雨程患六户籍地址",
"fixTelephone": "0575",
"patientMobileBelonger": "龚雨程",
"serviceTimeUnit": "d",
"groupUuid": "689ff3b0-f198-4e50-bbc6-c6fb048dc5ed"
}
94 changes: 94 additions & 0 deletions json2oc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
let fs = require('fs-extra'),
path = require('path'),
strlist = 'list<string>',
intlist = 'list<int>';

(async () => {

let data = await readFile("data.json");
let json = JSON.parse(data);
console.log("//========start========")
parsejson(json);
})().catch(console.log);

function parsejson(json, isRecursive) {
for (let key in json) {
let tplstr = "`@property (nonatomic, ${hold}) ${type}${name};`",
hold = "copy",
type = "NSString *",
name = key,
value = json[key];
if(isRecursive) tplstr = "`// @property (nonatomic, ${hold}) ${type}${name};`";
if(isInt(value)){
hold = "assign";
type = "NSInteger "
console.log(eval(tplstr));
}else if(isFloat(value)){
hold = "assign";
type = "CGFloat "
console.log(eval(tplstr));
}else{
if(typeof value == 'object' && value != null) {
// 碰到数组或对象, 执行一次子递归
if(isArray(value)){
console.log(`//-------数组: ${key} 开始`);
parsejson(value[0], true);
}else {
console.log(`//-------对象: ${key} 开始`);
parsejson(value, true);
}
console.log(`//-------${key} 结束`)
}else{
console.log(eval(tplstr));
}
}
}
}

async function readFile(filename) {
let fullpath = path.resolve(filename);
return await fs.readFile(fullpath, 'utf8').catch(console.log);
}

function isInt(n){
return Number(n) === n && n % 1 === 0;
}

function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}

function isArray(n){
return Object.prototype.toString.call(n) === '[object Array]'
}

// async function json_to_code () {
// let copyright = "WeDoctor Group",
// projectname = "",
// author = "walker",
// model_path = "templates/model",
// task_path = "templates/httpclient",
// // 模板内容
// h_content = await getFileContent(model_path, 'template.h'),
// m_content1 = await getFileContent(model_path, 'template.m'),
// m_content2 = await getFileContent(model_path, 'templatebase.m'),
// h_task = await getFileContent(task_path, 'task.h'),
// m_task = await getFileContent(task_path, 'task.m');
// let out_model = "output_model_single",
// exist_file = []; // 重复定义的类, 只生成一次, 生成过一次就丢到这个数组里打标
// await fs.emptyDir(out_model); // 创建/清空输出文件夹
// entities.forEach(async (model, index) => {
// // 有些子类字段是一样的, 我们写脚本的时候把类名写成一样的, 此处会 pass 掉
// // 但只对单次执行脚本有效
// if(classCollect.filter(m=>m==model.className).length>1) {
// if(exist_file.includes(model)) return;
// exist_file.push(model);
// }
// let m_content = model.isRoot ? m_content2 : m_content1;
// // 输出路径
// let h_file = getPath(out_model, model.className+'.h'),
// m_file = getPath(out_model, model.className+'.m');
// await renderFile(h_file, eval(h_content)).catch(console.log);
// await renderFile(m_file, eval(m_content)).catch(console.log);
// });
// }

0 comments on commit a7c76ba

Please sign in to comment.