mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 14:33:47 +00:00
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:
@@ -120,15 +120,11 @@ pub fn getText(self: *const Response, page: *Page) !js.Promise {
|
||||
|
||||
pub fn getJson(self: *Response, page: *Page) !js.Promise {
|
||||
const body = self._body orelse "";
|
||||
const value = std.json.parseFromSliceLeaky(
|
||||
std.json.Value,
|
||||
page.call_arena,
|
||||
body,
|
||||
.{},
|
||||
) catch |err| {
|
||||
const value = js.Value.fromJson(page.js, body) catch |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 {
|
||||
|
||||
@@ -67,7 +67,7 @@ const ReadyState = enum(u8) {
|
||||
|
||||
const Response = union(ResponseType) {
|
||||
text: []const u8,
|
||||
json: std.json.Value,
|
||||
json: js.Value,
|
||||
document: *Node.Document,
|
||||
};
|
||||
|
||||
@@ -254,8 +254,9 @@ pub fn getResponse(self: *XMLHttpRequest, page: *Page) !?Response {
|
||||
const res: Response = switch (self._response_type) {
|
||||
.text => .{ .text = data },
|
||||
.json => blk: {
|
||||
const parsed = try std.json.parseFromSliceLeaky(std.json.Value, page.call_arena, data, .{});
|
||||
break :blk .{ .json = parsed };
|
||||
const value = try js.Value.fromJson(page.js, data);
|
||||
const pvalue = try value.persist();
|
||||
break :blk .{ .json = pvalue };
|
||||
},
|
||||
.document => blk: {
|
||||
const document = try page._factory.node(Node.Document{ ._proto = undefined, ._type = .generic });
|
||||
|
||||
Reference in New Issue
Block a user