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

Provide a way to eagerly marshal (snapshot) all properties to js object. #399

Open
reitowo opened this issue Nov 22, 2024 · 3 comments
Open
Assignees

Comments

@reitowo
Copy link

reitowo commented Nov 22, 2024

When a function returns a class object, it can't be serialized to json and send it across RPC, because all of properties are dynamically loaded. Is there any way to marshal it to a completely JS object? Because compiling into a module doesn't work well.

It will be nice if can provide a snapshot helper to take the snapshot of a reference and let it pass with rpc.

@reitowo reitowo changed the title Unable to JSON.stringify Provide a way to eagerly marshal all properties to js object. Nov 22, 2024
@reitowo reitowo changed the title Provide a way to eagerly marshal all properties to js object. Provide a way to eagerly marshal (snapshot) all properties to js object. Nov 23, 2024
@jasongin
Copy link
Member

jasongin commented Jan 10, 2025

If you can use a struct instead of a class, then the object will be marshalled by value between .NET and JS and should get serialized normally.

I'm not sure why it doesn't work for a class (which is marshalled by reference). But one thing you could try is to define a toJSON() method on the .NET class, which should then get called by the JS serializer.

@reitowo
Copy link
Author

reitowo commented Jan 11, 2025

Sadly I can't.

It doesn't work probably because the class object doesn't actually have properties defined on it, and it just a proxy? The js JSON.stringify will then not able to serialize anything because theres no fields or properties to serialize.

Defining a toJson seems ok but I have to add a base class for everyone, and it still a pain in the ass, because of #400. If I pass the serialized string to other process through IPC, I cannot use it using the existing type definition otherwise I need to load dotnet runtime for every subprocess, which seems not ideal. I then have to manually copy these ts definitions.

It probably simpler if I can just import type from generated code, instead of ambient importing a whole dotnet runtime.

Or it will not, because serialized string will not fit into the dotnet types, we probably should also generate a pure js types ts definition in seperated files, and to be imported without importing dotnet runtime, that seems a better way to resolve #400.

@jasongin
Copy link
Member

It doesn't work probably because the class object doesn't actually have properties defined on it, and it just a proxy? The js JSON.stringify will then not able to serialize anything because theres no fields or properties to serialize.

I'll investigate why that doesn't work. There might be a way to fix the proxy to allow it to work with JSON.stringify(). Or have the marshaller automatically add a toJson() method for it.

@jasongin jasongin self-assigned this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants