Skip to content

Commit

Permalink
MOD: 修复json 值为null时无法解析的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
秦伟宽 committed Aug 16, 2022
1 parent fedee32 commit 872f899
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions scr/json-to-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function createMethod(keyName,object){

if (isArray(value)) {
let propertyName = replacePropertyName(key)
propertyName = firstLowerWord(propertyName);
var modelName = firstUpperWord(key)
printStr += `\\n ${propertyName} = $${propertyName}`
attString += ` List ${propertyName}Items = json['${key}'] ?? [];\n`
Expand All @@ -140,11 +141,13 @@ function createMethod(keyName,object){
}else if (isObject(value)) {
var modelName = firstUpperWord(key)
let propertyName = replacePropertyName(key)
propertyName = firstLowerWord(propertyName);
let reModelName = replacePropertyName(modelName)
printStr += `\\n ${propertyName} = $${propertyName}`
attString += ` ${propertyName} = ${reModelName}Model.fromJson(${methodName}(<String, dynamic>{}, json['${key}']));\n`
}else{
let propertyName = replacePropertyName(key)
propertyName = firstLowerWord(propertyName);
printStr += `\\n ${propertyName} = $${propertyName}`
attString += ` ${propertyName} = ${methodName}(${propertyName}, json['${key}']);\n`
}
Expand Down Expand Up @@ -275,6 +278,9 @@ function addArrayClass(value,keyName){
function createPropertyString(key,value){
var attString = '\n';
let propertyName = replacePropertyName(key)

propertyName = firstLowerWord(propertyName);

if (isBool(value)) {
attString += ` bool ${propertyName} = false;\n`;
}else if(isNumber(value)){
Expand All @@ -297,10 +303,12 @@ function createPropertyString(key,value){
let className = firstUpperWord(key)
let classNameModel = replacePropertyName(className)
attString += ` ${classNameModel}Model ${propertyName} = ${classNameModel}Model();\n`;
}else {
}else if (isNull(value)){
attString += ` var ${propertyName};\n`;
}else{
attString += ` var ${propertyName} = ${value};\n`;
}
return attString ;
return attString;
}

/// 数组内元素是否相等
Expand Down Expand Up @@ -437,6 +445,15 @@ function firstUpperWord(word){
return lowerWord.replace(lowerWord.charAt(0),lowerWord.charAt(0).toUpperCase());
}

///首字母小写
function firstLowerWord(word){
if (word.charAt(0) >= 'A' && word.charAt(0) <= 'Z') {
return word.replace(word.charAt(0),word.charAt(0).toLowerCase());
}
return word
}


function isBool(value){
return typeof value === 'boolean';
}
Expand All @@ -454,8 +471,15 @@ function isArray(value){
}

function isObject(value){
if (isNull(value)) {
return false
}
return value.constructor.toString().indexOf("Object")>-1;
}

function isNull(value){
return value === null;
}



0 comments on commit 872f899

Please sign in to comment.