diff options
author | 2022-02-27 04:54:48 -0800 | |
---|---|---|
committer | 2022-02-27 04:54:48 -0800 | |
commit | 09146d1c115f7307b545dae0c8cfcec2a4342322 (patch) | |
tree | 84cd144dff7b770eeb937b4f0d86d45fc206e648 | |
parent | 3f9c5edbf5d3951b13de186c24ce35189d25972d (diff) | |
download | bun-09146d1c115f7307b545dae0c8cfcec2a4342322.tar.gz bun-09146d1c115f7307b545dae0c8cfcec2a4342322.tar.zst bun-09146d1c115f7307b545dae0c8cfcec2a4342322.zip |
Update json_parser.zig
-rw-r--r-- | src/json_parser.zig | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/json_parser.zig b/src/json_parser.zig index ee596eaf7..c2236228f 100644 --- a/src/json_parser.zig +++ b/src/json_parser.zig @@ -665,7 +665,45 @@ var empty_string_data = Expr.Data{ .e_string = &empty_string }; var empty_object_data = Expr.Data{ .e_object = &empty_object }; var empty_array_data = Expr.Data{ .e_array = &empty_array }; -pub fn ParseJSON(source: *const logger.Source, log: *logger.Log, allocator: std.mem.Allocator) !Expr { +/// Parse JSON +/// This leaves UTF-16 strings as UTF-16 strings +/// The JavaScript Printer will handle escaping strings if necessary +pub fn ParseJSON( + source: *const logger.Source, + log: *logger.Log, + allocator: std.mem.Allocator, +) !Expr { + var parser = try JSONParser.init(allocator, source.*, log); + switch (source.contents.len) { + // This is to be consisntent with how disabled JS files are handled + 0 => { + return Expr{ .loc = logger.Loc{ .start = 0 }, .data = empty_object_data }; + }, + // This is a fast pass I guess + 2 => { + if (strings.eqlComptime(source.contents[0..1], "\"\"") or strings.eqlComptime(source.contents[0..1], "''")) { + return Expr{ .loc = logger.Loc{ .start = 0 }, .data = empty_string_data }; + } else if (strings.eqlComptime(source.contents[0..1], "{}")) { + return Expr{ .loc = logger.Loc{ .start = 0 }, .data = empty_object_data }; + } else if (strings.eqlComptime(source.contents[0..1], "[]")) { + return Expr{ .loc = logger.Loc{ .start = 0 }, .data = empty_array_data }; + } + }, + else => {}, + } + + return try parser.parseExpr(false, false); +} + +/// Parse JSON +/// This eagerly transcodes UTF-16 strings into UTF-8 strings +/// Use this when the text may need to be reprinted to disk as JSON (and not as JavaScript) +/// Eagerly converting UTF-8 to UTF-16 can cause a performance issue +pub fn ParseJSONUTF8( + source: *const logger.Source, + log: *logger.Log, + allocator: std.mem.Allocator, +) !Expr { var parser = try JSONParser.init(allocator, source.*, log); switch (source.contents.len) { // This is to be consisntent with how disabled JS files are handled @@ -708,7 +746,7 @@ pub fn ParseJSONForMacro(source: *const logger.Source, log: *logger.Log, allocat else => {}, } - return try parser.parseExpr(false, true); + return try parser.parseExpr(false, false); } pub const JSONParseResult = struct { |