Skip to content

Closes #43 Decode JSON response as UTF-8 by default #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/src/generators/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ class $clientName {
return Future.value(response);
}

// ------------------------------------------
// METHOD: _jsonDecode
// ------------------------------------------

dynamic _jsonDecode(http.Response r) {
return json.decode(utf8.decode(r.bodyBytes));
}

// ------------------------------------------
// METHOD: _request
// ------------------------------------------
Expand Down Expand Up @@ -880,7 +888,7 @@ class $clientName {
object: (s) {
// Handle deserialization of single object
if (s.ref != null || returnType.startsWith('Union')) {
decoder = "return $returnType.fromJson(json.decode(r.body));";
decoder = "return $returnType.fromJson(_jsonDecode(r));";
} else {
// Just return the whole response and allow user to handle
if (returnType == 'dynamic') {
Expand All @@ -893,7 +901,7 @@ class $clientName {
// Handle deserialization for array of objects
if (s.items.ref != null) {
decoder = """
final list = json.decode(r.body) as List;
final list = _jsonDecode(r) as List;
return list.map((e) => ${s.items.ref}.fromJson(e)).toList();
""";
}
Expand All @@ -902,7 +910,7 @@ class $clientName {
// Handle deserialization for map of objects
if (s.valueSchema?.ref != null) {
decoder = """
final map = json.decode(r.body) as Map<String, dynamic>;
final map = _jsonDecode(r) as Map<String, dynamic>;
return map.map((k, v) => MapEntry(k, ${s.valueSchema?.ref}.fromJson(v)));
""";
}
Expand All @@ -915,9 +923,9 @@ class $clientName {
} else if (returnType == 'Uint8List') {
decoder = "return r.bodyBytes;";
} else if (returnType.contains('List') || returnType.contains('Map')) {
decoder = "return $returnType.from(json.decode(r.body));";
decoder = "return $returnType.from(_jsonDecode(r));";
} else {
decoder = "return json.decode(r.body);";
decoder = "return _jsonDecode(r);";
}
}
}
Expand Down