feat: add message formatter
This commit is contained in:
parent
687e62466a
commit
f0cf0e7aac
1 changed files with 10 additions and 1 deletions
11
src/cbor.zig
11
src/cbor.zig
|
@ -897,8 +897,11 @@ pub fn extract_cbor(dest: *[]const u8) CborExtractor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn JsonStream(comptime T: type) type {
|
pub fn JsonStream(comptime T: type) type {
|
||||||
|
return JsonStreamWriter(T.Writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn JsonStreamWriter(comptime Writer: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Writer = T.Writer;
|
|
||||||
const JsonWriter = json.WriteStream(Writer, .{ .checked_to_fixed_depth = 256 });
|
const JsonWriter = json.WriteStream(Writer, .{ .checked_to_fixed_depth = 256 });
|
||||||
|
|
||||||
fn jsonWriteArray(w: *JsonWriter, iter: *[]const u8, minor: u5) !void {
|
fn jsonWriteArray(w: *JsonWriter, iter: *[]const u8, minor: u5) !void {
|
||||||
|
@ -954,6 +957,12 @@ pub fn toJson(cbor_buf: []const u8, json_buf: []u8) (JsonEncodeError || error{No
|
||||||
return fbs.getWritten();
|
return fbs.getWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn toJsonWriter(cbor_buf: []const u8, writer: anytype, options: std.json.StringifyOptions) !void {
|
||||||
|
var s = json.writeStream(writer, options);
|
||||||
|
var iter: []const u8 = cbor_buf;
|
||||||
|
try JsonStreamWriter(@TypeOf(writer)).jsonWriteValue(&s, &iter);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn toJsonAlloc(a: std.mem.Allocator, cbor_buf: []const u8) (JsonEncodeError)![]const u8 {
|
pub fn toJsonAlloc(a: std.mem.Allocator, cbor_buf: []const u8) (JsonEncodeError)![]const u8 {
|
||||||
var buf = std.ArrayList(u8).init(a);
|
var buf = std.ArrayList(u8).init(a);
|
||||||
defer buf.deinit();
|
defer buf.deinit();
|
||||||
|
|
Loading…
Add table
Reference in a new issue