File tree Expand file tree Collapse file tree 6 files changed +46
-5
lines changed Expand file tree Collapse file tree 6 files changed +46
-5
lines changed Original file line number Diff line number Diff line change 1+ ## 3.2.4
2+
3+ - Added isJsonable property to dart time to fix .toJson/.fromJson generation on unsupported types
14## 3.2.3
25
36- Loosen factory type requirements. If type is not found the factory will return a ` null ` value
Original file line number Diff line number Diff line change @@ -174,6 +174,10 @@ final List<Command> valueCommands = [
174174 final subject = testSubject as String ;
175175 self.type = subject.substring (1 );
176176 self.explicitTypeOverride = true ;
177+ self.isJsonable = self.type? .startsWith ('Map' ) != true &&
178+ self.type? .startsWith ("List" ) != true &&
179+ self.type != 'dynamic' &&
180+ self.type != 'num' ;
177181 return self;
178182 },
179183 ),
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class DartDeclaration {
99 List <String > imports = [];
1010 String ? type;
1111 bool explicitTypeOverride = false ;
12+ bool isJsonable = true ;
1213 dynamic jsonValue;
1314 String ? originalName;
1415 String ? name;
@@ -96,10 +97,7 @@ class DartDeclaration {
9697 conversion = '$jsonVar $isNullableString .toString()' ;
9798 } else if (type == 'double' ) {
9899 conversion = '($jsonVar as num).toDouble()' ;
99- } else if (explicitTypeOverride &&
100- type? .startsWith ('Map' ) != true &&
101- type? .startsWith ("List" ) != true &&
102- type != 'dynamic' ) {
100+ } else if (explicitTypeOverride && isJsonable) {
103101 conversion = modelFromJson (jsonVar);
104102 } else {
105103 conversion = '$jsonVar as $type ' ;
Original file line number Diff line number Diff line change 11name : json_to_model
22description : Generate model class from Json file.
3- version : 3.2.3
3+ version : 3.2.4
44homepage : https://github.com/fadhilx/json_to_model
55
66environment :
Original file line number Diff line number Diff line change 1+ import 'dart:convert' ;
2+ import 'dart:io' ;
3+
4+ import 'package:json_to_model/core/json_model.dart' ;
5+ import 'package:json_to_model/core/model_template.dart' ;
6+ import 'package:test/expect.dart' ;
7+ import 'package:test/scaffolding.dart' ;
8+
9+ void main () {
10+ test ('Check if .fromJson is not used by a type that is not jsonable' , () {
11+ final content = json.decode (File ('test/jsons/jsonable.json' ).readAsStringSync ()) as Map <String , dynamic >;
12+
13+ final jsonModel = JsonModel .fromMap (
14+ 'jsonable' ,
15+ content,
16+ relativePath: './' ,
17+ packageName: 'core' ,
18+ indexPath: 'index.dart' ,
19+ );
20+
21+ final output = modelFromJsonModel (jsonModel);
22+
23+ print (output);
24+
25+ expect (output, isNot (contains ("num.fromJson" )));
26+ expect (output, isNot (contains ("Map<String,dynamic.fromJson" )));
27+ expect (output, contains ("Class.fromJson" ));
28+ });
29+ }
Original file line number Diff line number Diff line change 1+ {
2+ "num" : " #num" ,
3+ "map" : " #Map<String,dynamic>" ,
4+ "class" : {
5+ "int" : 0
6+ }
7+ }
You can’t perform that action at this time.
0 commit comments