Skip to content

Commit

Permalink
use the string replace's method the resolve the int and double problem.
Browse files Browse the repository at this point in the history
replace the generate code's num to int.
  • Loading branch information
CaiJingLong committed Aug 11, 2018
1 parent 65cc06f commit 6f007e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
40 changes: 36 additions & 4 deletions lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Generator {
String entityName;
Version version;

Generator(this.jsonString, [this.entityName, this.version = Version.v0]);
Generator(this.jsonString, [this.entityName, this.version = Version.v0]) {
this.jsonString = convertJsonString(jsonString);
}

List<DefaultTemplate> templateList = [];

Expand Down Expand Up @@ -45,12 +47,14 @@ class Generator {
fieldList.forEach((filed) {
if (filed is MapField) {
// filed.typeString
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.map), className: filed.typeString);
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.map), className: filed.typeString);
templateList.add(template);
refreshTemplate(template);
} else if (filed is ListField) {
if (filed.childIsObject) {
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.list[0]), className: filed.typeName);
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.list[0]), className: filed.typeName);
templateList.add(template);
refreshTemplate(template);
}
Expand All @@ -60,7 +64,8 @@ class Generator {

String get fileName => camelCase2UnderScoreCase(entityName);

static const String importString = "import 'package:json_annotation/json_annotation.dart';";
static const String importString =
"import 'package:json_annotation/json_annotation.dart';";

String get header => """$importString
Expand All @@ -76,3 +81,30 @@ String camelCase2UnderScoreCase(String name) {
return "_" + str.toLowerCase();
});
}

String convertJsonString(String jsonString) {
var numberReg = RegExp(r"[0-9]\.[0-9]+");

//匹配小数数字正则
var allMatch = numberReg.allMatches(jsonString).toList();

for (var i = 0; i < allMatch.length; i++) {
//是一个小数数字
var m = allMatch[i];
var s = m.group(0);

// if (double.tryParse(s) is double) {
// // 说明是真实的小数
// // 这里直接匹配下一个
// print("$s is double");
// continue;
// }

// print("$s is int");

// 应该是double,但由于js的原因被识别成了整数数,这里对这种数据进行处理,将这里的最后一位从0替换为1
s = s.replaceRange(s.length - 1, s.length, "5");
jsonString = jsonString.replaceRange(m.start, m.end, s);
}
return jsonString;
}
3 changes: 2 additions & 1 deletion lib/json_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ void main() async {
ButtonElement formatButton = querySelector("#format");
formatButton.onClick.listen((click) {
String pretty;
pretty = convertJsonString(jsonInput.value);
try {
pretty = formatJson(jsonInput.value);
pretty = formatJson(pretty);
} on Exception {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class FieldHelper {
} else if (v is String) {
list.add(SimpleField("String", k));
} else if (v is int) {
list.add(SimpleField("num", k));
list.add(SimpleField("int", k));
} else if (v is double) {
list.add(SimpleField("double", k));
} else if (v is bool) {
Expand Down Expand Up @@ -257,7 +257,7 @@ class ListField extends Field {
type = "${ListField(item, "").typeString}";
} else if (item is Map<String, dynamic>) {
type = "${firstLetterUpper(nameString)}";
} else if (item is num) {
} else if (item is int) {
type = "int";
} else if (item is double) {
type = "double";
Expand Down

0 comments on commit 6f007e8

Please sign in to comment.