| author | |
| committer | |
| log | b8b264d1de2ce1421d815cbc05cf8a662e627b3b |
| tree | 45b1e30a9b4797d4885ab845e003fd534ef7f373 |
| parent | 02fb7045dc16c791436f7f4127c8a072e7acdff8 |
4 files changed, 40 insertions(+), 18 deletions(-)
json.zig+14-6| ... | ... | @@ -3,20 +3,21 @@ const string = []const u8; |
| 3 | 3 | const extras = @import("extras"); |
| 4 | 4 | const tracer = @import("tracer"); |
| 5 | 5 | const intrusive_parser = @import("intrusive-parser"); |
| 6 | const nio = @import("nio"); | |
| 6 | 7 | |
| 7 | 8 | pub const Error = error{ OutOfMemory, EndOfStream, MalformedJson }; |
| 8 | 9 | pub const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex); |
| 9 | 10 | |
| 10 | pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) (@TypeOf(inreader).Error || Error)!Document { | |
| 11 | pub fn parse(alloc: std.mem.Allocator, path: string, inreadable: anytype, options: Parser.Options) (Instance(@TypeOf(inreadable)).ReadError || Error)!Document { | |
| 11 | 12 | const t = tracer.trace(@src(), "", .{}); |
| 12 | 13 | defer t.end(); |
| 13 | 14 | |
| 14 | 15 | _ = path; |
| 15 | 16 | |
| 16 | var p = try Parser.init(alloc, inreader.any(), options); | |
| 17 | var p = try Parser.init(alloc, inreadable.anyReadable(), options); | |
| 17 | 18 | defer p.deinit(); |
| 18 | 19 | |
| 19 | const root = try parseElementPrecise(alloc, &p, @TypeOf(inreader).Error || Error); | |
| 20 | const root = try parseElementPrecise(alloc, &p, Instance(@TypeOf(inreadable)).ReadError || Error); | |
| 20 | 21 | if (p.parser.avail() > 0) return error.MalformedJson; |
| 21 | 22 | const data = try p.parser.data.toOwnedSlice(alloc); |
| 22 | 23 | |
| ... | ... | @@ -26,9 +27,16 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: |
| 26 | 27 | }; |
| 27 | 28 | } |
| 28 | 29 | |
| 30 | fn Instance(T: type) type { | |
| 31 | return switch (@typeInfo(T)) { | |
| 32 | .pointer => |info| info.child, | |
| 33 | else => T, | |
| 34 | }; | |
| 35 | } | |
| 36 | ||
| 29 | 37 | pub fn parseFromSlice(alloc: std.mem.Allocator, path: string, input: string, options: Parser.Options) !Document { |
| 30 | var fbs = std.io.fixedBufferStream(input); | |
| 31 | return parse(alloc, path, fbs.reader(), options); | |
| 38 | var fbs: nio.FixedBufferStream(string) = .init(input); | |
| 39 | return parse(alloc, path, &fbs, options); | |
| 32 | 40 | } |
| 33 | 41 | |
| 34 | 42 | fn parseElementPrecise(alloc: std.mem.Allocator, p: *Parser, comptime E: type) E!ValueIndex { |
| ... | ... | @@ -253,7 +261,7 @@ pub const Parser = struct { |
| 253 | 261 | support_trailing_commas: bool, |
| 254 | 262 | maximum_depth: u16, |
| 255 | 263 | |
| 256 | pub fn init(allocator: std.mem.Allocator, any: std.io.AnyReader, options: Options) !Parser { | |
| 264 | pub fn init(allocator: std.mem.Allocator, any: nio.AnyReadable, options: Options) !Parser { | |
| 257 | 265 | var p: Parser = .{ |
| 258 | 266 | .parser = intrusive_parser.Parser.init(allocator, any, @intFromEnum(Value.Tag.string)), |
| 259 | 267 | .support_trailing_commas = options.support_trailing_commas, |
licenses.txt+8| ... | ... | @@ -1,10 +1,18 @@ |
| 1 | 1 | MIT: |
| 2 | 2 | = https://spdx.org/licenses/MIT |
| 3 | 3 | - This |
| 4 | - git https://github.com/nektro/zig-errno | |
| 4 | 5 | - git https://github.com/nektro/zig-extras |
| 6 | - git https://github.com/nektro/zig-libc | |
| 7 | - git https://github.com/nektro/zig-sys-libc | |
| 5 | 8 | - git https://github.com/nektro/zig-tracer |
| 6 | 9 | - git https://github.com/nst/JSONTestSuite |
| 7 | 10 | |
| 8 | 11 | MPL-2.0: |
| 9 | 12 | = https://spdx.org/licenses/MPL-2.0 |
| 10 | 13 | - git https://github.com/nektro/zig-intrusive-parser |
| 14 | - git https://github.com/nektro/zig-nfs | |
| 15 | - git https://github.com/nektro/zig-nio | |
| 16 | ||
| 17 | Unspecified: | |
| 18 | - system_lib c |
test.zig+15-12| ... | ... | @@ -1,12 +1,15 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const json = @import("json"); |
| 3 | 3 | const build_options = @import("build_options"); |
| 4 | const nfs = @import("nfs"); | |
| 5 | const nio = @import("nio"); | |
| 6 | ||
| 4 | 7 | const JSONTestSuite_root = build_options.JSONTestSuite_root; |
| 5 | 8 | |
| 6 | 9 | fn parse_full(buffer: []const u8) !void { |
| 7 | 10 | const alloc = std.testing.allocator; |
| 8 | var fbs = std.io.fixedBufferStream(buffer); | |
| 9 | var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 11 | var fbs: nio.FixedBufferStream([]const u8) = .init(buffer); | |
| 12 | var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 10 | 13 | defer doc.deinit(alloc); |
| 11 | 14 | } |
| 12 | 15 | |
| ... | ... | @@ -260,11 +263,11 @@ test { |
| 260 | 263 | ); |
| 261 | 264 | } |
| 262 | 265 | |
| 263 | fn expectPass(path: []const u8) !void { | |
| 266 | fn expectPass(path: [:0]const u8) !void { | |
| 264 | 267 | const alloc = std.testing.allocator; |
| 265 | var file = try std.fs.cwd().openFile(path, .{}); | |
| 268 | const file = try nfs.cwd().openFile(path, .{}); | |
| 266 | 269 | defer file.close(); |
| 267 | var doc = try json.parse(alloc, path, file.reader(), .{ .support_trailing_commas = false, .maximum_depth = 100 }); | |
| 270 | var doc = try json.parse(alloc, path, file, .{ .support_trailing_commas = false, .maximum_depth = 100 }); | |
| 268 | 271 | defer doc.deinit(alloc); |
| 269 | 272 | } |
| 270 | 273 | |
| ... | ... | @@ -366,11 +369,11 @@ test { try expectPass(JSONTestSuite_root ++ "/y_structure_true_in_array.json"); |
| 366 | 369 | test { try expectPass(JSONTestSuite_root ++ "/y_structure_whitespace_array.json"); } |
| 367 | 370 | // zig fmt: on |
| 368 | 371 | |
| 369 | fn expectFail(path: []const u8) !void { | |
| 372 | fn expectFail(path: [:0]const u8) !void { | |
| 370 | 373 | const alloc = std.testing.allocator; |
| 371 | var file = try std.fs.cwd().openFile(path, .{}); | |
| 374 | const file = try nfs.cwd().openFile(path, .{}); | |
| 372 | 375 | defer file.close(); |
| 373 | var doc = json.parse(alloc, path, file.reader(), .{ .support_trailing_commas = false, .maximum_depth = 100 }) catch return; | |
| 376 | var doc = json.parse(alloc, path, file, .{ .support_trailing_commas = false, .maximum_depth = 100 }) catch return; | |
| 374 | 377 | defer doc.deinit(alloc); |
| 375 | 378 | try std.testing.expect(false); |
| 376 | 379 | } |
| ... | ... | @@ -568,8 +571,8 @@ test { try expectFail(JSONTestSuite_root ++ "/n_structure_whitespace_U+2060_word |
| 568 | 571 | |
| 569 | 572 | fn expectCanonical(buffer: []const u8) !void { |
| 570 | 573 | const alloc = std.testing.allocator; |
| 571 | var fbs = std.io.fixedBufferStream(buffer); | |
| 572 | var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 574 | var fbs: nio.FixedBufferStream([]const u8) = .init(buffer); | |
| 575 | var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 573 | 576 | defer doc.deinit(alloc); |
| 574 | 577 | doc.acquire(); |
| 575 | 578 | defer doc.release(); |
| ... | ... | @@ -589,10 +592,10 @@ test { try expectCanonical("[7,8,9]"); } |
| 589 | 592 | |
| 590 | 593 | test { |
| 591 | 594 | const alloc = std.testing.allocator; |
| 592 | var fbs = std.io.fixedBufferStream( | |
| 595 | var fbs: nio.FixedBufferStream([]const u8) = .init( | |
| 593 | 596 | \\["abc",456,"ghi",{"foo":"bar"}] |
| 594 | 597 | ); |
| 595 | var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 598 | var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 }); | |
| 596 | 599 | defer doc.deinit(alloc); |
| 597 | 600 | doc.acquire(); |
| 598 | 601 | defer doc.release(); |
zig.mod+3| ... | ... | @@ -7,8 +7,11 @@ dependencies: |
| 7 | 7 | - src: git https://github.com/nektro/zig-extras |
| 8 | 8 | - src: git https://github.com/nektro/zig-tracer |
| 9 | 9 | - src: git https://github.com/nektro/zig-intrusive-parser |
| 10 | - src: git https://github.com/nektro/zig-nio | |
| 10 | 11 | |
| 11 | 12 | root_dependencies: |
| 13 | - src: git https://github.com/nektro/zig-nio | |
| 14 | - src: git https://github.com/nektro/zig-nfs | |
| 12 | 15 | - src: git https://github.com/nst/JSONTestSuite commit-984defc2deaa653cb73cd29f4144a720ec9efe7c |
| 13 | 16 | id: bebdygynna6kk8zbbprkva6yd28x9wmf57vtzxjn |
| 14 | 17 | license: MIT |