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

Possible to 'pass-through' values that are not proper JSON? #669

Closed
fkettelhoit opened this issue Jul 15, 2020 · 3 comments
Closed

Possible to 'pass-through' values that are not proper JSON? #669

fkettelhoit opened this issue Jul 15, 2020 · 3 comments

Comments

@fkettelhoit
Copy link

I am working with lots of different data classes that need to get serialized to/from Map<String, Object>, which mostly contain serializable data types as values, but also functions and other classes that cannot and should not be converted to/from JSON-compatible types but just 'passed through' as they are (but not ignored either). Since I'm not using the resulting Map as a proper JSON, I don't want the serializer to touch any type that it does not know how to handle but not drop it during a 'serialization' round trip either. Is it possible to do something like this using json_serializable?

I tried to use the following JsonConverter:

class Passthrough<T> implements JsonConverter<T, Object> {
  const Passthrough();

  @override
  T fromJson(Object json) => json as T;

  @override
  Object toJson(T json) => json;
}

typedef SomeFnType = void Function();

@JsonSerializable()
class Foo {
  @JsonKey()
  @Passthrough()
  final SomeFnType someFn;

  Foo(this.someFn);
}

However this causes the build runner to fail (I assume because the resulting type is still not JSON-compatible):

[SEVERE]json_serializable:json_serializable on lib/src/some_file.dart: Could not generate `fromJson` code for `someFn`.
None of the provided `TypeHelper` instances support the defined type.
package:some_package/src/some_file.dart:44:20
   ╷
44 │   final SomeFnType someFn;
   │                    ^^^^^^
   ╵

Is it possible to do this using TypeHelpers? If so, is there any documentation on how to provide custom TypeHelpers?

@kevmoo
Copy link
Collaborator

kevmoo commented Jul 15, 2020

Have you tried a custom toJson that just returns the value?

@kevmoo kevmoo closed this as completed Jul 15, 2020
@fkettelhoit
Copy link
Author

That wouldn't work since I would have to write a separate toJson for every type (and the types are usually function types so there's not much opportunity for sharing a toJson implementation). I tried passing an identity function but even that doesn't work since JsonKey(toJson: ...) only works with a dynamically typed toJson implementation, not a generically typed one and thus the generated files are full of type errors.

Is it still possible to use TypeHelpers in the current version of json_serializable? I read that JsonConverters were introduced to reduce the need for TypeHelpers, but I don't think JsonConverters or toJson work in this scenario, so any pointers to relevant documentation about TypeHelpers would be appreciated.

@hacker1024
Copy link

#783

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

3 participants