Skip to content

Commit 4835b9d

Browse files
committed
update std.json to new I/O API
1 parent a92dc05 commit 4835b9d

File tree

6 files changed

+77
-117
lines changed

6 files changed

+77
-117
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Provides the necessary building blocks to develop Language Server Protocol imple
1010
# Installation
1111

1212
> [!NOTE]
13-
> The default branch requires Zig `0.15.0-dev.1145+3ae0ba096` or later. Checkout the `0.14.x` branch when using Zig 0.14
13+
> The default branch requires Zig `0.15.0-dev.1160+e43617e68` or later. Checkout the `0.14.x` branch when using Zig 0.14
1414
1515
```bash
1616
# Initialize a `zig build` project if you haven't already

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
33

4-
const minimum_zig_version = "0.15.0-dev.1145+3ae0ba096";
4+
const minimum_zig_version = "0.15.0-dev.1160+e43617e68";
55

66
pub fn build(b: *std.Build) void {
77
comptime if (builtin.zig_version.order(std.SemanticVersion.parse(minimum_zig_version) catch unreachable) == .lt) {

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .lsp_kit,
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.15.0-dev.1145+3ae0ba096",
4+
.minimum_zig_version = "0.15.0-dev.1160+e43617e68",
55
.dependencies = .{},
66
.paths = .{
77
"build.zig",

src/lsp.zig

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -707,23 +707,23 @@ pub const JsonRPCMessage = union(enum) {
707707
test "emit_null_optional_fields" {
708708
try std.testing.expectFmt(
709709
\\{"jsonrpc":"2.0","method":"exit"}
710-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = false })});
710+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = false })});
711711
try std.testing.expectFmt(
712712
\\{"jsonrpc":"2.0","method":"exit","params":null}
713-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = true })});
713+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = true })});
714714
try std.testing.expectFmt(
715715
\\{"jsonrpc":"2.0","method":"exit","params":null}
716-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = false })});
716+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = false })});
717717
try std.testing.expectFmt(
718718
\\{"jsonrpc":"2.0","method":"exit","params":null}
719-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = true })});
719+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = true })});
720720

721721
try std.testing.expectFmt(
722722
\\{"jsonrpc":"2.0","result":null}
723-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = false })});
723+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = false })});
724724
try std.testing.expectFmt(
725725
\\{"jsonrpc":"2.0","id":null,"result":null}
726-
, "{f}", .{parser.jsonFmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = true })});
726+
, "{f}", .{std.json.fmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = true })});
727727
}
728728

729729
fn testParse(message: []const u8, expected: JsonRPCMessage, parse_options: std.json.ParseOptions) !void {
@@ -738,10 +738,10 @@ pub const JsonRPCMessage = union(enum) {
738738
const parsed_from_value = try std.json.parseFromValue(JsonRPCMessage, allocator, parsed_value.value, parse_options);
739739
defer parsed_from_value.deinit();
740740

741-
const from_slice_stringified = try std.json.stringifyAlloc(allocator, parsed_from_slice.value, .{ .whitespace = .indent_2 });
741+
const from_slice_stringified = try std.json.Stringify.valueAlloc(allocator, parsed_from_slice.value, .{ .whitespace = .indent_2 });
742742
defer allocator.free(from_slice_stringified);
743743

744-
const from_value_stringified = try std.json.stringifyAlloc(allocator, parsed_from_value.value, .{ .whitespace = .indent_2 });
744+
const from_value_stringified = try std.json.Stringify.valueAlloc(allocator, parsed_from_value.value, .{ .whitespace = .indent_2 });
745745
defer allocator.free(from_value_stringified);
746746

747747
if (!std.mem.eql(u8, from_slice_stringified, from_value_stringified)) {
@@ -872,13 +872,13 @@ test TypedJsonRPCRequest {
872872

873873
try std.testing.expectFmt(
874874
\\{"jsonrpc":"2.0","id":42,"method":"name","params":null}
875-
, "{f}", .{parser.jsonFmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = null }, .{})});
875+
, "{f}", .{std.json.fmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = null }, .{})});
876876
try std.testing.expectFmt(
877877
\\{"jsonrpc":"2.0","id":"42","method":"name"}
878-
, "{f}", .{parser.jsonFmt(Request{ .id = .{ .string = "42" }, .method = "name", .params = null }, .{ .emit_null_optional_fields = false })});
878+
, "{f}", .{std.json.fmt(Request{ .id = .{ .string = "42" }, .method = "name", .params = null }, .{ .emit_null_optional_fields = false })});
879879
try std.testing.expectFmt(
880880
\\{"jsonrpc":"2.0","id":42,"method":"name","params":true}
881-
, "{f}", .{parser.jsonFmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = true }, .{})});
881+
, "{f}", .{std.json.fmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = true }, .{})});
882882
}
883883

884884
pub fn TypedJsonRPCNotification(
@@ -924,13 +924,13 @@ test TypedJsonRPCNotification {
924924

925925
try std.testing.expectFmt(
926926
\\{"jsonrpc":"2.0","method":"name","params":null}
927-
, "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = null }, .{})});
927+
, "{f}", .{std.json.fmt(Notification{ .method = "name", .params = null }, .{})});
928928
try std.testing.expectFmt(
929929
\\{"jsonrpc":"2.0","method":"name"}
930-
, "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = null }, .{ .emit_null_optional_fields = false })});
930+
, "{f}", .{std.json.fmt(Notification{ .method = "name", .params = null }, .{ .emit_null_optional_fields = false })});
931931
try std.testing.expectFmt(
932932
\\{"jsonrpc":"2.0","method":"name","params":true}
933-
, "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = true }, .{})});
933+
, "{f}", .{std.json.fmt(Notification{ .method = "name", .params = true }, .{})});
934934
}
935935

936936
pub fn TypedJsonRPCResponse(
@@ -982,13 +982,13 @@ test TypedJsonRPCResponse {
982982

983983
try std.testing.expectFmt(
984984
\\{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"message","data":null}}
985-
, "{f}", .{parser.jsonFmt(Response{
985+
, "{f}", .{std.json.fmt(Response{
986986
.id = null,
987987
.result_or_error = .{ .@"error" = .{ .code = .invalid_request, .message = "message", .data = .null } },
988988
}, .{})});
989989
try std.testing.expectFmt(
990990
\\{"jsonrpc":"2.0","id":5,"result":true}
991-
, "{f}", .{parser.jsonFmt(Response{
991+
, "{f}", .{std.json.fmt(Response{
992992
.id = .{ .number = 5 },
993993
.result_or_error = .{ .result = true },
994994
}, .{})});
@@ -1205,14 +1205,14 @@ pub const Transport = struct {
12051205
method: []const u8,
12061206
comptime Params: type,
12071207
params: Params,
1208-
options: std.json.StringifyOptions,
1208+
options: std.json.Stringify.Options,
12091209
) (WriteError || std.mem.Allocator.Error)!void {
12101210
const request: TypedJsonRPCRequest(Params) = .{
12111211
.id = id,
12121212
.method = method,
12131213
.params = params,
12141214
};
1215-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1215+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12161216
defer allocator.free(json_message);
12171217
try transport.writeJsonMessage(json_message);
12181218
}
@@ -1223,13 +1223,13 @@ pub const Transport = struct {
12231223
method: []const u8,
12241224
comptime Params: type,
12251225
params: Params,
1226-
options: std.json.StringifyOptions,
1226+
options: std.json.Stringify.Options,
12271227
) (WriteError || std.mem.Allocator.Error)!void {
12281228
const request: TypedJsonRPCNotification(Params) = .{
12291229
.method = method,
12301230
.params = params,
12311231
};
1232-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1232+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12331233
defer allocator.free(json_message);
12341234
try transport.writeJsonMessage(json_message);
12351235
}
@@ -1240,13 +1240,13 @@ pub const Transport = struct {
12401240
id: ?JsonRPCMessage.ID,
12411241
comptime Result: type,
12421242
result: Result,
1243-
options: std.json.StringifyOptions,
1243+
options: std.json.Stringify.Options,
12441244
) (WriteError || std.mem.Allocator.Error)!void {
12451245
const request: TypedJsonRPCResponse(Result) = .{
12461246
.id = id,
12471247
.result_or_error = .{ .result = result },
12481248
};
1249-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1249+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12501250
defer allocator.free(json_message);
12511251
try transport.writeJsonMessage(json_message);
12521252
}
@@ -1256,13 +1256,13 @@ pub const Transport = struct {
12561256
allocator: std.mem.Allocator,
12571257
id: ?JsonRPCMessage.ID,
12581258
err: JsonRPCMessage.Response.Error,
1259-
options: std.json.StringifyOptions,
1259+
options: std.json.Stringify.Options,
12601260
) (WriteError || std.mem.Allocator.Error)!void {
12611261
const request: TypedJsonRPCResponse(void) = .{
12621262
.id = id,
12631263
.result_or_error = .{ .@"error" = err },
12641264
};
1265-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1265+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12661266
defer allocator.free(json_message);
12671267
try transport.writeJsonMessage(json_message);
12681268
}
@@ -1375,14 +1375,14 @@ pub fn writeRequest(
13751375
method: []const u8,
13761376
comptime Params: type,
13771377
params: Params,
1378-
options: std.json.StringifyOptions,
1378+
options: std.json.Stringify.Options,
13791379
) (std.io.Writer.Error || std.mem.Allocator.Error)!void {
13801380
const request: TypedJsonRPCRequest(Params) = .{
13811381
.id = id,
13821382
.method = method,
13831383
.params = params,
13841384
};
1385-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1385+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
13861386
defer allocator.free(json_message);
13871387
try writeJsonMessage(writer, json_message);
13881388
}
@@ -1418,13 +1418,13 @@ pub fn writeNotification(
14181418
method: []const u8,
14191419
comptime Params: type,
14201420
params: Params,
1421-
options: std.json.StringifyOptions,
1421+
options: std.json.Stringify.Options,
14221422
) (std.io.Writer.Error || std.mem.Allocator.Error)!void {
14231423
const request: TypedJsonRPCNotification(Params) = .{
14241424
.method = method,
14251425
.params = params,
14261426
};
1427-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1427+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
14281428
defer allocator.free(json_message);
14291429
try writeJsonMessage(writer, json_message);
14301430
}
@@ -1458,13 +1458,13 @@ pub fn writeResponse(
14581458
id: ?JsonRPCMessage.ID,
14591459
comptime Result: type,
14601460
result: Result,
1461-
options: std.json.StringifyOptions,
1461+
options: std.json.Stringify.Options,
14621462
) (std.io.Writer.Error || std.mem.Allocator.Error)!void {
14631463
const request: TypedJsonRPCResponse(Result) = .{
14641464
.id = id,
14651465
.result_or_error = .{ .result = result },
14661466
};
1467-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1467+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
14681468
defer allocator.free(json_message);
14691469
try writeJsonMessage(writer, json_message);
14701470
}
@@ -1497,13 +1497,13 @@ pub fn writeErrorResponse(
14971497
allocator: std.mem.Allocator,
14981498
id: ?JsonRPCMessage.ID,
14991499
err: JsonRPCMessage.Response.Error,
1500-
options: std.json.StringifyOptions,
1500+
options: std.json.Stringify.Options,
15011501
) (std.io.Writer.Error || std.mem.Allocator.Error)!void {
15021502
const request: TypedJsonRPCResponse(void) = .{
15031503
.id = id,
15041504
.result_or_error = .{ .@"error" = err },
15051505
};
1506-
const json_message = try std.json.stringifyAlloc(allocator, request, options);
1506+
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
15071507
defer allocator.free(json_message);
15081508
try writeJsonMessage(writer, json_message);
15091509
}
@@ -1571,7 +1571,7 @@ fn bufPrintLogMessageTypeErased(
15711571
var writer: std.io.Writer = .fixed(buffer);
15721572
writer.print(
15731573
\\{{"jsonrpc":"2.0","method":"window/logMessage","params":{{"type":{f},"message":"
1574-
, .{parser.jsonFmt(message_type, .{})}) catch unreachable;
1574+
, .{std.json.fmt(message_type, .{})}) catch unreachable;
15751575

15761576
const json_message_suffix = "\"}}".*;
15771577
const ellipses = "...".*;
@@ -2332,9 +2332,9 @@ fn testMessageWithOptions(
23322332
const message_from_value = try std.json.parseFromValueLeaky(ExampleMessage, arena.allocator(), value, parse_options);
23332333
const message_from_slice = try ExampleMessage.parseFromSliceLeaky(arena.allocator(), json_message, parse_options);
23342334

2335-
const message_string = try std.json.stringifyAlloc(arena.allocator(), message, .{ .whitespace = .indent_2 });
2336-
const message_from_value_string = try std.json.stringifyAlloc(arena.allocator(), message_from_value, .{ .whitespace = .indent_2 });
2337-
const message_from_slice_string = try std.json.stringifyAlloc(arena.allocator(), message_from_slice, .{ .whitespace = .indent_2 });
2335+
const message_string = try std.json.Stringify.valueAlloc(arena.allocator(), message, .{ .whitespace = .indent_2 });
2336+
const message_from_value_string = try std.json.Stringify.valueAlloc(arena.allocator(), message_from_value, .{ .whitespace = .indent_2 });
2337+
const message_from_slice_string = try std.json.Stringify.valueAlloc(arena.allocator(), message_from_slice, .{ .whitespace = .indent_2 });
23382338

23392339
try std.testing.expectEqualStrings(message_string, message_from_value_string);
23402340
try std.testing.expectEqualStrings(message_string, message_from_slice_string);
@@ -2422,23 +2422,23 @@ test "Message - ignore_unknown_fields" {
24222422
test "Message - stringify emit_null_optional_fields" {
24232423
try std.testing.expectFmt(
24242424
\\{"jsonrpc":"2.0","method":"exit"}
2425-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = false })});
2425+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = false })});
24262426
try std.testing.expectFmt(
24272427
\\{"jsonrpc":"2.0","method":"exit","params":null}
2428-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = true })});
2428+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = true })});
24292429

24302430
try std.testing.expectFmt(
24312431
\\{"jsonrpc":"2.0","method":"foo"}
2432-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = false })});
2432+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = false })});
24332433
try std.testing.expectFmt(
24342434
\\{"jsonrpc":"2.0","method":"foo","params":null}
2435-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = true })});
2435+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = true })});
24362436
try std.testing.expectFmt(
24372437
\\{"jsonrpc":"2.0","method":"foo","params":null}
2438-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = false })});
2438+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = false })});
24392439
try std.testing.expectFmt(
24402440
\\{"jsonrpc":"2.0","method":"foo","params":null}
2441-
, "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = true })});
2441+
, "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = true })});
24422442
}
24432443

24442444
test "Message.Request" {

src/main.zig

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ fn writeRequest(writer: *std.io.Writer, meta_model: MetaModel, request: MetaMode
338338
\\
339339
, .{
340340
request.method,
341-
if (request.documentation) |documentation| jsonFmt(documentation, .{}) else null,
341+
if (request.documentation) |documentation| std.json.fmt(documentation, .{}) else null,
342342
messageDirectionName(request.messageDirection),
343343
// NOTE: Multiparams not used here, so we dont have to implement them :)
344344
if (request.params) |params| fmtType(params.Type, &meta_model) else null,
345345
fmtType(request.result, &meta_model),
346346
if (request.partialResult) |ty| fmtType(ty, &meta_model) else null,
347347
if (request.errorData) |ty| fmtType(ty, &meta_model) else null,
348-
if (request.registrationMethod) |method| jsonFmt(method, .{}) else null,
348+
if (request.registrationMethod) |method| std.json.fmt(method, .{}) else null,
349349
if (request.registrationOptions) |ty| fmtType(ty, &meta_model) else null,
350350
});
351351
}
@@ -364,11 +364,11 @@ fn writeNotification(writer: *std.io.Writer, meta_model: MetaModel, notification
364364
\\
365365
, .{
366366
notification.method,
367-
if (notification.documentation) |documentation| jsonFmt(documentation, .{}) else null,
367+
if (notification.documentation) |documentation| std.json.fmt(documentation, .{}) else null,
368368
messageDirectionName(notification.messageDirection),
369369
// NOTE: Multiparams not used here, so we dont have to implement them :)
370370
if (notification.params) |params| fmtType(params.Type, &meta_model) else null,
371-
if (notification.registrationMethod) |method| jsonFmt(method, .{}) else null,
371+
if (notification.registrationMethod) |method| std.json.fmt(method, .{}) else null,
372372
if (notification.registrationOptions) |ty| fmtType(ty, &meta_model) else null,
373373
});
374374
}
@@ -479,23 +479,3 @@ fn writeMetaModel(writer: *std.io.Writer, meta_model: MetaModel) std.io.Writer.E
479479
}
480480
try writer.writeAll("};\n");
481481
}
482-
483-
/// Like `std.json.fmt` but supports `std.io.Writer`.
484-
pub fn jsonFmt(value: anytype, options: std.json.StringifyOptions) std.fmt.Alt(FormatJson(@TypeOf(value)), FormatJson(@TypeOf(value)).format) {
485-
return .{ .data = .{ .value = value, .options = options } };
486-
}
487-
488-
fn FormatJson(comptime T: type) type {
489-
return struct {
490-
value: T,
491-
options: std.json.StringifyOptions,
492-
493-
pub fn format(data: @This(), writer: *std.io.Writer) std.io.Writer.Error!void {
494-
const any_writer: std.io.AnyWriter = .{
495-
.context = @ptrCast(writer),
496-
.writeFn = @ptrCast(&std.io.Writer.write),
497-
};
498-
std.json.stringify(data.value, data.options, any_writer) catch |err| return @errorCast(err);
499-
}
500-
};
501-
}

0 commit comments

Comments
 (0)