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

fetch - How should JSON objects be passed into JavaScript #546

Closed
kissxrl opened this issue Oct 29, 2023 · 2 comments
Closed

fetch - How should JSON objects be passed into JavaScript #546

kissxrl opened this issue Oct 29, 2023 · 2 comments
Assignees
Labels

Comments

@kissxrl
Copy link

kissxrl commented Oct 29, 2023

#535

I am currently using ethers and using it in v8, but ethers will use fetch

How to implement fetch in C # when accessing the network

using var engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableTaskPromiseConversion);
engine.AddHostObject("fetch", Response.fetch);


public sealed class Response {
    private string _html;
    public Task<string> text() => Task.FromResult(_html);
    public static async Task<Response> fetch(string url, IScriptObject obj = null){
        if (obj != null) ...............
        using var client = new HttpClient();
        return new Response { _html = await client.GetStringAsync(url) };
    }
}

I added a method, but JavaScript cannot recognize this object

public Task<object> json()
{
        var jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(_html);
        return Task.FromResult(jsonObject);
}
@ClearScriptLib
Copy link
Collaborator

Hi @kissxrl,

Try something like this:

public Task<object> json() => Task.FromResult<object>(ScriptEngine.Current.Script.JSON.parse(_html));

Good luck!

@ClearScriptLib
Copy link
Collaborator

Please reopen this issue if you have additional thoughts or questions about this topic. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants