Skip to content

api: Switch to fused json utf8 decoder for parsing response #695

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
May 22, 2024
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
10 changes: 9 additions & 1 deletion lib/api/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import '../log.dart';
import '../model/localizations.dart';
import 'exception.dart';

/// A fused JSON + UTF-8 decoder.
///
/// This object is an instance of [`_JsonUtf8Decoder`][1] which is
/// a fast-path present in VM and WASM standard library implementations.
///
/// [1]: https://github.com/dart-lang/sdk/blob/6c7452ac1530fe6161023c9b3007764ab26cc96d/sdk/lib/_internal/vm/lib/convert_patch.dart#L55
final jsonUtf8Decoder = const Utf8Decoder().fuse(const JsonDecoder());

/// A value for an API request parameter, to use directly without JSON encoding.
class RawParameter {
RawParameter(this.value);
Expand Down Expand Up @@ -105,7 +113,7 @@ class ApiConnection {
Map<String, dynamic>? json;
try {
final bytes = await response.stream.toBytes();
json = jsonDecode(utf8.decode(bytes));
json = jsonUtf8Decoder.convert(bytes) as Map<String, dynamic>?;
} catch (e) {
// We'll throw something below, seeing `json` is null.
}
Expand Down
Loading