google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.54k stars 392 forks source link

[doc request] Compile-time and Runtime Serializable Object Check #1431

Closed pitazzo closed 4 weeks ago

pitazzo commented 1 month ago

Hi,

I have a question regarding the usage of the package.

1.  Is there any way to check at compile-time if an object is serializable using json_serializable?
2.  If not, is there a way to check at runtime if an object is serializable without using reflection?

To provide more context, I want to achieve something similar to checking if a class inherits from another class. However, since json_serializable works with decorators instead of inheritance, I don’t see a clear way to perform this check.

I’m posting this as an issue rather than asking on StackOverflow because I believe this is an important question that, in my opinion, should be explicitly documented. If it is documented, I haven’t been able to find it.

Any guidance on this would be greatly appreciated.

Thanks!

kevmoo commented 4 weeks ago

Short of calling toJson() on an object with dynamic invocation and seeing if it throws, there's no way to do this.

What's your scenario?