Skip to content
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

Decoding JSON to generic class #26

Closed
sabin26 opened this issue Sep 7, 2023 · 2 comments
Closed

Decoding JSON to generic class #26

sabin26 opened this issue Sep 7, 2023 · 2 comments

Comments

@sabin26
Copy link

sabin26 commented Sep 7, 2023

Hey, I am trying to parse JSON string to dart object using Model.fromJson method inside a squadron worker.

This should work fine for a given model class (not tested though).

@freezed
class Model with _$Model {
  const factory Model({
    required final int id,
    required final String name,
  }) = _Model;
  factory Model.fromJson(final Map<String, dynamic> json) =>
      _$ModelFromJson(json);
}

@SquadronService()
class MyService {
  @SquadronMethod()
  FutureOr<Model> loadModel(final String data) {
    final json = jsonDecode(data) as Map<String, Object?>;
    return Model.fromJson(json);
  }
}

But what I want to do is parse the JSON string to a generic dart object with something as:

@SquadronService()
class MyService {
  @SquadronMethod()
  FutureOr<T> load({
     required final String data,
     required final T Function(Map<String, Object?>) fromJson,
   } ) {
    final json = jsonDecode(data) as Map<String, Object?>;
    return fromJson(json);
  }
}

Here, I can't pass fromJson method as an argument. It doesn't let the build runner pass with squadron_builder. Previously when I was using dart:isolate, I was able to pass this using SendPort.

My use-case is more complicated than this, but I am stuck by this issue. One way to solve this is by returning the Map<String, Object?> and use fromJson from main thread. However, since I am offloading my task, I wanted to parse the Json to dart object from the worker thread.

@sabin26 sabin26 changed the title Decoding JSON for generic class Decoding JSON to generic class Sep 7, 2023
@d-markey
Copy link
Owner

d-markey commented Sep 7, 2023

Hello,

I've pushed changes to support your use-case.

Plase note that this will probably not work on Web (I haven't tested, but I guess functions cannot cross Web Worker boundaries. Even if they could, you'd have to implement a marshaler so that your T instances can cross the border too. The overhead might negate the advantage of parsing and hydrating in a separate Web Worker).

Please also note that it may also not work on all platforms -- it's a bit difficult to tell exactly what is sendable across 2 isolates. Some info is available from Dart's doc on isolates as well as the doc for send().

Publishing the update now!

Regards

@d-markey
Copy link
Owner

d-markey commented Sep 7, 2023

The doc for send() also mentions bug http://dartbug.com/36983 / dart-lang/sdk#36983. This means if you provide a closure to the worker, it may end up capturing too many things. If this happens, maybe it can be worked around by passing a top-level function, or a static method / constructor tear-off (which is probably always preferable, anyways).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants