Skip to content

Commit d4255f7

Browse files
author
Matthijs Heuten
committed
fix: add jsonable property
1 parent aa8b96f commit d4255f7

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
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

lib/core/command.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
),

lib/core/dart_declaration.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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';

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: json_to_model
22
description: Generate model class from Json file.
3-
version: 3.2.3
3+
version: 3.2.4
44
homepage: https://github.com/fadhilx/json_to_model
55

66
environment:

test/jsonable_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

test/jsons/jsonable.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"num" : "#num",
3+
"map" : "#Map<String,dynamic>",
4+
"class" : {
5+
"int" : 0
6+
}
7+
}

0 commit comments

Comments
 (0)