Skip to content

JSInterop: Consider JSType.Json for conveniently marshaling data #77784

@lambdageek

Description

@lambdageek

consider some code like this:

public record Todo(int id, int userId, string title, bool completed);

public class SampleApp {
  public static async Task<Todo[]> GetTodos() { ... }
}

Right now to make use of something like that from JavaScript, we have to write some boilerplate:

public class SampleApp {
  [JSExport]
  public static Task<string> GetTodosExportWrapper() => JsonSerializer.SerializeAsync (await GetTodos());
}
   // in main.js
   const exports = await getAssemblyExports(...);
   const json = JSON.parse (await exports.SampleApp.GetTodosExportWrapper());

It would be nice if we had a convenience marshaler for moving Json blobs around:

  [JSExport]
  [return:JSMarshalAs<JSType.Promise<JSType.Json>>]
  public static Task<Todo[]> GetTodos() { ... }
  // in main.js
  const exports = await getAssemblyExports(...);
  const json = await exports.SampleApp.GetTodos ();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions