use V8 json parser with xhr/fetch webAPIs

The pure zig JSON parser didn't generate the same type of values than JS
JSON.parse command.
Using directly V8's JSON parser gives the assurance to have the right
JS types.
Moreover, it avoid data transformations between Zig and V8.
This commit is contained in:
Pierre Tachoire
2025-12-25 12:48:24 +01:00
parent d50f6b830a
commit 7cc2c2344e
2 changed files with 7 additions and 10 deletions

View File

@@ -120,15 +120,11 @@ pub fn getText(self: *const Response, page: *Page) !js.Promise {
pub fn getJson(self: *Response, page: *Page) !js.Promise { pub fn getJson(self: *Response, page: *Page) !js.Promise {
const body = self._body orelse ""; const body = self._body orelse "";
const value = std.json.parseFromSliceLeaky( const value = js.Value.fromJson(page.js, body) catch |err| {
std.json.Value,
page.call_arena,
body,
.{},
) catch |err| {
return page.js.rejectPromise(.{@errorName(err)}); return page.js.rejectPromise(.{@errorName(err)});
}; };
return page.js.resolvePromise(value); const pvalue = try value.persist();
return page.js.resolvePromise(pvalue);
} }
pub const JsApi = struct { pub const JsApi = struct {

View File

@@ -67,7 +67,7 @@ const ReadyState = enum(u8) {
const Response = union(ResponseType) { const Response = union(ResponseType) {
text: []const u8, text: []const u8,
json: std.json.Value, json: js.Value,
document: *Node.Document, document: *Node.Document,
}; };
@@ -254,8 +254,9 @@ pub fn getResponse(self: *XMLHttpRequest, page: *Page) !?Response {
const res: Response = switch (self._response_type) { const res: Response = switch (self._response_type) {
.text => .{ .text = data }, .text => .{ .text = data },
.json => blk: { .json => blk: {
const parsed = try std.json.parseFromSliceLeaky(std.json.Value, page.call_arena, data, .{}); const value = try js.Value.fromJson(page.js, data);
break :blk .{ .json = parsed }; const pvalue = try value.persist();
break :blk .{ .json = pvalue };
}, },
.document => blk: { .document => blk: {
const document = try page._factory.node(Node.Document{ ._proto = undefined, ._type = .generic }); const document = try page._factory.node(Node.Document{ ._proto = undefined, ._type = .generic });