Skip to content

Commit fd5fdea

Browse files
committed
Closes #34 Decode JSON response as UTF-8 by default
1 parent 19a2643 commit fd5fdea

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/src/generators/client.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ class $clientName {
258258
return Future.value(response);
259259
}
260260
261+
// ------------------------------------------
262+
// METHOD: _jsonDecode
263+
// ------------------------------------------
264+
265+
dynamic _jsonDecode(http.Response r) {
266+
return json.decode(utf8.decode(r.bodyBytes));
267+
}
268+
261269
// ------------------------------------------
262270
// METHOD: _request
263271
// ------------------------------------------
@@ -880,7 +888,7 @@ class $clientName {
880888
object: (s) {
881889
// Handle deserialization of single object
882890
if (s.ref != null || returnType.startsWith('Union')) {
883-
decoder = "return $returnType.fromJson(json.decode(r.body));";
891+
decoder = "return $returnType.fromJson(_jsonDecode(r));";
884892
} else {
885893
// Just return the whole response and allow user to handle
886894
if (returnType == 'dynamic') {
@@ -893,7 +901,7 @@ class $clientName {
893901
// Handle deserialization for array of objects
894902
if (s.items.ref != null) {
895903
decoder = """
896-
final list = json.decode(r.body) as List;
904+
final list = _jsonDecode(r) as List;
897905
return list.map((e) => ${s.items.ref}.fromJson(e)).toList();
898906
""";
899907
}
@@ -902,7 +910,7 @@ class $clientName {
902910
// Handle deserialization for map of objects
903911
if (s.valueSchema?.ref != null) {
904912
decoder = """
905-
final map = json.decode(r.body) as Map<String, dynamic>;
913+
final map = _jsonDecode(r) as Map<String, dynamic>;
906914
return map.map((k, v) => MapEntry(k, ${s.valueSchema?.ref}.fromJson(v)));
907915
""";
908916
}
@@ -915,9 +923,9 @@ class $clientName {
915923
} else if (returnType == 'Uint8List') {
916924
decoder = "return r.bodyBytes;";
917925
} else if (returnType.contains('List') || returnType.contains('Map')) {
918-
decoder = "return $returnType.from(json.decode(r.body));";
926+
decoder = "return $returnType.from(_jsonDecode(r));";
919927
} else {
920-
decoder = "return json.decode(r.body);";
928+
decoder = "return _jsonDecode(r);";
921929
}
922930
}
923931
}

0 commit comments

Comments
 (0)