qwik icon indicating copy to clipboard operation
qwik copied to clipboard

[✨] Custom Class serialization

Open swwind opened this issue 1 year ago • 1 comments

Is your feature request related to a problem?

I'm using MongoDB and every request contains one or more ObjectId class object which can not be serialized.

And I'm lazy to write a bunch of .maps and .toString()s to convert those fields to string manually.

Describe the solution you'd like

Write a bunch of .maps and .toString()s after every request is too terrible for me.

I'm looking for those solutions

  1. Add a serializer to handle this ObjectId in mongodb (or in bson) just like URL and Date. (ideal but a waste for non-mongodb user)
  2. Implement above serializer as a plugin for runtime. (possible?)
  3. Support custom serialization for any class, such as using toString() or qSerialize() function automatically. (however, using toString() converts any class object into string, which requires additional type transform in useLoader() and useAction, etc.)

Describe alternatives you've considered

n/a

Additional context

No response

swwind avatar Mar 19 '23 08:03 swwind

https://github.com/bluesky-social/atproto has a class object BlobRef that is also not serializable, would love to write a custom serializer instead of bugging the authors to change it.

mikuhl-dev avatar May 08 '23 00:05 mikuhl-dev

Facing a similar challenge with Prisma returning a Decimal object.

Would it be possible to support adding custom serializers via a vite plugin?

guzart avatar Nov 26 '23 05:11 guzart

Custom serializers is a complex problem, which is why we have been punting it. Perhaps you can send a PR to add the Decimal Object?

mhevery avatar Dec 10 '23 21:12 mhevery

I think most custom serializers can be avoided by putting methods in the store that on invocation check if the desired object exists and if not, create it with noSerialize before doing their own work.

The technique I describe here can be useful for that.

I don't think Qwik should ship with support for random libraries but maybe the $sync proposal can help with doing actual custom serialization

wmertens avatar Dec 10 '23 22:12 wmertens